blob: bef1f0da4840ab3048bd6661eff1b59dc406eb63 [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 Chungb745afb2015-03-02 11:51:23 -080027import android.graphics.Point;
Winson Chung043f2af2012-03-01 16:09:54 -080028import android.graphics.PointF;
Romain Guyfb5411e2010-02-24 10:04:17 -080029import android.graphics.Rect;
Sunny Goyalc46bfef2015-01-05 12:40:08 -080030import android.os.Build;
Adam Cohen7a14d0b2011-06-24 11:36:35 -070031import android.text.InputType;
Adam Cohene601a432011-07-26 21:51:30 -070032import android.text.Selection;
33import android.text.Spannable;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080034import android.util.AttributeSet;
Adam Cohen3bf84d32012-05-07 20:17:14 -070035import android.util.Log;
Adam Cohen76fc0852011-06-17 13:26:23 -070036import android.view.ActionMode;
37import android.view.KeyEvent;
Adam Cohendf2cc412011-04-27 16:56:57 -070038import android.view.LayoutInflater;
Adam Cohen76fc0852011-06-17 13:26:23 -070039import android.view.Menu;
40import android.view.MenuItem;
Adam Cohen0c872ba2011-05-05 10:34:16 -070041import android.view.MotionEvent;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080042import android.view.View;
Sunny Goyalc4918352015-03-10 18:15:48 -070043import android.view.ViewGroup;
Adam Cohen3371da02011-10-25 21:38:29 -070044import android.view.accessibility.AccessibilityEvent;
45import android.view.accessibility.AccessibilityManager;
Adam Cohenc4fe9ea2014-08-18 18:54:10 -070046import android.view.animation.AccelerateInterpolator;
Adam Cohen76fc0852011-06-17 13:26:23 -070047import android.view.inputmethod.EditorInfo;
48import android.view.inputmethod.InputMethodManager;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080049import android.widget.LinearLayout;
Adam Cohendf2cc412011-04-27 16:56:57 -070050import android.widget.TextView;
Daniel Sandler325dc232013-06-05 22:57:57 -040051import com.android.launcher3.FolderInfo.FolderListener;
Sunny Goyalbc753352015-03-05 09:40:44 -080052import com.android.launcher3.Workspace.ItemOperator;
Romain Guyedcce092010-03-04 13:03:17 -080053
Adam Cohenc0dcf592011-06-01 15:30:43 -070054import java.util.ArrayList;
Adam Cohen3bf84d32012-05-07 20:17:14 -070055import java.util.Collections;
Adam Cohenc0dcf592011-06-01 15:30:43 -070056
The Android Open Source Project31dd5032009-03-03 19:32:27 -080057/**
58 * Represents a set of icons chosen by the user or generated by the system.
59 */
Adam Cohen8dfcba42011-07-07 16:38:18 -070060public class Folder extends LinearLayout implements DragSource, View.OnClickListener,
Adam Cohenea0818d2011-09-30 20:06:58 -070061 View.OnLongClickListener, DropTarget, FolderListener, TextView.OnEditorActionListener,
62 View.OnFocusChangeListener {
Adam Cohendf2cc412011-04-27 16:56:57 -070063 private static final String TAG = "Launcher.Folder";
64
Sunny Goyalc3a609f2015-02-26 17:43:50 -080065 /**
66 * We avoid measuring {@link #mContentWrapper} with a 0 width or height, as this
67 * results in CellLayout being measured as UNSPECIFIED, which it does not support.
68 */
69 private static final int MIN_CONTENT_DIMEN = 5;
Sunny Goyal290800b2015-03-05 11:33:33 -080070 private static final boolean ALLOW_FOLDER_SCROLL = true;
Adam Cohen4eac29a2011-07-11 17:53:37 -070071
Adam Cohendf2cc412011-04-27 16:56:57 -070072 static final int STATE_NONE = -1;
73 static final int STATE_SMALL = 0;
74 static final int STATE_ANIMATING = 1;
75 static final int STATE_OPEN = 2;
76
Adam Cohenf0f4eda2013-06-06 21:27:03 -070077 private static final int REORDER_DELAY = 250;
Adam Cohen5d518fa2013-12-05 14:16:23 -080078 private static final int ON_EXIT_CLOSE_DELAY = 400;
Sunny Goyalc3a609f2015-02-26 17:43:50 -080079 private static final Rect sTempRect = new Rect();
80
81 private static String sDefaultFolderName;
82 private static String sHintText;
83
84 private final Alarm mReorderAlarm = new Alarm();
85 private final Alarm mOnExitAlarm = new Alarm();
86
87 private final ArrayList<View> mItemsInReadingOrder = new ArrayList<View>();
88
89 private final int mExpandDuration;
90 private final int mMaterialExpandDuration;
91 private final int mMaterialExpandStagger;
92
93 private final InputMethodManager mInputMethodManager;
94
95 protected final Launcher mLauncher;
96 protected DragController mDragController;
97 protected FolderInfo mInfo;
98
Adam Cohen2801caf2011-05-13 20:57:39 -070099 private FolderIcon mFolderIcon;
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800100
Sunny Goyalbc753352015-03-05 09:40:44 -0800101 private FolderContent mContent;
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800102 private View mContentWrapper;
103 FolderEditText mFolderName;
104
Sunny Goyal290800b2015-03-05 11:33:33 -0800105 private View mFooter;
106 private int mFooterHeight;
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800107
108 // Cell ranks used for drag and drop
109 private int mTargetRank, mPrevTargetRank, mEmptyCellRank;
110
111 private int mState = STATE_NONE;
112 private boolean mRearrangeOnClose = false;
Adam Cohen7c693212011-05-18 15:26:57 -0700113 boolean mItemsInvalidated = false;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700114 private ShortcutInfo mCurrentDragInfo;
115 private View mCurrentDragView;
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800116 private boolean mIsExternalDrag;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700117 boolean mSuppressOnAdd = false;
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700118 private boolean mDragInProgress = false;
119 private boolean mDeleteFolderOnDropCompleted = false;
120 private boolean mSuppressFolderDeletion = false;
Adam Cohen05e0f402011-08-01 12:12:49 -0700121 private boolean mItemAddedBackToSelfViaIcon = false;
Adam Cohen268c4752012-06-06 17:47:33 -0700122 private float mFolderIconPivotX;
123 private float mFolderIconPivotY;
Adam Cohen76fc0852011-06-17 13:26:23 -0700124 private boolean mIsEditingName = false;
Adam Cohen1960ea42013-11-12 11:33:14 +0000125
Adam Cohenfb91f302012-06-11 15:45:18 -0700126 private boolean mDestroyed;
127
Michael Jurka1e2f4652013-07-08 18:03:46 -0700128 private Runnable mDeferredAction;
129 private boolean mDeferDropAfterUninstall;
130 private boolean mUninstallSuccessful;
131
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800132 /**
133 * Used to inflate the Workspace from XML.
134 *
135 * @param context The application's context.
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800136 * @param attrs The attributes set containing the Workspace's customization values.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800137 */
138 public Folder(Context context, AttributeSet attrs) {
139 super(context, attrs);
140 setAlwaysDrawnWithCacheEnabled(false);
Adam Cohen76fc0852011-06-17 13:26:23 -0700141 mInputMethodManager = (InputMethodManager)
Michael Jurka8b805b12012-04-18 14:23:14 -0700142 getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
Adam Cohen76fc0852011-06-17 13:26:23 -0700143
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800144 Resources res = getResources();
Adam Cohenc4fe9ea2014-08-18 18:54:10 -0700145 mExpandDuration = res.getInteger(R.integer.config_folderExpandDuration);
146 mMaterialExpandDuration = res.getInteger(R.integer.config_materialFolderExpandDuration);
147 mMaterialExpandStagger = res.getInteger(R.integer.config_materialFolderExpandStagger);
Adam Cohen4ef610f2011-06-21 22:37:36 -0700148
149 if (sDefaultFolderName == null) {
150 sDefaultFolderName = res.getString(R.string.folder_name);
151 }
Adam Cohena65beee2011-06-27 21:32:23 -0700152 if (sHintText == null) {
153 sHintText = res.getString(R.string.folder_hint_text);
154 }
Adam Cohen4eac29a2011-07-11 17:53:37 -0700155 mLauncher = (Launcher) context;
Adam Cohenac56cff2011-09-28 20:45:37 -0700156 // We need this view to be focusable in touch mode so that when text editing of the folder
157 // name is complete, we have something to focus on, thus hiding the cursor and giving
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800158 // reliable behavior when clicking the text field (since it will always gain focus on click).
Adam Cohenac56cff2011-09-28 20:45:37 -0700159 setFocusableInTouchMode(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800160 }
161
162 @Override
163 protected void onFinishInflate() {
164 super.onFinishInflate();
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800165 mContentWrapper = findViewById(R.id.folder_content_wrapper);
Sunny Goyalbc753352015-03-05 09:40:44 -0800166 mContent = (FolderContent) findViewById(R.id.folder_content);
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800167 mContent.setFolder(this);
Sunny Goyaldcbcc862014-08-12 15:58:36 -0700168
Adam Cohenac56cff2011-09-28 20:45:37 -0700169 mFolderName = (FolderEditText) findViewById(R.id.folder_name);
170 mFolderName.setFolder(this);
Adam Cohenea0818d2011-09-30 20:06:58 -0700171 mFolderName.setOnFocusChangeListener(this);
Adam Cohen76fc0852011-06-17 13:26:23 -0700172
Adam Cohen76fc0852011-06-17 13:26:23 -0700173 // We disable action mode for now since it messes up the view on phones
174 mFolderName.setCustomSelectionActionModeCallback(mActionModeCallback);
Adam Cohen76fc0852011-06-17 13:26:23 -0700175 mFolderName.setOnEditorActionListener(this);
Adam Cohen4ef610f2011-06-21 22:37:36 -0700176 mFolderName.setSelectAllOnFocus(true);
Adam Cohen7a14d0b2011-06-24 11:36:35 -0700177 mFolderName.setInputType(mFolderName.getInputType() |
178 InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS | InputType.TYPE_TEXT_FLAG_CAP_WORDS);
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800179
Sunny Goyal290800b2015-03-05 11:33:33 -0800180 mFooter = ALLOW_FOLDER_SCROLL ? findViewById(R.id.folder_footer) : mFolderName;
181 // We find out how tall footer wants to be (it is set to wrap_content), so that
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800182 // we can allocate the appropriate amount of space for it.
183 int measureSpec = MeasureSpec.UNSPECIFIED;
Sunny Goyal290800b2015-03-05 11:33:33 -0800184 mFooter.measure(measureSpec, measureSpec);
185 mFooterHeight = mFooter.getMeasuredHeight();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800186 }
Adam Cohen2801caf2011-05-13 20:57:39 -0700187
Adam Cohen76fc0852011-06-17 13:26:23 -0700188 private ActionMode.Callback mActionModeCallback = new ActionMode.Callback() {
189 public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
190 return false;
191 }
192
193 public boolean onCreateActionMode(ActionMode mode, Menu menu) {
194 return false;
195 }
196
197 public void onDestroyActionMode(ActionMode mode) {
198 }
199
200 public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
201 return false;
202 }
203 };
204
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800205 public void onClick(View v) {
Adam Cohendf2cc412011-04-27 16:56:57 -0700206 Object tag = v.getTag();
207 if (tag instanceof ShortcutInfo) {
Adam Cohenb5fe60c2013-06-06 22:03:51 -0700208 mLauncher.onClick(v);
Adam Cohendf2cc412011-04-27 16:56:57 -0700209 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800210 }
211
212 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -0700213 // Return if global dragging is not enabled
214 if (!mLauncher.isDraggingEnabled()) return true;
215
Adam Cohendf2cc412011-04-27 16:56:57 -0700216 Object tag = v.getTag();
217 if (tag instanceof ShortcutInfo) {
Adam Cohendf2cc412011-04-27 16:56:57 -0700218 ShortcutInfo item = (ShortcutInfo) tag;
219 if (!v.isInTouchMode()) {
220 return false;
221 }
222
Winson Chungb745afb2015-03-02 11:51:23 -0800223 mLauncher.getWorkspace().beginDragShared(v, new Point(), this, false);
Adam Cohen76078c42011-06-09 15:06:52 -0700224
225 mCurrentDragInfo = item;
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800226 mEmptyCellRank = item.rank;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700227 mCurrentDragView = v;
Adam Cohenfc53cd22011-07-20 15:45:11 -0700228
Sunny Goyal290800b2015-03-05 11:33:33 -0800229 mContent.removeItem(mCurrentDragView);
Adam Cohenfc53cd22011-07-20 15:45:11 -0700230 mInfo.remove(mCurrentDragInfo);
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700231 mDragInProgress = true;
Adam Cohen05e0f402011-08-01 12:12:49 -0700232 mItemAddedBackToSelfViaIcon = false;
Adam Cohendf2cc412011-04-27 16:56:57 -0700233 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800234 return true;
235 }
236
Adam Cohen76fc0852011-06-17 13:26:23 -0700237 public boolean isEditingName() {
238 return mIsEditingName;
239 }
240
241 public void startEditingFolderName() {
Adam Cohena65beee2011-06-27 21:32:23 -0700242 mFolderName.setHint("");
Adam Cohen76fc0852011-06-17 13:26:23 -0700243 mIsEditingName = true;
244 }
245
246 public void dismissEditingName() {
247 mInputMethodManager.hideSoftInputFromWindow(getWindowToken(), 0);
248 doneEditingFolderName(true);
249 }
250
251 public void doneEditingFolderName(boolean commit) {
Adam Cohena65beee2011-06-27 21:32:23 -0700252 mFolderName.setHint(sHintText);
Adam Cohen1df26a32011-08-12 16:15:23 -0700253 // Convert to a string here to ensure that no other state associated with the text field
254 // gets saved.
Adam Cohen3371da02011-10-25 21:38:29 -0700255 String newTitle = mFolderName.getText().toString();
256 mInfo.setTitle(newTitle);
Adam Cohen76fc0852011-06-17 13:26:23 -0700257 LauncherModel.updateItemInDatabase(mLauncher, mInfo);
Adam Cohenac56cff2011-09-28 20:45:37 -0700258
Adam Cohen3371da02011-10-25 21:38:29 -0700259 if (commit) {
260 sendCustomAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED,
Michael Jurka8b805b12012-04-18 14:23:14 -0700261 String.format(getContext().getString(R.string.folder_renamed), newTitle));
Adam Cohen3371da02011-10-25 21:38:29 -0700262 }
Adam Cohenac56cff2011-09-28 20:45:37 -0700263 // In order to clear the focus from the text field, we set the focus on ourself. This
264 // ensures that every time the field is clicked, focus is gained, giving reliable behavior.
265 requestFocus();
266
Adam Cohene601a432011-07-26 21:51:30 -0700267 Selection.setSelection((Spannable) mFolderName.getText(), 0, 0);
Adam Cohen76fc0852011-06-17 13:26:23 -0700268 mIsEditingName = false;
269 }
270
271 public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
272 if (actionId == EditorInfo.IME_ACTION_DONE) {
273 dismissEditingName();
274 return true;
275 }
276 return false;
277 }
278
279 public View getEditTextRegion() {
280 return mFolderName;
281 }
282
Adam Cohen0c872ba2011-05-05 10:34:16 -0700283 /**
284 * We need to handle touch events to prevent them from falling through to the workspace below.
285 */
286 @Override
287 public boolean onTouchEvent(MotionEvent ev) {
288 return true;
289 }
290
Joe Onorato00acb122009-08-04 16:04:30 -0400291 public void setDragController(DragController dragController) {
292 mDragController = dragController;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800293 }
294
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800295 public void setFolderIcon(FolderIcon icon) {
Adam Cohen2801caf2011-05-13 20:57:39 -0700296 mFolderIcon = icon;
297 }
298
Adam Cohen3371da02011-10-25 21:38:29 -0700299 @Override
300 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
301 // When the folder gets focus, we don't want to announce the list of items.
302 return true;
303 }
304
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800305 /**
306 * @return the FolderInfo object associated with this folder
307 */
308 FolderInfo getInfo() {
309 return mInfo;
310 }
311
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800312 void bind(FolderInfo info) {
313 mInfo = info;
Adam Cohendf2cc412011-04-27 16:56:57 -0700314 ArrayList<ShortcutInfo> children = info.contents;
Sunny Goyal08f72612015-01-05 13:41:43 -0800315 Collections.sort(children, Utilities.RANK_COMPARATOR);
Sunny Goyal08f72612015-01-05 13:41:43 -0800316
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800317 ArrayList<ShortcutInfo> overflow = mContent.bindItems(children);
Adam Cohen0057bbc2011-08-12 18:30:51 -0700318
Jason Monk4ff73882014-04-24 16:48:07 -0400319 // If our folder has too many items we prune them from the list. This is an issue
Adam Cohenc508b2d2011-06-28 14:41:44 -0700320 // when upgrading from the old Folders implementation which could contain an unlimited
321 // number of items.
322 for (ShortcutInfo item: overflow) {
323 mInfo.remove(item);
324 LauncherModel.deleteItemFromDatabase(mLauncher, item);
325 }
326
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800327 DragLayer.LayoutParams lp = (DragLayer.LayoutParams) getLayoutParams();
328 if (lp == null) {
329 lp = new DragLayer.LayoutParams(0, 0);
330 lp.customPosition = true;
331 setLayoutParams(lp);
332 }
333 centerAboutIcon();
334
Adam Cohen4dbe6d92011-05-18 17:14:15 -0700335 mItemsInvalidated = true;
Adam Cohenac56cff2011-09-28 20:45:37 -0700336 updateTextViewFocus();
Adam Cohena9cf38f2011-05-02 15:36:58 -0700337 mInfo.addListener(this);
Adam Cohen4ef610f2011-06-21 22:37:36 -0700338
Adam Cohenafb01ee2011-06-23 15:38:03 -0700339 if (!sDefaultFolderName.contentEquals(mInfo.title)) {
Adam Cohen4ef610f2011-06-21 22:37:36 -0700340 mFolderName.setText(mInfo.title);
341 } else {
342 mFolderName.setText("");
343 }
Winson Chung33231f52013-12-09 16:57:45 -0800344
345 // In case any children didn't come across during loading, clean up the folder accordingly
346 mFolderIcon.post(new Runnable() {
347 public void run() {
348 if (getItemCount() <= 1) {
349 replaceFolderWithFinalItem();
350 }
351 }
352 });
Adam Cohendf2cc412011-04-27 16:56:57 -0700353 }
354
355 /**
356 * Creates a new UserFolder, inflated from R.layout.user_folder.
357 *
358 * @param context The application's context.
359 *
360 * @return A new UserFolder.
361 */
362 static Folder fromXml(Context context) {
Sunny Goyal290800b2015-03-05 11:33:33 -0800363 return (Folder) LayoutInflater.from(context).inflate(
364 ALLOW_FOLDER_SCROLL ? R.layout.user_folder_scroll : R.layout.user_folder, null);
Adam Cohendf2cc412011-04-27 16:56:57 -0700365 }
366
367 /**
368 * This method is intended to make the UserFolder to be visually identical in size and position
369 * to its associated FolderIcon. This allows for a seamless transition into the expanded state.
370 */
371 private void positionAndSizeAsIcon() {
Adam Cohen3e8f8112011-07-02 18:03:00 -0700372 if (!(getParent() instanceof DragLayer)) return;
Adam Cohen662b5982011-12-13 17:45:21 -0800373 setScaleX(0.8f);
374 setScaleY(0.8f);
375 setAlpha(0f);
Adam Cohendf2cc412011-04-27 16:56:57 -0700376 mState = STATE_SMALL;
377 }
378
Adam Cohenc4fe9ea2014-08-18 18:54:10 -0700379 private void prepareReveal() {
380 setScaleX(1f);
381 setScaleY(1f);
382 setAlpha(1f);
383 mState = STATE_SMALL;
384 }
385
Adam Cohendf2cc412011-04-27 16:56:57 -0700386 public void animateOpen() {
Adam Cohen8e776a62011-06-28 18:10:06 -0700387 if (!(getParent() instanceof DragLayer)) return;
Adam Cohen6441de02011-12-14 14:25:32 -0800388
Adam Cohenc4fe9ea2014-08-18 18:54:10 -0700389 Animator openFolderAnim = null;
390 final Runnable onCompleteRunnable;
Kenny Guyd794a3f2014-09-16 15:17:58 +0100391 if (!Utilities.isLmpOrAbove()) {
Adam Cohenc4fe9ea2014-08-18 18:54:10 -0700392 positionAndSizeAsIcon();
393 centerAboutIcon();
394
395 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1);
396 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.0f);
397 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.0f);
398 final ObjectAnimator oa =
399 LauncherAnimUtils.ofPropertyValuesHolder(this, alpha, scaleX, scaleY);
400 oa.setDuration(mExpandDuration);
401 openFolderAnim = oa;
402
403 setLayerType(LAYER_TYPE_HARDWARE, null);
404 onCompleteRunnable = new Runnable() {
405 @Override
406 public void run() {
407 setLayerType(LAYER_TYPE_NONE, null);
408 }
409 };
410 } else {
411 prepareReveal();
412 centerAboutIcon();
413
414 int width = getPaddingLeft() + getPaddingRight() + mContent.getDesiredWidth();
415 int height = getFolderHeight();
416
417 float transX = - 0.075f * (width / 2 - getPivotX());
418 float transY = - 0.075f * (height / 2 - getPivotY());
419 setTranslationX(transX);
420 setTranslationY(transY);
421 PropertyValuesHolder tx = PropertyValuesHolder.ofFloat("translationX", transX, 0);
422 PropertyValuesHolder ty = PropertyValuesHolder.ofFloat("translationY", transY, 0);
423
424 int rx = (int) Math.max(Math.max(width - getPivotX(), 0), getPivotX());
425 int ry = (int) Math.max(Math.max(height - getPivotY(), 0), getPivotY());
426 float radius = (float) Math.sqrt(rx * rx + ry * ry);
427 AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
428 Animator reveal = LauncherAnimUtils.createCircularReveal(this, (int) getPivotX(),
429 (int) getPivotY(), 0, radius);
430 reveal.setDuration(mMaterialExpandDuration);
431 reveal.setInterpolator(new LogDecelerateInterpolator(100, 0));
432
Sunny Goyalbc753352015-03-05 09:40:44 -0800433 mContentWrapper.setAlpha(0f);
434 Animator iconsAlpha = LauncherAnimUtils.ofFloat(mContentWrapper, "alpha", 0f, 1f);
Adam Cohenc4fe9ea2014-08-18 18:54:10 -0700435 iconsAlpha.setDuration(mMaterialExpandDuration);
436 iconsAlpha.setStartDelay(mMaterialExpandStagger);
437 iconsAlpha.setInterpolator(new AccelerateInterpolator(1.5f));
438
Sunny Goyal290800b2015-03-05 11:33:33 -0800439 mFooter.setAlpha(0f);
440 Animator textAlpha = LauncherAnimUtils.ofFloat(mFooter, "alpha", 0f, 1f);
Adam Cohenc4fe9ea2014-08-18 18:54:10 -0700441 textAlpha.setDuration(mMaterialExpandDuration);
442 textAlpha.setStartDelay(mMaterialExpandStagger);
443 textAlpha.setInterpolator(new AccelerateInterpolator(1.5f));
444
445 Animator drift = LauncherAnimUtils.ofPropertyValuesHolder(this, tx, ty);
446 drift.setDuration(mMaterialExpandDuration);
447 drift.setStartDelay(mMaterialExpandStagger);
448 drift.setInterpolator(new LogDecelerateInterpolator(60, 0));
449
450 anim.play(drift);
451 anim.play(iconsAlpha);
452 anim.play(textAlpha);
453 anim.play(reveal);
454
455 openFolderAnim = anim;
456
Sunny Goyalbc753352015-03-05 09:40:44 -0800457 mContentWrapper.setLayerType(LAYER_TYPE_HARDWARE, null);
Adam Cohenc4fe9ea2014-08-18 18:54:10 -0700458 onCompleteRunnable = new Runnable() {
459 @Override
460 public void run() {
Sunny Goyalbc753352015-03-05 09:40:44 -0800461 mContentWrapper.setLayerType(LAYER_TYPE_NONE, null);
Adam Cohenc4fe9ea2014-08-18 18:54:10 -0700462 }
463 };
464 }
465 openFolderAnim.addListener(new AnimatorListenerAdapter() {
Adam Cohendf2cc412011-04-27 16:56:57 -0700466 @Override
467 public void onAnimationStart(Animator animation) {
Adam Cohen3371da02011-10-25 21:38:29 -0700468 sendCustomAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED,
Sunny Goyalbc753352015-03-05 09:40:44 -0800469 mContent.getAccessibilityDescription());
Adam Cohendf2cc412011-04-27 16:56:57 -0700470 mState = STATE_ANIMATING;
471 }
472 @Override
473 public void onAnimationEnd(Animator animation) {
Adam Cohen2801caf2011-05-13 20:57:39 -0700474 mState = STATE_OPEN;
Adam Cohenc4fe9ea2014-08-18 18:54:10 -0700475
476 if (onCompleteRunnable != null) {
477 onCompleteRunnable.run();
478 }
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800479
Sunny Goyalbc753352015-03-05 09:40:44 -0800480 mContent.setFocusOnFirstChild();
Adam Cohendf2cc412011-04-27 16:56:57 -0700481 }
482 });
Adam Cohenc4fe9ea2014-08-18 18:54:10 -0700483 openFolderAnim.start();
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800484
485 // Make sure the folder picks up the last drag move even if the finger doesn't move.
486 if (mDragController.isDragging()) {
487 mDragController.forceTouchMove();
488 }
489 }
490
491 public void beginExternalDrag(ShortcutInfo item) {
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800492 mCurrentDragInfo = item;
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800493 mEmptyCellRank = mContent.allocateNewLastItemRank();
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800494 mIsExternalDrag = true;
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800495 mDragInProgress = true;
Adam Cohendf2cc412011-04-27 16:56:57 -0700496 }
497
Adam Cohen3371da02011-10-25 21:38:29 -0700498 private void sendCustomAccessibilityEvent(int type, String text) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700499 AccessibilityManager accessibilityManager = (AccessibilityManager)
500 getContext().getSystemService(Context.ACCESSIBILITY_SERVICE);
501 if (accessibilityManager.isEnabled()) {
Adam Cohen3371da02011-10-25 21:38:29 -0700502 AccessibilityEvent event = AccessibilityEvent.obtain(type);
503 onInitializeAccessibilityEvent(event);
504 event.getText().add(text);
Michael Jurka8b805b12012-04-18 14:23:14 -0700505 accessibilityManager.sendAccessibilityEvent(event);
Adam Cohen3371da02011-10-25 21:38:29 -0700506 }
507 }
508
Adam Cohendf2cc412011-04-27 16:56:57 -0700509 public void animateClosed() {
Adam Cohen8e776a62011-06-28 18:10:06 -0700510 if (!(getParent() instanceof DragLayer)) return;
Adam Cohen662b5982011-12-13 17:45:21 -0800511 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0);
512 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 0.9f);
513 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 0.9f);
Michael Jurka032e6ba2013-04-22 15:08:42 +0200514 final ObjectAnimator oa =
Michael Jurka2ecf9952012-06-18 12:52:28 -0700515 LauncherAnimUtils.ofPropertyValuesHolder(this, alpha, scaleX, scaleY);
Adam Cohendf2cc412011-04-27 16:56:57 -0700516
Adam Cohen2801caf2011-05-13 20:57:39 -0700517 oa.addListener(new AnimatorListenerAdapter() {
Adam Cohendf2cc412011-04-27 16:56:57 -0700518 @Override
519 public void onAnimationEnd(Animator animation) {
Adam Cohen2801caf2011-05-13 20:57:39 -0700520 onCloseComplete();
Michael Jurka0121c3e2012-05-31 08:36:04 -0700521 setLayerType(LAYER_TYPE_NONE, null);
Adam Cohen2801caf2011-05-13 20:57:39 -0700522 mState = STATE_SMALL;
Adam Cohendf2cc412011-04-27 16:56:57 -0700523 }
524 @Override
525 public void onAnimationStart(Animator animation) {
Adam Cohen3371da02011-10-25 21:38:29 -0700526 sendCustomAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED,
Michael Jurka8b805b12012-04-18 14:23:14 -0700527 getContext().getString(R.string.folder_closed));
Adam Cohendf2cc412011-04-27 16:56:57 -0700528 mState = STATE_ANIMATING;
529 }
530 });
Adam Cohen2801caf2011-05-13 20:57:39 -0700531 oa.setDuration(mExpandDuration);
Michael Jurka0121c3e2012-05-31 08:36:04 -0700532 setLayerType(LAYER_TYPE_HARDWARE, null);
Michael Jurkaf1ad6082013-03-13 12:55:46 +0100533 oa.start();
Adam Cohendf2cc412011-04-27 16:56:57 -0700534 }
535
Adam Cohencb3382b2011-05-24 14:07:08 -0700536 public boolean acceptDrop(DragObject d) {
537 final ItemInfo item = (ItemInfo) d.dragInfo;
Adam Cohendf2cc412011-04-27 16:56:57 -0700538 final int itemType = item.itemType;
Adam Cohen2801caf2011-05-13 20:57:39 -0700539 return ((itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION ||
540 itemType == LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT) &&
541 !isFull());
Adam Cohendf2cc412011-04-27 16:56:57 -0700542 }
543
Adam Cohencb3382b2011-05-24 14:07:08 -0700544 public void onDragEnter(DragObject d) {
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800545 mPrevTargetRank = -1;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700546 mOnExitAlarm.cancelAlarm();
547 }
548
549 OnAlarmListener mReorderAlarmListener = new OnAlarmListener() {
550 public void onAlarm(Alarm alarm) {
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800551 mContent.realTimeReorder(mEmptyCellRank, mTargetRank);
552 mEmptyCellRank = mTargetRank;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700553 }
554 };
555
Sunny Goyalc46bfef2015-01-05 12:40:08 -0800556 @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
Adam Cohen2374abf2013-04-16 14:56:57 -0700557 public boolean isLayoutRtl() {
558 return (getLayoutDirection() == LAYOUT_DIRECTION_RTL);
559 }
560
Adam Cohencb3382b2011-05-24 14:07:08 -0700561 public void onDragOver(DragObject d) {
Sunny Goyal1587d532015-01-29 09:57:16 -0800562 final float[] r = d.getVisualCenter(null);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700563 r[0] -= getPaddingLeft();
564 r[1] -= getPaddingTop();
565
Sunny Goyalbc753352015-03-05 09:40:44 -0800566 mTargetRank = mContent.findNearestArea((int) r[0], (int) r[1]);
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800567 if (mTargetRank != mPrevTargetRank) {
Alan Viverette4cda5b72013-08-28 17:53:41 -0700568 mReorderAlarm.cancelAlarm();
Sunny Goyalc46bfef2015-01-05 12:40:08 -0800569 mReorderAlarm.setOnAlarmListener(mReorderAlarmListener);
570 mReorderAlarm.setAlarm(REORDER_DELAY);
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800571 mPrevTargetRank = mTargetRank;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700572 }
Adam Cohendf2cc412011-04-27 16:56:57 -0700573 }
574
Adam Cohenbfbfd262011-06-13 16:55:12 -0700575 OnAlarmListener mOnExitAlarmListener = new OnAlarmListener() {
576 public void onAlarm(Alarm alarm) {
Adam Cohen3e8f8112011-07-02 18:03:00 -0700577 completeDragExit();
Adam Cohenbfbfd262011-06-13 16:55:12 -0700578 }
579 };
580
Adam Cohen95bb8002011-07-03 23:40:28 -0700581 public void completeDragExit() {
Adam Cohen3e8f8112011-07-02 18:03:00 -0700582 mLauncher.closeFolder();
583 mCurrentDragInfo = null;
584 mCurrentDragView = null;
585 mSuppressOnAdd = false;
586 mRearrangeOnClose = true;
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800587 mIsExternalDrag = false;
Adam Cohen3e8f8112011-07-02 18:03:00 -0700588 }
589
Adam Cohencb3382b2011-05-24 14:07:08 -0700590 public void onDragExit(DragObject d) {
Mindy DelliCarpini53b8d072013-07-03 08:23:13 -0700591 // We only close the folder if this is a true drag exit, ie. not because
592 // a drop has occurred above the folder.
Adam Cohenbfbfd262011-06-13 16:55:12 -0700593 if (!d.dragComplete) {
594 mOnExitAlarm.setOnAlarmListener(mOnExitAlarmListener);
595 mOnExitAlarm.setAlarm(ON_EXIT_CLOSE_DELAY);
596 }
597 mReorderAlarm.cancelAlarm();
Adam Cohen2801caf2011-05-13 20:57:39 -0700598 }
599
Michael Jurka1e2f4652013-07-08 18:03:46 -0700600 public void onDropCompleted(final View target, final DragObject d,
601 final boolean isFlingToDelete, final boolean success) {
602 if (mDeferDropAfterUninstall) {
Michael Jurkaf3007582013-08-21 14:33:57 +0200603 Log.d(TAG, "Deferred handling drop because waiting for uninstall.");
Michael Jurka1e2f4652013-07-08 18:03:46 -0700604 mDeferredAction = new Runnable() {
605 public void run() {
606 onDropCompleted(target, d, isFlingToDelete, success);
607 mDeferredAction = null;
608 }
609 };
610 return;
611 }
612
613 boolean beingCalledAfterUninstall = mDeferredAction != null;
614 boolean successfulDrop =
615 success && (!beingCalledAfterUninstall || mUninstallSuccessful);
Winson Chung5f8afe62013-08-12 16:19:28 -0700616
Michael Jurka1e2f4652013-07-08 18:03:46 -0700617 if (successfulDrop) {
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800618 if (mDeleteFolderOnDropCompleted && !mItemAddedBackToSelfViaIcon && target != this) {
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700619 replaceFolderWithFinalItem();
620 }
621 } else {
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800622 rearrangeChildren();
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700623 // The drag failed, we need to return the item to the folder
624 mFolderIcon.onDrop(d);
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700625 }
626
627 if (target != this) {
628 if (mOnExitAlarm.alarmPending()) {
629 mOnExitAlarm.cancelAlarm();
Michael Jurka54554252013-08-01 12:52:23 +0200630 if (!successfulDrop) {
Adam Cohen7a8b82b2013-05-29 18:41:50 -0700631 mSuppressFolderDeletion = true;
632 }
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700633 completeDragExit();
634 }
Adam Cohen9c58d822013-12-13 17:18:10 -0800635 }
636
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700637 mDeleteFolderOnDropCompleted = false;
638 mDragInProgress = false;
Adam Cohen05e0f402011-08-01 12:12:49 -0700639 mItemAddedBackToSelfViaIcon = false;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700640 mCurrentDragInfo = null;
641 mCurrentDragView = null;
642 mSuppressOnAdd = false;
Adam Cohen4045eb72011-10-06 11:44:26 -0700643
644 // Reordering may have occured, and we need to save the new item locations. We do this once
645 // at the end to prevent unnecessary database operations.
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700646 updateItemLocationsInDatabaseBatch();
Adam Cohen4045eb72011-10-06 11:44:26 -0700647 }
648
Michael Jurka1e2f4652013-07-08 18:03:46 -0700649 public void deferCompleteDropAfterUninstallActivity() {
650 mDeferDropAfterUninstall = true;
651 }
652
653 public void onUninstallActivityReturned(boolean success) {
654 mDeferDropAfterUninstall = false;
655 mUninstallSuccessful = success;
656 if (mDeferredAction != null) {
657 mDeferredAction.run();
658 }
659 }
660
Winson Chunga48487a2012-03-20 16:19:37 -0700661 @Override
Winson Chungeeb5bbc2013-11-13 15:47:05 -0800662 public float getIntrinsicIconScaleFactor() {
663 return 1f;
664 }
665
666 @Override
Winson Chung043f2af2012-03-01 16:09:54 -0800667 public boolean supportsFlingToDelete() {
668 return true;
669 }
670
Mathew Inwood1eeb3fc2013-11-25 17:01:34 +0000671 @Override
672 public boolean supportsAppInfoDropTarget() {
673 return false;
674 }
675
676 @Override
677 public boolean supportsDeleteDropTarget() {
678 return true;
679 }
680
Winson Chunga48487a2012-03-20 16:19:37 -0700681 public void onFlingToDelete(DragObject d, int x, int y, PointF vec) {
682 // Do nothing
683 }
684
685 @Override
686 public void onFlingToDeleteCompleted() {
687 // Do nothing
688 }
689
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700690 private void updateItemLocationsInDatabaseBatch() {
691 ArrayList<View> list = getItemsInReadingOrder();
692 ArrayList<ItemInfo> items = new ArrayList<ItemInfo>();
693 for (int i = 0; i < list.size(); i++) {
694 View v = list.get(i);
695 ItemInfo info = (ItemInfo) v.getTag();
Sunny Goyal08f72612015-01-05 13:41:43 -0800696 info.rank = i;
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700697 items.add(info);
698 }
699
700 LauncherModel.moveItemsInDatabase(mLauncher, items, mInfo.id, 0);
701 }
702
Adam Cohene25af792013-06-06 23:08:25 -0700703 public void addItemLocationsInDatabase() {
704 ArrayList<View> list = getItemsInReadingOrder();
705 for (int i = 0; i < list.size(); i++) {
706 View v = list.get(i);
707 ItemInfo info = (ItemInfo) v.getTag();
708 LauncherModel.addItemToDatabase(mLauncher, info, mInfo.id, 0,
709 info.cellX, info.cellY, false);
710 }
711 }
712
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700713 public void notifyDrop() {
714 if (mDragInProgress) {
Adam Cohen05e0f402011-08-01 12:12:49 -0700715 mItemAddedBackToSelfViaIcon = true;
Adam Cohen76078c42011-06-09 15:06:52 -0700716 }
Adam Cohendf2cc412011-04-27 16:56:57 -0700717 }
718
719 public boolean isDropEnabled() {
720 return true;
721 }
722
Adam Cohen2801caf2011-05-13 20:57:39 -0700723 public boolean isFull() {
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800724 return mContent.isFull();
Adam Cohen2801caf2011-05-13 20:57:39 -0700725 }
726
727 private void centerAboutIcon() {
Adam Cohen8e776a62011-06-28 18:10:06 -0700728 DragLayer.LayoutParams lp = (DragLayer.LayoutParams) getLayoutParams();
Adam Cohen2801caf2011-05-13 20:57:39 -0700729
Winson Chung892c74d2013-08-22 16:15:50 -0700730 DragLayer parent = (DragLayer) mLauncher.findViewById(R.id.drag_layer);
Adam Cohen2801caf2011-05-13 20:57:39 -0700731 int width = getPaddingLeft() + getPaddingRight() + mContent.getDesiredWidth();
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700732 int height = getFolderHeight();
Adam Cohen2801caf2011-05-13 20:57:39 -0700733
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800734 float scale = parent.getDescendantRectRelativeToSelf(mFolderIcon, sTempRect);
Adam Cohen8e776a62011-06-28 18:10:06 -0700735
Winson Chungaf40f202013-09-18 18:26:31 -0700736 LauncherAppState app = LauncherAppState.getInstance();
737 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
738
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800739 int centerX = (int) (sTempRect.left + sTempRect.width() * scale / 2);
740 int centerY = (int) (sTempRect.top + sTempRect.height() * scale / 2);
Adam Cohen2801caf2011-05-13 20:57:39 -0700741 int centeredLeft = centerX - width / 2;
742 int centeredTop = centerY - height / 2;
Winson Chung3057b1c2013-10-10 17:35:35 -0700743 int currentPage = mLauncher.getWorkspace().getNextPage();
Adam Cohen7cc1bc42011-12-13 21:28:43 -0800744 // In case the workspace is scrolling, we need to use the final scroll to compute
745 // the folders bounds.
746 mLauncher.getWorkspace().setFinalScrollForPageChange(currentPage);
Adam Cohen35e7e642011-07-17 14:47:18 -0700747 // We first fetch the currently visible CellLayoutChildren
Adam Cohen7cc1bc42011-12-13 21:28:43 -0800748 CellLayout currentLayout = (CellLayout) mLauncher.getWorkspace().getChildAt(currentPage);
Michael Jurkaa52570f2012-03-20 03:18:20 -0700749 ShortcutAndWidgetContainer boundingLayout = currentLayout.getShortcutsAndWidgets();
Adam Cohen35e7e642011-07-17 14:47:18 -0700750 Rect bounds = new Rect();
751 parent.getDescendantRectRelativeToSelf(boundingLayout, bounds);
Adam Cohen7cc1bc42011-12-13 21:28:43 -0800752 // We reset the workspaces scroll
753 mLauncher.getWorkspace().resetFinalScrollForPageChange(currentPage);
Adam Cohen2801caf2011-05-13 20:57:39 -0700754
Adam Cohen35e7e642011-07-17 14:47:18 -0700755 // We need to bound the folder to the currently visible CellLayoutChildren
756 int left = Math.min(Math.max(bounds.left, centeredLeft),
757 bounds.left + bounds.width() - width);
758 int top = Math.min(Math.max(bounds.top, centeredTop),
759 bounds.top + bounds.height() - height);
Winson Chungaf40f202013-09-18 18:26:31 -0700760 if (grid.isPhone() && (grid.availableWidthPx - width) < grid.iconSizePx) {
761 // Center the folder if it is full (on phones only)
762 left = (grid.availableWidthPx - width) / 2;
763 } else if (width >= bounds.width()) {
764 // If the folder doesn't fit within the bounds, center it about the desired bounds
Adam Cohen35e7e642011-07-17 14:47:18 -0700765 left = bounds.left + (bounds.width() - width) / 2;
Adam Cohen0e4857c2011-06-30 17:05:14 -0700766 }
Adam Cohen35e7e642011-07-17 14:47:18 -0700767 if (height >= bounds.height()) {
768 top = bounds.top + (bounds.height() - height) / 2;
Adam Cohen0e4857c2011-06-30 17:05:14 -0700769 }
Adam Cohen2801caf2011-05-13 20:57:39 -0700770
771 int folderPivotX = width / 2 + (centeredLeft - left);
772 int folderPivotY = height / 2 + (centeredTop - top);
773 setPivotX(folderPivotX);
774 setPivotY(folderPivotY);
Adam Cohen268c4752012-06-06 17:47:33 -0700775 mFolderIconPivotX = (int) (mFolderIcon.getMeasuredWidth() *
Adam Cohen2801caf2011-05-13 20:57:39 -0700776 (1.0f * folderPivotX / width));
Adam Cohen268c4752012-06-06 17:47:33 -0700777 mFolderIconPivotY = (int) (mFolderIcon.getMeasuredHeight() *
Adam Cohen2801caf2011-05-13 20:57:39 -0700778 (1.0f * folderPivotY / height));
Adam Cohen3bf84d32012-05-07 20:17:14 -0700779
Adam Cohen662b5982011-12-13 17:45:21 -0800780 lp.width = width;
781 lp.height = height;
782 lp.x = left;
783 lp.y = top;
Adam Cohen2801caf2011-05-13 20:57:39 -0700784 }
785
Adam Cohen268c4752012-06-06 17:47:33 -0700786 float getPivotXForIconAnimation() {
787 return mFolderIconPivotX;
788 }
789 float getPivotYForIconAnimation() {
790 return mFolderIconPivotY;
791 }
792
Winson Chung892c74d2013-08-22 16:15:50 -0700793 private int getContentAreaHeight() {
794 LauncherAppState app = LauncherAppState.getInstance();
795 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
796 Rect workspacePadding = grid.getWorkspacePadding(grid.isLandscape ?
797 CellLayout.LANDSCAPE : CellLayout.PORTRAIT);
798 int maxContentAreaHeight = grid.availableHeightPx -
Winson Chung892c74d2013-08-22 16:15:50 -0700799 workspacePadding.top - workspacePadding.bottom -
Sunny Goyal290800b2015-03-05 11:33:33 -0800800 mFooterHeight;
Adam Cohen1960ea42013-11-12 11:33:14 +0000801 int height = Math.min(maxContentAreaHeight,
Winson Chung892c74d2013-08-22 16:15:50 -0700802 mContent.getDesiredHeight());
Adam Cohen1960ea42013-11-12 11:33:14 +0000803 return Math.max(height, MIN_CONTENT_DIMEN);
804 }
805
806 private int getContentAreaWidth() {
807 return Math.max(mContent.getDesiredWidth(), MIN_CONTENT_DIMEN);
Winson Chung892c74d2013-08-22 16:15:50 -0700808 }
809
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700810 private int getFolderHeight() {
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800811 return getFolderHeight(getContentAreaHeight());
812 }
813
814 private int getFolderHeight(int contentAreaHeight) {
Sunny Goyal290800b2015-03-05 11:33:33 -0800815 return getPaddingTop() + getPaddingBottom() + contentAreaHeight + mFooterHeight;
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700816 }
Adam Cohen234c4cd2011-07-17 21:03:04 -0700817
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700818 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800819 int contentWidth = getContentAreaWidth();
820 int contentHeight = getContentAreaHeight();
Adam Cohen1960ea42013-11-12 11:33:14 +0000821
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800822 int contentAreaWidthSpec = MeasureSpec.makeMeasureSpec(contentWidth, MeasureSpec.EXACTLY);
823 int contentAreaHeightSpec = MeasureSpec.makeMeasureSpec(contentHeight, MeasureSpec.EXACTLY);
824
825 mContent.setFixedSize(contentWidth, contentHeight);
826 mContentWrapper.measure(contentAreaWidthSpec, contentAreaHeightSpec);
Sunny Goyal290800b2015-03-05 11:33:33 -0800827 mFooter.measure(contentAreaWidthSpec,
828 MeasureSpec.makeMeasureSpec(mFooterHeight, MeasureSpec.EXACTLY));
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800829
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
Sunny Goyalc4918352015-03-10 18:15:48 -0700853 // TODO remove this once GSA code fix is submitted
854 public ViewGroup getContent() {
855 return (ViewGroup) mContent;
856 }
857
Adam Cohena9cf38f2011-05-02 15:36:58 -0700858 public int getItemCount() {
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800859 return mContent.getItemCount();
Adam Cohena9cf38f2011-05-02 15:36:58 -0700860 }
861
Adam Cohen2801caf2011-05-13 20:57:39 -0700862 private void onCloseComplete() {
Adam Cohen05e0f402011-08-01 12:12:49 -0700863 DragLayer parent = (DragLayer) getParent();
Michael Jurka5649c282012-06-18 10:33:21 -0700864 if (parent != null) {
865 parent.removeView(this);
866 }
Adam Cohen4554ee12011-08-03 16:13:21 -0700867 mDragController.removeDropTarget((DropTarget) this);
Adam Cohen05e0f402011-08-01 12:12:49 -0700868 clearFocus();
Adam Cohenac56cff2011-09-28 20:45:37 -0700869 mFolderIcon.requestFocus();
Adam Cohen05e0f402011-08-01 12:12:49 -0700870
Adam Cohen2801caf2011-05-13 20:57:39 -0700871 if (mRearrangeOnClose) {
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800872 rearrangeChildren();
Adam Cohen2801caf2011-05-13 20:57:39 -0700873 mRearrangeOnClose = false;
874 }
Adam Cohenafb01ee2011-06-23 15:38:03 -0700875 if (getItemCount() <= 1) {
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700876 if (!mDragInProgress && !mSuppressFolderDeletion) {
877 replaceFolderWithFinalItem();
878 } else if (mDragInProgress) {
879 mDeleteFolderOnDropCompleted = true;
880 }
Adam Cohenafb01ee2011-06-23 15:38:03 -0700881 }
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700882 mSuppressFolderDeletion = false;
Adam Cohenafb01ee2011-06-23 15:38:03 -0700883 }
884
885 private void replaceFolderWithFinalItem() {
Adam Cohenafb01ee2011-06-23 15:38:03 -0700886 // Add the last remaining child to the workspace in place of the folder
Adam Cohenfb91f302012-06-11 15:45:18 -0700887 Runnable onCompleteRunnable = new Runnable() {
888 @Override
889 public void run() {
Adam Cohendcd297f2013-06-18 13:13:40 -0700890 CellLayout cellLayout = mLauncher.getCellLayout(mInfo.container, mInfo.screenId);
Adam Cohenafb01ee2011-06-23 15:38:03 -0700891
Winson Chung33231f52013-12-09 16:57:45 -0800892 View child = null;
Adam Cohenfb91f302012-06-11 15:45:18 -0700893 // Move the item from the folder to the workspace, in the position of the folder
894 if (getItemCount() == 1) {
895 ShortcutInfo finalItem = mInfo.contents.get(0);
Adam Cohenc5e63f32012-07-12 16:16:57 -0700896 child = mLauncher.createShortcut(R.layout.application, cellLayout,
Adam Cohenfb91f302012-06-11 15:45:18 -0700897 finalItem);
898 LauncherModel.addOrMoveItemInDatabase(mLauncher, finalItem, mInfo.container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700899 mInfo.screenId, mInfo.cellX, mInfo.cellY);
Adam Cohenfb91f302012-06-11 15:45:18 -0700900 }
Adam Cohen487f7dd2012-06-28 18:12:10 -0700901 if (getItemCount() <= 1) {
902 // Remove the folder
903 LauncherModel.deleteItemFromDatabase(mLauncher, mInfo);
Dan Sandler0eb687f2014-01-10 13:24:55 -0500904 if (cellLayout != null) {
905 // b/12446428 -- sometimes the cell layout has already gone away?
906 cellLayout.removeView(mFolderIcon);
907 }
Adam Cohen487f7dd2012-06-28 18:12:10 -0700908 if (mFolderIcon instanceof DropTarget) {
909 mDragController.removeDropTarget((DropTarget) mFolderIcon);
910 }
911 mLauncher.removeFolder(mInfo);
912 }
Adam Cohenc5e63f32012-07-12 16:16:57 -0700913 // We add the child after removing the folder to prevent both from existing at
Winson Chung0e6a7132013-08-23 12:55:10 -0700914 // the same time in the CellLayout. We need to add the new item with addInScreenFromBind()
915 // to ensure that hotseat items are placed correctly.
Adam Cohenc5e63f32012-07-12 16:16:57 -0700916 if (child != null) {
Winson Chung0e6a7132013-08-23 12:55:10 -0700917 mLauncher.getWorkspace().addInScreenFromBind(child, mInfo.container, mInfo.screenId,
Adam Cohenc5e63f32012-07-12 16:16:57 -0700918 mInfo.cellX, mInfo.cellY, mInfo.spanX, mInfo.spanY);
919 }
Adam Cohenfb91f302012-06-11 15:45:18 -0700920 }
921 };
Sunny Goyalbc753352015-03-05 09:40:44 -0800922 View finalChild = mContent.getLastItem();
Adam Cohenfb91f302012-06-11 15:45:18 -0700923 if (finalChild != null) {
924 mFolderIcon.performDestroyAnimation(finalChild, onCompleteRunnable);
Winson Chung33231f52013-12-09 16:57:45 -0800925 } else {
926 onCompleteRunnable.run();
Adam Cohenafb01ee2011-06-23 15:38:03 -0700927 }
Adam Cohenfb91f302012-06-11 15:45:18 -0700928 mDestroyed = true;
929 }
930
931 boolean isDestroyed() {
932 return mDestroyed;
Adam Cohen2801caf2011-05-13 20:57:39 -0700933 }
934
Adam Cohenac56cff2011-09-28 20:45:37 -0700935 // This method keeps track of the last item in the folder for the purposes
936 // of keyboard focus
Sunny Goyal290800b2015-03-05 11:33:33 -0800937 public void updateTextViewFocus() {
Sunny Goyalbc753352015-03-05 09:40:44 -0800938 View lastChild = mContent.getLastItem();
Adam Cohenac56cff2011-09-28 20:45:37 -0700939 if (lastChild != null) {
940 mFolderName.setNextFocusDownId(lastChild.getId());
941 mFolderName.setNextFocusRightId(lastChild.getId());
942 mFolderName.setNextFocusLeftId(lastChild.getId());
943 mFolderName.setNextFocusUpId(lastChild.getId());
944 }
945 }
946
Adam Cohenbfbfd262011-06-13 16:55:12 -0700947 public void onDrop(DragObject d) {
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800948 Runnable cleanUpRunnable = null;
949
Adam Cohen689ff162014-05-08 17:27:56 -0700950 // If we are coming from All Apps space, we defer removing the extra empty screen
951 // until the folder closes
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800952 if (d.dragSource != mLauncher.getWorkspace() && !(d.dragSource instanceof Folder)) {
953 cleanUpRunnable = new Runnable() {
954 @Override
955 public void run() {
Adam Cohen689ff162014-05-08 17:27:56 -0700956 mLauncher.exitSpringLoadedDragModeDelayed(true,
957 Launcher.EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT,
958 null);
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800959 }
960 };
Adam Cohenbfbfd262011-06-13 16:55:12 -0700961 }
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800962
963 View currentDragView;
964 ShortcutInfo si = mCurrentDragInfo;
965 if (mIsExternalDrag) {
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800966 currentDragView = mContent.createAndAddViewForRank(si, mEmptyCellRank);
Sunny Goyal95abbb32014-08-04 10:53:22 -0700967 // Actually move the item in the database if it was an external drag. Call this
968 // before creating the view, so that ShortcutInfo is updated appropriately.
969 LauncherModel.addOrMoveItemInDatabase(
970 mLauncher, si, mInfo.id, 0, si.cellX, si.cellY);
971
972 // We only need to update the locations if it doesn't get handled in #onDropCompleted.
973 if (d.dragSource != this) {
974 updateItemLocationsInDatabaseBatch();
975 }
976 mIsExternalDrag = false;
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800977 } else {
978 currentDragView = mCurrentDragView;
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800979 mContent.addViewForRank(currentDragView, si, mEmptyCellRank);
Adam Cohenbfbfd262011-06-13 16:55:12 -0700980 }
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800981
982 if (d.dragView.hasDrawn()) {
983
984 // Temporarily reset the scale such that the animation target gets calculated correctly.
985 float scaleX = getScaleX();
986 float scaleY = getScaleY();
987 setScaleX(1.0f);
988 setScaleY(1.0f);
989 mLauncher.getDragLayer().animateViewIntoPosition(d.dragView, currentDragView,
990 cleanUpRunnable, null);
991 setScaleX(scaleX);
992 setScaleY(scaleY);
993 } else {
994 d.deferDragViewCleanupPostAnimation = false;
995 currentDragView.setVisibility(VISIBLE);
996 }
997 mItemsInvalidated = true;
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800998 rearrangeChildren();
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800999
Jorim Jaggi55bd9722014-01-16 15:30:42 -08001000 // Temporarily suppress the listener, as we did all the work already here.
1001 mSuppressOnAdd = true;
1002 mInfo.add(si);
1003 mSuppressOnAdd = false;
Sunny Goyal4b020172014-08-28 14:51:14 -07001004 // Clear the drag info, as it is no longer being dragged.
1005 mCurrentDragInfo = null;
Adam Cohenbfbfd262011-06-13 16:55:12 -07001006 }
1007
Adam Cohen7a8b82b2013-05-29 18:41:50 -07001008 // This is used so the item doesn't immediately appear in the folder when added. In one case
1009 // we need to create the illusion that the item isn't added back to the folder yet, to
1010 // to correspond to the animation of the icon back into the folder. This is
1011 public void hideItem(ShortcutInfo info) {
1012 View v = getViewForInfo(info);
1013 v.setVisibility(INVISIBLE);
1014 }
1015 public void showItem(ShortcutInfo info) {
1016 View v = getViewForInfo(info);
1017 v.setVisibility(VISIBLE);
1018 }
1019
Adam Cohenbfbfd262011-06-13 16:55:12 -07001020 public void onAdd(ShortcutInfo item) {
1021 mItemsInvalidated = true;
Adam Cohen05e0f402011-08-01 12:12:49 -07001022 // If the item was dropped onto this open folder, we have done the work associated
1023 // with adding the item to the folder, as indicated by mSuppressOnAdd being set
Adam Cohenbfbfd262011-06-13 16:55:12 -07001024 if (mSuppressOnAdd) return;
Sunny Goyalbc753352015-03-05 09:40:44 -08001025 mContent.createAndAddViewForRank(item, mContent.allocateNewLastItemRank());
Adam Cohenbfbfd262011-06-13 16:55:12 -07001026 LauncherModel.addOrMoveItemInDatabase(
1027 mLauncher, item, mInfo.id, 0, item.cellX, item.cellY);
1028 }
1029
Adam Cohena9cf38f2011-05-02 15:36:58 -07001030 public void onRemove(ShortcutInfo item) {
Adam Cohen7c693212011-05-18 15:26:57 -07001031 mItemsInvalidated = true;
Adam Cohen05e0f402011-08-01 12:12:49 -07001032 // If this item is being dragged from this open folder, we have already handled
1033 // the work associated with removing the item, so we don't have to do anything here.
Adam Cohenbfbfd262011-06-13 16:55:12 -07001034 if (item == mCurrentDragInfo) return;
Adam Cohendf1e4e82011-06-24 15:57:39 -07001035 View v = getViewForInfo(item);
Sunny Goyal290800b2015-03-05 11:33:33 -08001036 mContent.removeItem(v);
Adam Cohen2801caf2011-05-13 20:57:39 -07001037 if (mState == STATE_ANIMATING) {
1038 mRearrangeOnClose = true;
1039 } else {
Sunny Goyalc3a609f2015-02-26 17:43:50 -08001040 rearrangeChildren();
Adam Cohen2801caf2011-05-13 20:57:39 -07001041 }
Adam Cohenafb01ee2011-06-23 15:38:03 -07001042 if (getItemCount() <= 1) {
1043 replaceFolderWithFinalItem();
1044 }
Adam Cohena9cf38f2011-05-02 15:36:58 -07001045 }
Adam Cohen7c693212011-05-18 15:26:57 -07001046
Sunny Goyalbc753352015-03-05 09:40:44 -08001047 private View getViewForInfo(final ShortcutInfo item) {
1048 return mContent.iterateOverItems(new ItemOperator() {
1049
1050 @Override
1051 public boolean evaluate(ItemInfo info, View view, View parent) {
1052 return info == item;
Adam Cohendf1e4e82011-06-24 15:57:39 -07001053 }
Sunny Goyalbc753352015-03-05 09:40:44 -08001054 });
Adam Cohendf1e4e82011-06-24 15:57:39 -07001055 }
1056
Adam Cohen76078c42011-06-09 15:06:52 -07001057 public void onItemsChanged() {
Adam Cohenac56cff2011-09-28 20:45:37 -07001058 updateTextViewFocus();
Adam Cohen76078c42011-06-09 15:06:52 -07001059 }
Adam Cohenac56cff2011-09-28 20:45:37 -07001060
Adam Cohen76fc0852011-06-17 13:26:23 -07001061 public void onTitleChanged(CharSequence title) {
1062 }
Adam Cohen76078c42011-06-09 15:06:52 -07001063
Adam Cohen7c693212011-05-18 15:26:57 -07001064 public ArrayList<View> getItemsInReadingOrder() {
1065 if (mItemsInvalidated) {
1066 mItemsInReadingOrder.clear();
Sunny Goyalbc753352015-03-05 09:40:44 -08001067 mContent.iterateOverItems(new ItemOperator() {
1068
1069 @Override
1070 public boolean evaluate(ItemInfo info, View view, View parent) {
1071 mItemsInReadingOrder.add(view);
1072 return false;
Adam Cohen7c693212011-05-18 15:26:57 -07001073 }
Sunny Goyalbc753352015-03-05 09:40:44 -08001074 });
Adam Cohen7c693212011-05-18 15:26:57 -07001075 mItemsInvalidated = false;
1076 }
1077 return mItemsInReadingOrder;
1078 }
Adam Cohen8dfcba42011-07-07 16:38:18 -07001079
1080 public void getLocationInDragLayer(int[] loc) {
1081 mLauncher.getDragLayer().getLocationInDragLayer(this, loc);
1082 }
Adam Cohenea0818d2011-09-30 20:06:58 -07001083
1084 public void onFocusChange(View v, boolean hasFocus) {
1085 if (v == mFolderName && hasFocus) {
1086 startEditingFolderName();
1087 }
1088 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001089
1090 @Override
1091 public void getHitRectRelativeToDragLayer(Rect outRect) {
1092 getHitRect(outRect);
1093 }
Sunny Goyalbc753352015-03-05 09:40:44 -08001094
1095 public static interface FolderContent {
1096 void setFolder(Folder f);
1097
Sunny Goyal290800b2015-03-05 11:33:33 -08001098 void removeItem(View v);
Sunny Goyalbc753352015-03-05 09:40:44 -08001099
1100 boolean isFull();
1101 int getItemCount();
1102
1103 int getDesiredWidth();
1104 int getDesiredHeight();
1105 void setFixedSize(int width, int height);
1106
1107 /**
1108 * Iterates over all its items in a reading order.
1109 * @return the view for which the operator returned true.
1110 */
1111 View iterateOverItems(ItemOperator op);
1112 View getLastItem();
1113
1114 String getAccessibilityDescription();
1115
1116 /**
1117 * Binds items to the layout.
1118 * @return list of items that could not be bound, probably because we hit the max size limit.
1119 */
1120 ArrayList<ShortcutInfo> bindItems(ArrayList<ShortcutInfo> children);
1121
1122 /**
1123 * Create space for a new item at the end, and returns the rank for that item.
1124 * Resizes the content if necessary.
1125 */
1126 int allocateNewLastItemRank();
1127
1128 View createAndAddViewForRank(ShortcutInfo item, int rank);
1129
1130 /**
1131 * Adds the {@param view} to the layout based on {@param rank} and updated the position
1132 * related attributes. It assumes that {@param item} is already attached to the view.
1133 */
1134 void addViewForRank(View view, ShortcutInfo item, int rank);
1135
1136 /**
1137 * Reorders the items such that the {@param empty} spot moves to {@param target}
1138 */
1139 void realTimeReorder(int empty, int target);
1140
1141 /**
1142 * @return the rank of the cell nearest to the provided pixel position.
1143 */
1144 int findNearestArea(int pixelX, int pixelY);
1145
1146 /**
1147 * Updates position and rank of all the children in the view based.
1148 * @param list the ordered list of children.
1149 * @param itemCount if greater than the total children count, empty spaces are left
1150 * at the end.
1151 */
1152 void arrangeChildren(ArrayList<View> list, int itemCount);
1153
1154 /**
1155 * Sets the focus on the first visible child.
1156 */
1157 void setFocusOnFirstChild();
1158 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001159}