blob: 80fc32880ecae77798a51739acde5617261844c5 [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;
Romain Guyedcce092010-03-04 13:03:17 -080051
Adam Cohenc0dcf592011-06-01 15:30:43 -070052import java.util.ArrayList;
Adam Cohen3bf84d32012-05-07 20:17:14 -070053import java.util.Collections;
Adam Cohenc0dcf592011-06-01 15:30:43 -070054
The Android Open Source Project31dd5032009-03-03 19:32:27 -080055/**
56 * Represents a set of icons chosen by the user or generated by the system.
57 */
Adam Cohen8dfcba42011-07-07 16:38:18 -070058public class Folder extends LinearLayout implements DragSource, View.OnClickListener,
Adam Cohenea0818d2011-09-30 20:06:58 -070059 View.OnLongClickListener, DropTarget, FolderListener, TextView.OnEditorActionListener,
60 View.OnFocusChangeListener {
Adam Cohendf2cc412011-04-27 16:56:57 -070061 private static final String TAG = "Launcher.Folder";
62
Sunny Goyalc3a609f2015-02-26 17:43:50 -080063 /**
64 * We avoid measuring {@link #mContentWrapper} with a 0 width or height, as this
65 * results in CellLayout being measured as UNSPECIFIED, which it does not support.
66 */
67 private static final int MIN_CONTENT_DIMEN = 5;
Adam Cohen4eac29a2011-07-11 17:53:37 -070068
Adam Cohendf2cc412011-04-27 16:56:57 -070069 static final int STATE_NONE = -1;
70 static final int STATE_SMALL = 0;
71 static final int STATE_ANIMATING = 1;
72 static final int STATE_OPEN = 2;
73
Adam Cohenf0f4eda2013-06-06 21:27:03 -070074 private static final int REORDER_DELAY = 250;
Adam Cohen5d518fa2013-12-05 14:16:23 -080075 private static final int ON_EXIT_CLOSE_DELAY = 400;
Sunny Goyalc3a609f2015-02-26 17:43:50 -080076 private static final Rect sTempRect = new Rect();
77
78 private static String sDefaultFolderName;
79 private static String sHintText;
80
81 private final Alarm mReorderAlarm = new Alarm();
82 private final Alarm mOnExitAlarm = new Alarm();
83
84 private final ArrayList<View> mItemsInReadingOrder = new ArrayList<View>();
85
86 private final int mExpandDuration;
87 private final int mMaterialExpandDuration;
88 private final int mMaterialExpandStagger;
89
90 private final InputMethodManager mInputMethodManager;
91
92 protected final Launcher mLauncher;
93 protected DragController mDragController;
94 protected FolderInfo mInfo;
95
Adam Cohen2801caf2011-05-13 20:57:39 -070096 private FolderIcon mFolderIcon;
Sunny Goyalc3a609f2015-02-26 17:43:50 -080097
98 private FolderCellLayout mContent;
99 private View mContentWrapper;
100 FolderEditText mFolderName;
101
102 private View mBottomContent;
103 private int mBottomContentHeight;
104
105 // Cell ranks used for drag and drop
106 private int mTargetRank, mPrevTargetRank, mEmptyCellRank;
107
108 private int mState = STATE_NONE;
109 private boolean mRearrangeOnClose = false;
Adam Cohen7c693212011-05-18 15:26:57 -0700110 boolean mItemsInvalidated = false;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700111 private ShortcutInfo mCurrentDragInfo;
112 private View mCurrentDragView;
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800113 private boolean mIsExternalDrag;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700114 boolean mSuppressOnAdd = false;
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700115 private boolean mDragInProgress = false;
116 private boolean mDeleteFolderOnDropCompleted = false;
117 private boolean mSuppressFolderDeletion = false;
Adam Cohen05e0f402011-08-01 12:12:49 -0700118 private boolean mItemAddedBackToSelfViaIcon = false;
Adam Cohen268c4752012-06-06 17:47:33 -0700119 private float mFolderIconPivotX;
120 private float mFolderIconPivotY;
Adam Cohen76fc0852011-06-17 13:26:23 -0700121 private boolean mIsEditingName = false;
Adam Cohen1960ea42013-11-12 11:33:14 +0000122
Adam Cohenfb91f302012-06-11 15:45:18 -0700123 private boolean mDestroyed;
124
Michael Jurka1e2f4652013-07-08 18:03:46 -0700125 private Runnable mDeferredAction;
126 private boolean mDeferDropAfterUninstall;
127 private boolean mUninstallSuccessful;
128
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800129 /**
130 * Used to inflate the Workspace from XML.
131 *
132 * @param context The application's context.
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800133 * @param attrs The attributes set containing the Workspace's customization values.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800134 */
135 public Folder(Context context, AttributeSet attrs) {
136 super(context, attrs);
137 setAlwaysDrawnWithCacheEnabled(false);
Adam Cohen76fc0852011-06-17 13:26:23 -0700138 mInputMethodManager = (InputMethodManager)
Michael Jurka8b805b12012-04-18 14:23:14 -0700139 getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
Adam Cohen76fc0852011-06-17 13:26:23 -0700140
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800141 Resources res = getResources();
Adam Cohenc4fe9ea2014-08-18 18:54:10 -0700142 mExpandDuration = res.getInteger(R.integer.config_folderExpandDuration);
143 mMaterialExpandDuration = res.getInteger(R.integer.config_materialFolderExpandDuration);
144 mMaterialExpandStagger = res.getInteger(R.integer.config_materialFolderExpandStagger);
Adam Cohen4ef610f2011-06-21 22:37:36 -0700145
146 if (sDefaultFolderName == null) {
147 sDefaultFolderName = res.getString(R.string.folder_name);
148 }
Adam Cohena65beee2011-06-27 21:32:23 -0700149 if (sHintText == null) {
150 sHintText = res.getString(R.string.folder_hint_text);
151 }
Adam Cohen4eac29a2011-07-11 17:53:37 -0700152 mLauncher = (Launcher) context;
Adam Cohenac56cff2011-09-28 20:45:37 -0700153 // We need this view to be focusable in touch mode so that when text editing of the folder
154 // name is complete, we have something to focus on, thus hiding the cursor and giving
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800155 // reliable behavior when clicking the text field (since it will always gain focus on click).
Adam Cohenac56cff2011-09-28 20:45:37 -0700156 setFocusableInTouchMode(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800157 }
158
159 @Override
160 protected void onFinishInflate() {
161 super.onFinishInflate();
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800162 mContentWrapper = findViewById(R.id.folder_content_wrapper);
163 mContent = (FolderCellLayout) findViewById(R.id.folder_content);
164 mContent.setFolder(this);
Sunny Goyaldcbcc862014-08-12 15:58:36 -0700165
Winson Chung5f8afe62013-08-12 16:19:28 -0700166 LauncherAppState app = LauncherAppState.getInstance();
167 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
168
Winson Chung5f8afe62013-08-12 16:19:28 -0700169 mContent.setCellDimensions(grid.folderCellWidthPx, grid.folderCellHeightPx);
Adam Cohen2801caf2011-05-13 20:57:39 -0700170 mContent.setGridSize(0, 0);
Michael Jurkaa52570f2012-03-20 03:18:20 -0700171 mContent.getShortcutsAndWidgets().setMotionEventSplittingEnabled(false);
Adam Cohen2374abf2013-04-16 14:56:57 -0700172 mContent.setInvertIfRtl(true);
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800173
Adam Cohenac56cff2011-09-28 20:45:37 -0700174 mFolderName = (FolderEditText) findViewById(R.id.folder_name);
175 mFolderName.setFolder(this);
Adam Cohenea0818d2011-09-30 20:06:58 -0700176 mFolderName.setOnFocusChangeListener(this);
Adam Cohen76fc0852011-06-17 13:26:23 -0700177
Adam Cohen76fc0852011-06-17 13:26:23 -0700178 // We disable action mode for now since it messes up the view on phones
179 mFolderName.setCustomSelectionActionModeCallback(mActionModeCallback);
Adam Cohen76fc0852011-06-17 13:26:23 -0700180 mFolderName.setOnEditorActionListener(this);
Adam Cohen4ef610f2011-06-21 22:37:36 -0700181 mFolderName.setSelectAllOnFocus(true);
Adam Cohen7a14d0b2011-06-24 11:36:35 -0700182 mFolderName.setInputType(mFolderName.getInputType() |
183 InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS | InputType.TYPE_TEXT_FLAG_CAP_WORDS);
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800184
185 // We only have the folder name at the bottom for now
186 mBottomContent = mFolderName;
187 // We find out how tall the bottom content wants to be (it is set to wrap_content), so that
188 // we can allocate the appropriate amount of space for it.
189 int measureSpec = MeasureSpec.UNSPECIFIED;
190 mBottomContent.measure(measureSpec, measureSpec);
191 mBottomContentHeight = mBottomContent.getMeasuredHeight();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800192 }
Adam Cohen2801caf2011-05-13 20:57:39 -0700193
Adam Cohen76fc0852011-06-17 13:26:23 -0700194 private ActionMode.Callback mActionModeCallback = new ActionMode.Callback() {
195 public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
196 return false;
197 }
198
199 public boolean onCreateActionMode(ActionMode mode, Menu menu) {
200 return false;
201 }
202
203 public void onDestroyActionMode(ActionMode mode) {
204 }
205
206 public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
207 return false;
208 }
209 };
210
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800211 public void onClick(View v) {
Adam Cohendf2cc412011-04-27 16:56:57 -0700212 Object tag = v.getTag();
213 if (tag instanceof ShortcutInfo) {
Adam Cohenb5fe60c2013-06-06 22:03:51 -0700214 mLauncher.onClick(v);
Adam Cohendf2cc412011-04-27 16:56:57 -0700215 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800216 }
217
218 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -0700219 // Return if global dragging is not enabled
220 if (!mLauncher.isDraggingEnabled()) return true;
221
Adam Cohendf2cc412011-04-27 16:56:57 -0700222 Object tag = v.getTag();
223 if (tag instanceof ShortcutInfo) {
Adam Cohendf2cc412011-04-27 16:56:57 -0700224 ShortcutInfo item = (ShortcutInfo) tag;
225 if (!v.isInTouchMode()) {
226 return false;
227 }
228
Adam Cohenac8c8762011-07-13 11:15:27 -0700229 mLauncher.getWorkspace().beginDragShared(v, this);
Adam Cohen76078c42011-06-09 15:06:52 -0700230
231 mCurrentDragInfo = item;
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800232 mEmptyCellRank = item.rank;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700233 mCurrentDragView = v;
Adam Cohenfc53cd22011-07-20 15:45:11 -0700234
235 mContent.removeView(mCurrentDragView);
236 mInfo.remove(mCurrentDragInfo);
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700237 mDragInProgress = true;
Adam Cohen05e0f402011-08-01 12:12:49 -0700238 mItemAddedBackToSelfViaIcon = false;
Adam Cohendf2cc412011-04-27 16:56:57 -0700239 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800240 return true;
241 }
242
Adam Cohen76fc0852011-06-17 13:26:23 -0700243 public boolean isEditingName() {
244 return mIsEditingName;
245 }
246
247 public void startEditingFolderName() {
Adam Cohena65beee2011-06-27 21:32:23 -0700248 mFolderName.setHint("");
Adam Cohen76fc0852011-06-17 13:26:23 -0700249 mIsEditingName = true;
250 }
251
252 public void dismissEditingName() {
253 mInputMethodManager.hideSoftInputFromWindow(getWindowToken(), 0);
254 doneEditingFolderName(true);
255 }
256
257 public void doneEditingFolderName(boolean commit) {
Adam Cohena65beee2011-06-27 21:32:23 -0700258 mFolderName.setHint(sHintText);
Adam Cohen1df26a32011-08-12 16:15:23 -0700259 // Convert to a string here to ensure that no other state associated with the text field
260 // gets saved.
Adam Cohen3371da02011-10-25 21:38:29 -0700261 String newTitle = mFolderName.getText().toString();
262 mInfo.setTitle(newTitle);
Adam Cohen76fc0852011-06-17 13:26:23 -0700263 LauncherModel.updateItemInDatabase(mLauncher, mInfo);
Adam Cohenac56cff2011-09-28 20:45:37 -0700264
Adam Cohen3371da02011-10-25 21:38:29 -0700265 if (commit) {
266 sendCustomAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED,
Michael Jurka8b805b12012-04-18 14:23:14 -0700267 String.format(getContext().getString(R.string.folder_renamed), newTitle));
Adam Cohen3371da02011-10-25 21:38:29 -0700268 }
Adam Cohenac56cff2011-09-28 20:45:37 -0700269 // In order to clear the focus from the text field, we set the focus on ourself. This
270 // ensures that every time the field is clicked, focus is gained, giving reliable behavior.
271 requestFocus();
272
Adam Cohene601a432011-07-26 21:51:30 -0700273 Selection.setSelection((Spannable) mFolderName.getText(), 0, 0);
Adam Cohen76fc0852011-06-17 13:26:23 -0700274 mIsEditingName = false;
275 }
276
277 public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
278 if (actionId == EditorInfo.IME_ACTION_DONE) {
279 dismissEditingName();
280 return true;
281 }
282 return false;
283 }
284
285 public View getEditTextRegion() {
286 return mFolderName;
287 }
288
Adam Cohen0c872ba2011-05-05 10:34:16 -0700289 /**
290 * We need to handle touch events to prevent them from falling through to the workspace below.
291 */
292 @Override
293 public boolean onTouchEvent(MotionEvent ev) {
294 return true;
295 }
296
Joe Onorato00acb122009-08-04 16:04:30 -0400297 public void setDragController(DragController dragController) {
298 mDragController = dragController;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800299 }
300
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800301 public void setFolderIcon(FolderIcon icon) {
Adam Cohen2801caf2011-05-13 20:57:39 -0700302 mFolderIcon = icon;
303 }
304
Adam Cohen3371da02011-10-25 21:38:29 -0700305 @Override
306 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
307 // When the folder gets focus, we don't want to announce the list of items.
308 return true;
309 }
310
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800311 /**
312 * @return the FolderInfo object associated with this folder
313 */
314 FolderInfo getInfo() {
315 return mInfo;
316 }
317
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800318 void bind(FolderInfo info) {
319 mInfo = info;
Adam Cohendf2cc412011-04-27 16:56:57 -0700320 ArrayList<ShortcutInfo> children = info.contents;
Sunny Goyal08f72612015-01-05 13:41:43 -0800321 Collections.sort(children, Utilities.RANK_COMPARATOR);
Sunny Goyal08f72612015-01-05 13:41:43 -0800322
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800323 ArrayList<ShortcutInfo> overflow = mContent.bindItems(children);
Adam Cohen0057bbc2011-08-12 18:30:51 -0700324
Jason Monk4ff73882014-04-24 16:48:07 -0400325 // If our folder has too many items we prune them from the list. This is an issue
Adam Cohenc508b2d2011-06-28 14:41:44 -0700326 // when upgrading from the old Folders implementation which could contain an unlimited
327 // number of items.
328 for (ShortcutInfo item: overflow) {
329 mInfo.remove(item);
330 LauncherModel.deleteItemFromDatabase(mLauncher, item);
331 }
332
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800333 DragLayer.LayoutParams lp = (DragLayer.LayoutParams) getLayoutParams();
334 if (lp == null) {
335 lp = new DragLayer.LayoutParams(0, 0);
336 lp.customPosition = true;
337 setLayoutParams(lp);
338 }
339 centerAboutIcon();
340
Adam Cohen4dbe6d92011-05-18 17:14:15 -0700341 mItemsInvalidated = true;
Adam Cohenac56cff2011-09-28 20:45:37 -0700342 updateTextViewFocus();
Adam Cohena9cf38f2011-05-02 15:36:58 -0700343 mInfo.addListener(this);
Adam Cohen4ef610f2011-06-21 22:37:36 -0700344
Adam Cohenafb01ee2011-06-23 15:38:03 -0700345 if (!sDefaultFolderName.contentEquals(mInfo.title)) {
Adam Cohen4ef610f2011-06-21 22:37:36 -0700346 mFolderName.setText(mInfo.title);
347 } else {
348 mFolderName.setText("");
349 }
Winson Chung33231f52013-12-09 16:57:45 -0800350
351 // In case any children didn't come across during loading, clean up the folder accordingly
352 mFolderIcon.post(new Runnable() {
353 public void run() {
354 if (getItemCount() <= 1) {
355 replaceFolderWithFinalItem();
356 }
357 }
358 });
Adam Cohendf2cc412011-04-27 16:56:57 -0700359 }
360
361 /**
362 * Creates a new UserFolder, inflated from R.layout.user_folder.
363 *
364 * @param context The application's context.
365 *
366 * @return A new UserFolder.
367 */
368 static Folder fromXml(Context context) {
369 return (Folder) LayoutInflater.from(context).inflate(R.layout.user_folder, null);
370 }
371
372 /**
373 * This method is intended to make the UserFolder to be visually identical in size and position
374 * to its associated FolderIcon. This allows for a seamless transition into the expanded state.
375 */
376 private void positionAndSizeAsIcon() {
Adam Cohen3e8f8112011-07-02 18:03:00 -0700377 if (!(getParent() instanceof DragLayer)) return;
Adam Cohen662b5982011-12-13 17:45:21 -0800378 setScaleX(0.8f);
379 setScaleY(0.8f);
380 setAlpha(0f);
Adam Cohendf2cc412011-04-27 16:56:57 -0700381 mState = STATE_SMALL;
382 }
383
Adam Cohenc4fe9ea2014-08-18 18:54:10 -0700384 private void prepareReveal() {
385 setScaleX(1f);
386 setScaleY(1f);
387 setAlpha(1f);
388 mState = STATE_SMALL;
389 }
390
Adam Cohendf2cc412011-04-27 16:56:57 -0700391 public void animateOpen() {
Adam Cohen8e776a62011-06-28 18:10:06 -0700392 if (!(getParent() instanceof DragLayer)) return;
Adam Cohen6441de02011-12-14 14:25:32 -0800393
Adam Cohenc4fe9ea2014-08-18 18:54:10 -0700394 Animator openFolderAnim = null;
395 final Runnable onCompleteRunnable;
Kenny Guyd794a3f2014-09-16 15:17:58 +0100396 if (!Utilities.isLmpOrAbove()) {
Adam Cohenc4fe9ea2014-08-18 18:54:10 -0700397 positionAndSizeAsIcon();
398 centerAboutIcon();
399
400 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1);
401 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.0f);
402 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.0f);
403 final ObjectAnimator oa =
404 LauncherAnimUtils.ofPropertyValuesHolder(this, alpha, scaleX, scaleY);
405 oa.setDuration(mExpandDuration);
406 openFolderAnim = oa;
407
408 setLayerType(LAYER_TYPE_HARDWARE, null);
409 onCompleteRunnable = new Runnable() {
410 @Override
411 public void run() {
412 setLayerType(LAYER_TYPE_NONE, null);
413 }
414 };
415 } else {
416 prepareReveal();
417 centerAboutIcon();
418
419 int width = getPaddingLeft() + getPaddingRight() + mContent.getDesiredWidth();
420 int height = getFolderHeight();
421
422 float transX = - 0.075f * (width / 2 - getPivotX());
423 float transY = - 0.075f * (height / 2 - getPivotY());
424 setTranslationX(transX);
425 setTranslationY(transY);
426 PropertyValuesHolder tx = PropertyValuesHolder.ofFloat("translationX", transX, 0);
427 PropertyValuesHolder ty = PropertyValuesHolder.ofFloat("translationY", transY, 0);
428
429 int rx = (int) Math.max(Math.max(width - getPivotX(), 0), getPivotX());
430 int ry = (int) Math.max(Math.max(height - getPivotY(), 0), getPivotY());
431 float radius = (float) Math.sqrt(rx * rx + ry * ry);
432 AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
433 Animator reveal = LauncherAnimUtils.createCircularReveal(this, (int) getPivotX(),
434 (int) getPivotY(), 0, radius);
435 reveal.setDuration(mMaterialExpandDuration);
436 reveal.setInterpolator(new LogDecelerateInterpolator(100, 0));
437
438 mContent.setAlpha(0f);
439 Animator iconsAlpha = LauncherAnimUtils.ofFloat(mContent, "alpha", 0f, 1f);
440 iconsAlpha.setDuration(mMaterialExpandDuration);
441 iconsAlpha.setStartDelay(mMaterialExpandStagger);
442 iconsAlpha.setInterpolator(new AccelerateInterpolator(1.5f));
443
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800444 mBottomContent.setAlpha(0f);
445 Animator textAlpha = LauncherAnimUtils.ofFloat(mBottomContent, "alpha", 0f, 1f);
Adam Cohenc4fe9ea2014-08-18 18:54:10 -0700446 textAlpha.setDuration(mMaterialExpandDuration);
447 textAlpha.setStartDelay(mMaterialExpandStagger);
448 textAlpha.setInterpolator(new AccelerateInterpolator(1.5f));
449
450 Animator drift = LauncherAnimUtils.ofPropertyValuesHolder(this, tx, ty);
451 drift.setDuration(mMaterialExpandDuration);
452 drift.setStartDelay(mMaterialExpandStagger);
453 drift.setInterpolator(new LogDecelerateInterpolator(60, 0));
454
455 anim.play(drift);
456 anim.play(iconsAlpha);
457 anim.play(textAlpha);
458 anim.play(reveal);
459
460 openFolderAnim = anim;
461
462 mContent.setLayerType(LAYER_TYPE_HARDWARE, null);
463 onCompleteRunnable = new Runnable() {
464 @Override
465 public void run() {
466 mContent.setLayerType(LAYER_TYPE_NONE, null);
467 }
468 };
469 }
470 openFolderAnim.addListener(new AnimatorListenerAdapter() {
Adam Cohendf2cc412011-04-27 16:56:57 -0700471 @Override
472 public void onAnimationStart(Animator animation) {
Adam Cohen3371da02011-10-25 21:38:29 -0700473 sendCustomAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED,
Michael Jurka8b805b12012-04-18 14:23:14 -0700474 String.format(getContext().getString(R.string.folder_opened),
Adam Cohen3371da02011-10-25 21:38:29 -0700475 mContent.getCountX(), mContent.getCountY()));
Adam Cohendf2cc412011-04-27 16:56:57 -0700476 mState = STATE_ANIMATING;
477 }
478 @Override
479 public void onAnimationEnd(Animator animation) {
Adam Cohen2801caf2011-05-13 20:57:39 -0700480 mState = STATE_OPEN;
Adam Cohenc4fe9ea2014-08-18 18:54:10 -0700481
482 if (onCompleteRunnable != null) {
483 onCompleteRunnable.run();
484 }
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800485
Adam Cohenac56cff2011-09-28 20:45:37 -0700486 setFocusOnFirstChild();
Adam Cohendf2cc412011-04-27 16:56:57 -0700487 }
488 });
Adam Cohenc4fe9ea2014-08-18 18:54:10 -0700489 openFolderAnim.start();
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800490
491 // Make sure the folder picks up the last drag move even if the finger doesn't move.
492 if (mDragController.isDragging()) {
493 mDragController.forceTouchMove();
494 }
495 }
496
497 public void beginExternalDrag(ShortcutInfo item) {
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800498 mCurrentDragInfo = item;
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800499 mEmptyCellRank = mContent.allocateNewLastItemRank();
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800500 mIsExternalDrag = true;
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800501 mDragInProgress = true;
Adam Cohendf2cc412011-04-27 16:56:57 -0700502 }
503
Adam Cohen3371da02011-10-25 21:38:29 -0700504 private void sendCustomAccessibilityEvent(int type, String text) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700505 AccessibilityManager accessibilityManager = (AccessibilityManager)
506 getContext().getSystemService(Context.ACCESSIBILITY_SERVICE);
507 if (accessibilityManager.isEnabled()) {
Adam Cohen3371da02011-10-25 21:38:29 -0700508 AccessibilityEvent event = AccessibilityEvent.obtain(type);
509 onInitializeAccessibilityEvent(event);
510 event.getText().add(text);
Michael Jurka8b805b12012-04-18 14:23:14 -0700511 accessibilityManager.sendAccessibilityEvent(event);
Adam Cohen3371da02011-10-25 21:38:29 -0700512 }
513 }
514
Adam Cohenac56cff2011-09-28 20:45:37 -0700515 private void setFocusOnFirstChild() {
516 View firstChild = mContent.getChildAt(0, 0);
517 if (firstChild != null) {
518 firstChild.requestFocus();
519 }
520 }
521
Adam Cohendf2cc412011-04-27 16:56:57 -0700522 public void animateClosed() {
Adam Cohen8e776a62011-06-28 18:10:06 -0700523 if (!(getParent() instanceof DragLayer)) return;
Adam Cohen662b5982011-12-13 17:45:21 -0800524 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0);
525 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 0.9f);
526 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 0.9f);
Michael Jurka032e6ba2013-04-22 15:08:42 +0200527 final ObjectAnimator oa =
Michael Jurka2ecf9952012-06-18 12:52:28 -0700528 LauncherAnimUtils.ofPropertyValuesHolder(this, alpha, scaleX, scaleY);
Adam Cohendf2cc412011-04-27 16:56:57 -0700529
Adam Cohen2801caf2011-05-13 20:57:39 -0700530 oa.addListener(new AnimatorListenerAdapter() {
Adam Cohendf2cc412011-04-27 16:56:57 -0700531 @Override
532 public void onAnimationEnd(Animator animation) {
Adam Cohen2801caf2011-05-13 20:57:39 -0700533 onCloseComplete();
Michael Jurka0121c3e2012-05-31 08:36:04 -0700534 setLayerType(LAYER_TYPE_NONE, null);
Adam Cohen2801caf2011-05-13 20:57:39 -0700535 mState = STATE_SMALL;
Adam Cohendf2cc412011-04-27 16:56:57 -0700536 }
537 @Override
538 public void onAnimationStart(Animator animation) {
Adam Cohen3371da02011-10-25 21:38:29 -0700539 sendCustomAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED,
Michael Jurka8b805b12012-04-18 14:23:14 -0700540 getContext().getString(R.string.folder_closed));
Adam Cohendf2cc412011-04-27 16:56:57 -0700541 mState = STATE_ANIMATING;
542 }
543 });
Adam Cohen2801caf2011-05-13 20:57:39 -0700544 oa.setDuration(mExpandDuration);
Michael Jurka0121c3e2012-05-31 08:36:04 -0700545 setLayerType(LAYER_TYPE_HARDWARE, null);
Michael Jurkaf1ad6082013-03-13 12:55:46 +0100546 oa.start();
Adam Cohendf2cc412011-04-27 16:56:57 -0700547 }
548
Adam Cohencb3382b2011-05-24 14:07:08 -0700549 public boolean acceptDrop(DragObject d) {
550 final ItemInfo item = (ItemInfo) d.dragInfo;
Adam Cohendf2cc412011-04-27 16:56:57 -0700551 final int itemType = item.itemType;
Adam Cohen2801caf2011-05-13 20:57:39 -0700552 return ((itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION ||
553 itemType == LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT) &&
554 !isFull());
Adam Cohendf2cc412011-04-27 16:56:57 -0700555 }
556
Adam Cohencb3382b2011-05-24 14:07:08 -0700557 public void onDragEnter(DragObject d) {
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800558 mPrevTargetRank = -1;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700559 mOnExitAlarm.cancelAlarm();
560 }
561
562 OnAlarmListener mReorderAlarmListener = new OnAlarmListener() {
563 public void onAlarm(Alarm alarm) {
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800564 mContent.realTimeReorder(mEmptyCellRank, mTargetRank);
565 mEmptyCellRank = mTargetRank;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700566 }
567 };
568
Sunny Goyalc46bfef2015-01-05 12:40:08 -0800569 @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
Adam Cohen2374abf2013-04-16 14:56:57 -0700570 public boolean isLayoutRtl() {
571 return (getLayoutDirection() == LAYOUT_DIRECTION_RTL);
572 }
573
Adam Cohencb3382b2011-05-24 14:07:08 -0700574 public void onDragOver(DragObject d) {
Sunny Goyal1587d532015-01-29 09:57:16 -0800575 final float[] r = d.getVisualCenter(null);
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700576 r[0] -= getPaddingLeft();
577 r[1] -= getPaddingTop();
578
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800579 mTargetRank = mContent.findNearestArea((int) r[0], (int) r[1], 1, 1);
580 if (mTargetRank != mPrevTargetRank) {
Alan Viverette4cda5b72013-08-28 17:53:41 -0700581 mReorderAlarm.cancelAlarm();
Sunny Goyalc46bfef2015-01-05 12:40:08 -0800582 mReorderAlarm.setOnAlarmListener(mReorderAlarmListener);
583 mReorderAlarm.setAlarm(REORDER_DELAY);
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800584 mPrevTargetRank = mTargetRank;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700585 }
Adam Cohendf2cc412011-04-27 16:56:57 -0700586 }
587
Adam Cohenbfbfd262011-06-13 16:55:12 -0700588 OnAlarmListener mOnExitAlarmListener = new OnAlarmListener() {
589 public void onAlarm(Alarm alarm) {
Adam Cohen3e8f8112011-07-02 18:03:00 -0700590 completeDragExit();
Adam Cohenbfbfd262011-06-13 16:55:12 -0700591 }
592 };
593
Adam Cohen95bb8002011-07-03 23:40:28 -0700594 public void completeDragExit() {
Adam Cohen3e8f8112011-07-02 18:03:00 -0700595 mLauncher.closeFolder();
596 mCurrentDragInfo = null;
597 mCurrentDragView = null;
598 mSuppressOnAdd = false;
599 mRearrangeOnClose = true;
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800600 mIsExternalDrag = false;
Adam Cohen3e8f8112011-07-02 18:03:00 -0700601 }
602
Adam Cohencb3382b2011-05-24 14:07:08 -0700603 public void onDragExit(DragObject d) {
Mindy DelliCarpini53b8d072013-07-03 08:23:13 -0700604 // We only close the folder if this is a true drag exit, ie. not because
605 // a drop has occurred above the folder.
Adam Cohenbfbfd262011-06-13 16:55:12 -0700606 if (!d.dragComplete) {
607 mOnExitAlarm.setOnAlarmListener(mOnExitAlarmListener);
608 mOnExitAlarm.setAlarm(ON_EXIT_CLOSE_DELAY);
609 }
610 mReorderAlarm.cancelAlarm();
Adam Cohen2801caf2011-05-13 20:57:39 -0700611 }
612
Michael Jurka1e2f4652013-07-08 18:03:46 -0700613 public void onDropCompleted(final View target, final DragObject d,
614 final boolean isFlingToDelete, final boolean success) {
615 if (mDeferDropAfterUninstall) {
Michael Jurkaf3007582013-08-21 14:33:57 +0200616 Log.d(TAG, "Deferred handling drop because waiting for uninstall.");
Michael Jurka1e2f4652013-07-08 18:03:46 -0700617 mDeferredAction = new Runnable() {
618 public void run() {
619 onDropCompleted(target, d, isFlingToDelete, success);
620 mDeferredAction = null;
621 }
622 };
623 return;
624 }
625
626 boolean beingCalledAfterUninstall = mDeferredAction != null;
627 boolean successfulDrop =
628 success && (!beingCalledAfterUninstall || mUninstallSuccessful);
Winson Chung5f8afe62013-08-12 16:19:28 -0700629
Michael Jurka1e2f4652013-07-08 18:03:46 -0700630 if (successfulDrop) {
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800631 if (mDeleteFolderOnDropCompleted && !mItemAddedBackToSelfViaIcon && target != this) {
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700632 replaceFolderWithFinalItem();
633 }
634 } else {
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800635 rearrangeChildren();
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700636 // The drag failed, we need to return the item to the folder
637 mFolderIcon.onDrop(d);
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700638 }
639
640 if (target != this) {
641 if (mOnExitAlarm.alarmPending()) {
642 mOnExitAlarm.cancelAlarm();
Michael Jurka54554252013-08-01 12:52:23 +0200643 if (!successfulDrop) {
Adam Cohen7a8b82b2013-05-29 18:41:50 -0700644 mSuppressFolderDeletion = true;
645 }
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700646 completeDragExit();
647 }
Adam Cohen9c58d822013-12-13 17:18:10 -0800648 }
649
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700650 mDeleteFolderOnDropCompleted = false;
651 mDragInProgress = false;
Adam Cohen05e0f402011-08-01 12:12:49 -0700652 mItemAddedBackToSelfViaIcon = false;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700653 mCurrentDragInfo = null;
654 mCurrentDragView = null;
655 mSuppressOnAdd = false;
Adam Cohen4045eb72011-10-06 11:44:26 -0700656
657 // Reordering may have occured, and we need to save the new item locations. We do this once
658 // at the end to prevent unnecessary database operations.
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700659 updateItemLocationsInDatabaseBatch();
Adam Cohen4045eb72011-10-06 11:44:26 -0700660 }
661
Michael Jurka1e2f4652013-07-08 18:03:46 -0700662 public void deferCompleteDropAfterUninstallActivity() {
663 mDeferDropAfterUninstall = true;
664 }
665
666 public void onUninstallActivityReturned(boolean success) {
667 mDeferDropAfterUninstall = false;
668 mUninstallSuccessful = success;
669 if (mDeferredAction != null) {
670 mDeferredAction.run();
671 }
672 }
673
Winson Chunga48487a2012-03-20 16:19:37 -0700674 @Override
Winson Chungeeb5bbc2013-11-13 15:47:05 -0800675 public float getIntrinsicIconScaleFactor() {
676 return 1f;
677 }
678
679 @Override
Winson Chung043f2af2012-03-01 16:09:54 -0800680 public boolean supportsFlingToDelete() {
681 return true;
682 }
683
Mathew Inwood1eeb3fc2013-11-25 17:01:34 +0000684 @Override
685 public boolean supportsAppInfoDropTarget() {
686 return false;
687 }
688
689 @Override
690 public boolean supportsDeleteDropTarget() {
691 return true;
692 }
693
Winson Chunga48487a2012-03-20 16:19:37 -0700694 public void onFlingToDelete(DragObject d, int x, int y, PointF vec) {
695 // Do nothing
696 }
697
698 @Override
699 public void onFlingToDeleteCompleted() {
700 // Do nothing
701 }
702
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700703 private void updateItemLocationsInDatabaseBatch() {
704 ArrayList<View> list = getItemsInReadingOrder();
705 ArrayList<ItemInfo> items = new ArrayList<ItemInfo>();
706 for (int i = 0; i < list.size(); i++) {
707 View v = list.get(i);
708 ItemInfo info = (ItemInfo) v.getTag();
Sunny Goyal08f72612015-01-05 13:41:43 -0800709 info.rank = i;
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700710 items.add(info);
711 }
712
713 LauncherModel.moveItemsInDatabase(mLauncher, items, mInfo.id, 0);
714 }
715
Adam Cohene25af792013-06-06 23:08:25 -0700716 public void addItemLocationsInDatabase() {
717 ArrayList<View> list = getItemsInReadingOrder();
718 for (int i = 0; i < list.size(); i++) {
719 View v = list.get(i);
720 ItemInfo info = (ItemInfo) v.getTag();
721 LauncherModel.addItemToDatabase(mLauncher, info, mInfo.id, 0,
722 info.cellX, info.cellY, false);
723 }
724 }
725
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700726 public void notifyDrop() {
727 if (mDragInProgress) {
Adam Cohen05e0f402011-08-01 12:12:49 -0700728 mItemAddedBackToSelfViaIcon = true;
Adam Cohen76078c42011-06-09 15:06:52 -0700729 }
Adam Cohendf2cc412011-04-27 16:56:57 -0700730 }
731
732 public boolean isDropEnabled() {
733 return true;
734 }
735
Adam Cohen2801caf2011-05-13 20:57:39 -0700736 public boolean isFull() {
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800737 return mContent.isFull();
Adam Cohen2801caf2011-05-13 20:57:39 -0700738 }
739
740 private void centerAboutIcon() {
Adam Cohen8e776a62011-06-28 18:10:06 -0700741 DragLayer.LayoutParams lp = (DragLayer.LayoutParams) getLayoutParams();
Adam Cohen2801caf2011-05-13 20:57:39 -0700742
Winson Chung892c74d2013-08-22 16:15:50 -0700743 DragLayer parent = (DragLayer) mLauncher.findViewById(R.id.drag_layer);
Adam Cohen2801caf2011-05-13 20:57:39 -0700744 int width = getPaddingLeft() + getPaddingRight() + mContent.getDesiredWidth();
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700745 int height = getFolderHeight();
Adam Cohen2801caf2011-05-13 20:57:39 -0700746
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800747 float scale = parent.getDescendantRectRelativeToSelf(mFolderIcon, sTempRect);
Adam Cohen8e776a62011-06-28 18:10:06 -0700748
Winson Chungaf40f202013-09-18 18:26:31 -0700749 LauncherAppState app = LauncherAppState.getInstance();
750 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
751
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800752 int centerX = (int) (sTempRect.left + sTempRect.width() * scale / 2);
753 int centerY = (int) (sTempRect.top + sTempRect.height() * scale / 2);
Adam Cohen2801caf2011-05-13 20:57:39 -0700754 int centeredLeft = centerX - width / 2;
755 int centeredTop = centerY - height / 2;
Winson Chung3057b1c2013-10-10 17:35:35 -0700756 int currentPage = mLauncher.getWorkspace().getNextPage();
Adam Cohen7cc1bc42011-12-13 21:28:43 -0800757 // In case the workspace is scrolling, we need to use the final scroll to compute
758 // the folders bounds.
759 mLauncher.getWorkspace().setFinalScrollForPageChange(currentPage);
Adam Cohen35e7e642011-07-17 14:47:18 -0700760 // We first fetch the currently visible CellLayoutChildren
Adam Cohen7cc1bc42011-12-13 21:28:43 -0800761 CellLayout currentLayout = (CellLayout) mLauncher.getWorkspace().getChildAt(currentPage);
Michael Jurkaa52570f2012-03-20 03:18:20 -0700762 ShortcutAndWidgetContainer boundingLayout = currentLayout.getShortcutsAndWidgets();
Adam Cohen35e7e642011-07-17 14:47:18 -0700763 Rect bounds = new Rect();
764 parent.getDescendantRectRelativeToSelf(boundingLayout, bounds);
Adam Cohen7cc1bc42011-12-13 21:28:43 -0800765 // We reset the workspaces scroll
766 mLauncher.getWorkspace().resetFinalScrollForPageChange(currentPage);
Adam Cohen2801caf2011-05-13 20:57:39 -0700767
Adam Cohen35e7e642011-07-17 14:47:18 -0700768 // We need to bound the folder to the currently visible CellLayoutChildren
769 int left = Math.min(Math.max(bounds.left, centeredLeft),
770 bounds.left + bounds.width() - width);
771 int top = Math.min(Math.max(bounds.top, centeredTop),
772 bounds.top + bounds.height() - height);
Winson Chungaf40f202013-09-18 18:26:31 -0700773 if (grid.isPhone() && (grid.availableWidthPx - width) < grid.iconSizePx) {
774 // Center the folder if it is full (on phones only)
775 left = (grid.availableWidthPx - width) / 2;
776 } else if (width >= bounds.width()) {
777 // If the folder doesn't fit within the bounds, center it about the desired bounds
Adam Cohen35e7e642011-07-17 14:47:18 -0700778 left = bounds.left + (bounds.width() - width) / 2;
Adam Cohen0e4857c2011-06-30 17:05:14 -0700779 }
Adam Cohen35e7e642011-07-17 14:47:18 -0700780 if (height >= bounds.height()) {
781 top = bounds.top + (bounds.height() - height) / 2;
Adam Cohen0e4857c2011-06-30 17:05:14 -0700782 }
Adam Cohen2801caf2011-05-13 20:57:39 -0700783
784 int folderPivotX = width / 2 + (centeredLeft - left);
785 int folderPivotY = height / 2 + (centeredTop - top);
786 setPivotX(folderPivotX);
787 setPivotY(folderPivotY);
Adam Cohen268c4752012-06-06 17:47:33 -0700788 mFolderIconPivotX = (int) (mFolderIcon.getMeasuredWidth() *
Adam Cohen2801caf2011-05-13 20:57:39 -0700789 (1.0f * folderPivotX / width));
Adam Cohen268c4752012-06-06 17:47:33 -0700790 mFolderIconPivotY = (int) (mFolderIcon.getMeasuredHeight() *
Adam Cohen2801caf2011-05-13 20:57:39 -0700791 (1.0f * folderPivotY / height));
Adam Cohen3bf84d32012-05-07 20:17:14 -0700792
Adam Cohen662b5982011-12-13 17:45:21 -0800793 lp.width = width;
794 lp.height = height;
795 lp.x = left;
796 lp.y = top;
Adam Cohen2801caf2011-05-13 20:57:39 -0700797 }
798
Adam Cohen268c4752012-06-06 17:47:33 -0700799 float getPivotXForIconAnimation() {
800 return mFolderIconPivotX;
801 }
802 float getPivotYForIconAnimation() {
803 return mFolderIconPivotY;
804 }
805
Winson Chung892c74d2013-08-22 16:15:50 -0700806 private int getContentAreaHeight() {
807 LauncherAppState app = LauncherAppState.getInstance();
808 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
809 Rect workspacePadding = grid.getWorkspacePadding(grid.isLandscape ?
810 CellLayout.LANDSCAPE : CellLayout.PORTRAIT);
811 int maxContentAreaHeight = grid.availableHeightPx -
Winson Chung892c74d2013-08-22 16:15:50 -0700812 workspacePadding.top - workspacePadding.bottom -
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800813 mBottomContentHeight;
Adam Cohen1960ea42013-11-12 11:33:14 +0000814 int height = Math.min(maxContentAreaHeight,
Winson Chung892c74d2013-08-22 16:15:50 -0700815 mContent.getDesiredHeight());
Adam Cohen1960ea42013-11-12 11:33:14 +0000816 return Math.max(height, MIN_CONTENT_DIMEN);
817 }
818
819 private int getContentAreaWidth() {
820 return Math.max(mContent.getDesiredWidth(), MIN_CONTENT_DIMEN);
Winson Chung892c74d2013-08-22 16:15:50 -0700821 }
822
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700823 private int getFolderHeight() {
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800824 return getFolderHeight(getContentAreaHeight());
825 }
826
827 private int getFolderHeight(int contentAreaHeight) {
828 return getPaddingTop() + getPaddingBottom() + contentAreaHeight + mBottomContentHeight;
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700829 }
Adam Cohen234c4cd2011-07-17 21:03:04 -0700830
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700831 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800832 int contentWidth = getContentAreaWidth();
833 int contentHeight = getContentAreaHeight();
Adam Cohen1960ea42013-11-12 11:33:14 +0000834
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800835 int contentAreaWidthSpec = MeasureSpec.makeMeasureSpec(contentWidth, MeasureSpec.EXACTLY);
836 int contentAreaHeightSpec = MeasureSpec.makeMeasureSpec(contentHeight, MeasureSpec.EXACTLY);
837
838 mContent.setFixedSize(contentWidth, contentHeight);
839 mContentWrapper.measure(contentAreaWidthSpec, contentAreaHeightSpec);
840
841 // Move the bottom content below mContent
842 mBottomContent.measure(contentAreaWidthSpec,
843 MeasureSpec.makeMeasureSpec(mBottomContentHeight, MeasureSpec.EXACTLY));
844
845 int folderWidth = getPaddingLeft() + getPaddingRight() + contentWidth;
846 int folderHeight = getFolderHeight(contentHeight);
847 setMeasuredDimension(folderWidth, folderHeight);
Adam Cohen234c4cd2011-07-17 21:03:04 -0700848 }
849
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800850 /**
851 * Rearranges the children based on their rank.
852 */
853 public void rearrangeChildren() {
854 rearrangeChildren(-1);
855 }
Adam Cohen2801caf2011-05-13 20:57:39 -0700856
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800857 /**
858 * Rearranges the children based on their rank.
859 * @param itemCount if greater than the total children count, empty spaces are left at the end,
860 * otherwise it is ignored.
861 */
862 public void rearrangeChildren(int itemCount) {
863 ArrayList<View> views = getItemsInReadingOrder();
864 mContent.arrangeChildren(views, Math.max(itemCount, views.size()));
Adam Cohen7c693212011-05-18 15:26:57 -0700865 mItemsInvalidated = true;
Adam Cohen2801caf2011-05-13 20:57:39 -0700866 }
867
Adam Cohena9cf38f2011-05-02 15:36:58 -0700868 public int getItemCount() {
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800869 return mContent.getItemCount();
Adam Cohena9cf38f2011-05-02 15:36:58 -0700870 }
871
872 public View getItemAt(int index) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700873 return mContent.getShortcutsAndWidgets().getChildAt(index);
Adam Cohena9cf38f2011-05-02 15:36:58 -0700874 }
875
Adam Cohen2801caf2011-05-13 20:57:39 -0700876 private void onCloseComplete() {
Adam Cohen05e0f402011-08-01 12:12:49 -0700877 DragLayer parent = (DragLayer) getParent();
Michael Jurka5649c282012-06-18 10:33:21 -0700878 if (parent != null) {
879 parent.removeView(this);
880 }
Adam Cohen4554ee12011-08-03 16:13:21 -0700881 mDragController.removeDropTarget((DropTarget) this);
Adam Cohen05e0f402011-08-01 12:12:49 -0700882 clearFocus();
Adam Cohenac56cff2011-09-28 20:45:37 -0700883 mFolderIcon.requestFocus();
Adam Cohen05e0f402011-08-01 12:12:49 -0700884
Adam Cohen2801caf2011-05-13 20:57:39 -0700885 if (mRearrangeOnClose) {
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800886 rearrangeChildren();
Adam Cohen2801caf2011-05-13 20:57:39 -0700887 mRearrangeOnClose = false;
888 }
Adam Cohenafb01ee2011-06-23 15:38:03 -0700889 if (getItemCount() <= 1) {
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700890 if (!mDragInProgress && !mSuppressFolderDeletion) {
891 replaceFolderWithFinalItem();
892 } else if (mDragInProgress) {
893 mDeleteFolderOnDropCompleted = true;
894 }
Adam Cohenafb01ee2011-06-23 15:38:03 -0700895 }
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700896 mSuppressFolderDeletion = false;
Adam Cohenafb01ee2011-06-23 15:38:03 -0700897 }
898
899 private void replaceFolderWithFinalItem() {
Adam Cohenafb01ee2011-06-23 15:38:03 -0700900 // Add the last remaining child to the workspace in place of the folder
Adam Cohenfb91f302012-06-11 15:45:18 -0700901 Runnable onCompleteRunnable = new Runnable() {
902 @Override
903 public void run() {
Adam Cohendcd297f2013-06-18 13:13:40 -0700904 CellLayout cellLayout = mLauncher.getCellLayout(mInfo.container, mInfo.screenId);
Adam Cohenafb01ee2011-06-23 15:38:03 -0700905
Winson Chung33231f52013-12-09 16:57:45 -0800906 View child = null;
Adam Cohenfb91f302012-06-11 15:45:18 -0700907 // Move the item from the folder to the workspace, in the position of the folder
908 if (getItemCount() == 1) {
909 ShortcutInfo finalItem = mInfo.contents.get(0);
Adam Cohenc5e63f32012-07-12 16:16:57 -0700910 child = mLauncher.createShortcut(R.layout.application, cellLayout,
Adam Cohenfb91f302012-06-11 15:45:18 -0700911 finalItem);
912 LauncherModel.addOrMoveItemInDatabase(mLauncher, finalItem, mInfo.container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700913 mInfo.screenId, mInfo.cellX, mInfo.cellY);
Adam Cohenfb91f302012-06-11 15:45:18 -0700914 }
Adam Cohen487f7dd2012-06-28 18:12:10 -0700915 if (getItemCount() <= 1) {
916 // Remove the folder
917 LauncherModel.deleteItemFromDatabase(mLauncher, mInfo);
Dan Sandler0eb687f2014-01-10 13:24:55 -0500918 if (cellLayout != null) {
919 // b/12446428 -- sometimes the cell layout has already gone away?
920 cellLayout.removeView(mFolderIcon);
921 }
Adam Cohen487f7dd2012-06-28 18:12:10 -0700922 if (mFolderIcon instanceof DropTarget) {
923 mDragController.removeDropTarget((DropTarget) mFolderIcon);
924 }
925 mLauncher.removeFolder(mInfo);
926 }
Adam Cohenc5e63f32012-07-12 16:16:57 -0700927 // We add the child after removing the folder to prevent both from existing at
Winson Chung0e6a7132013-08-23 12:55:10 -0700928 // the same time in the CellLayout. We need to add the new item with addInScreenFromBind()
929 // to ensure that hotseat items are placed correctly.
Adam Cohenc5e63f32012-07-12 16:16:57 -0700930 if (child != null) {
Winson Chung0e6a7132013-08-23 12:55:10 -0700931 mLauncher.getWorkspace().addInScreenFromBind(child, mInfo.container, mInfo.screenId,
Adam Cohenc5e63f32012-07-12 16:16:57 -0700932 mInfo.cellX, mInfo.cellY, mInfo.spanX, mInfo.spanY);
933 }
Adam Cohenfb91f302012-06-11 15:45:18 -0700934 }
935 };
936 View finalChild = getItemAt(0);
937 if (finalChild != null) {
938 mFolderIcon.performDestroyAnimation(finalChild, onCompleteRunnable);
Winson Chung33231f52013-12-09 16:57:45 -0800939 } else {
940 onCompleteRunnable.run();
Adam Cohenafb01ee2011-06-23 15:38:03 -0700941 }
Adam Cohenfb91f302012-06-11 15:45:18 -0700942 mDestroyed = true;
943 }
944
945 boolean isDestroyed() {
946 return mDestroyed;
Adam Cohen2801caf2011-05-13 20:57:39 -0700947 }
948
Adam Cohenac56cff2011-09-28 20:45:37 -0700949 // This method keeps track of the last item in the folder for the purposes
950 // of keyboard focus
951 private void updateTextViewFocus() {
952 View lastChild = getItemAt(getItemCount() - 1);
953 getItemAt(getItemCount() - 1);
954 if (lastChild != null) {
955 mFolderName.setNextFocusDownId(lastChild.getId());
956 mFolderName.setNextFocusRightId(lastChild.getId());
957 mFolderName.setNextFocusLeftId(lastChild.getId());
958 mFolderName.setNextFocusUpId(lastChild.getId());
959 }
960 }
961
Adam Cohenbfbfd262011-06-13 16:55:12 -0700962 public void onDrop(DragObject d) {
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800963 Runnable cleanUpRunnable = null;
964
Adam Cohen689ff162014-05-08 17:27:56 -0700965 // If we are coming from All Apps space, we defer removing the extra empty screen
966 // until the folder closes
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800967 if (d.dragSource != mLauncher.getWorkspace() && !(d.dragSource instanceof Folder)) {
968 cleanUpRunnable = new Runnable() {
969 @Override
970 public void run() {
Adam Cohen689ff162014-05-08 17:27:56 -0700971 mLauncher.exitSpringLoadedDragModeDelayed(true,
972 Launcher.EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT,
973 null);
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800974 }
975 };
Adam Cohenbfbfd262011-06-13 16:55:12 -0700976 }
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800977
978 View currentDragView;
979 ShortcutInfo si = mCurrentDragInfo;
980 if (mIsExternalDrag) {
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800981 currentDragView = mContent.createAndAddViewForRank(si, mEmptyCellRank);
Sunny Goyal95abbb32014-08-04 10:53:22 -0700982 // Actually move the item in the database if it was an external drag. Call this
983 // before creating the view, so that ShortcutInfo is updated appropriately.
984 LauncherModel.addOrMoveItemInDatabase(
985 mLauncher, si, mInfo.id, 0, si.cellX, si.cellY);
986
987 // We only need to update the locations if it doesn't get handled in #onDropCompleted.
988 if (d.dragSource != this) {
989 updateItemLocationsInDatabaseBatch();
990 }
991 mIsExternalDrag = false;
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800992 } else {
993 currentDragView = mCurrentDragView;
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800994 mContent.addViewForRank(currentDragView, si, mEmptyCellRank);
Adam Cohenbfbfd262011-06-13 16:55:12 -0700995 }
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800996
997 if (d.dragView.hasDrawn()) {
998
999 // Temporarily reset the scale such that the animation target gets calculated correctly.
1000 float scaleX = getScaleX();
1001 float scaleY = getScaleY();
1002 setScaleX(1.0f);
1003 setScaleY(1.0f);
1004 mLauncher.getDragLayer().animateViewIntoPosition(d.dragView, currentDragView,
1005 cleanUpRunnable, null);
1006 setScaleX(scaleX);
1007 setScaleY(scaleY);
1008 } else {
1009 d.deferDragViewCleanupPostAnimation = false;
1010 currentDragView.setVisibility(VISIBLE);
1011 }
1012 mItemsInvalidated = true;
Sunny Goyalc3a609f2015-02-26 17:43:50 -08001013 rearrangeChildren();
Jorim Jaggi55bd9722014-01-16 15:30:42 -08001014
Jorim Jaggi55bd9722014-01-16 15:30:42 -08001015 // Temporarily suppress the listener, as we did all the work already here.
1016 mSuppressOnAdd = true;
1017 mInfo.add(si);
1018 mSuppressOnAdd = false;
Sunny Goyal4b020172014-08-28 14:51:14 -07001019 // Clear the drag info, as it is no longer being dragged.
1020 mCurrentDragInfo = null;
Adam Cohenbfbfd262011-06-13 16:55:12 -07001021 }
1022
Adam Cohen7a8b82b2013-05-29 18:41:50 -07001023 // This is used so the item doesn't immediately appear in the folder when added. In one case
1024 // we need to create the illusion that the item isn't added back to the folder yet, to
1025 // to correspond to the animation of the icon back into the folder. This is
1026 public void hideItem(ShortcutInfo info) {
1027 View v = getViewForInfo(info);
1028 v.setVisibility(INVISIBLE);
1029 }
1030 public void showItem(ShortcutInfo info) {
1031 View v = getViewForInfo(info);
1032 v.setVisibility(VISIBLE);
1033 }
1034
Adam Cohenbfbfd262011-06-13 16:55:12 -07001035 public void onAdd(ShortcutInfo item) {
1036 mItemsInvalidated = true;
Adam Cohen05e0f402011-08-01 12:12:49 -07001037 // If the item was dropped onto this open folder, we have done the work associated
1038 // with adding the item to the folder, as indicated by mSuppressOnAdd being set
Adam Cohenbfbfd262011-06-13 16:55:12 -07001039 if (mSuppressOnAdd) return;
Sunny Goyalc3a609f2015-02-26 17:43:50 -08001040 mContent.createAndAddShortcutToEnd(item);
Adam Cohenbfbfd262011-06-13 16:55:12 -07001041 LauncherModel.addOrMoveItemInDatabase(
1042 mLauncher, item, mInfo.id, 0, item.cellX, item.cellY);
1043 }
1044
Adam Cohena9cf38f2011-05-02 15:36:58 -07001045 public void onRemove(ShortcutInfo item) {
Adam Cohen7c693212011-05-18 15:26:57 -07001046 mItemsInvalidated = true;
Adam Cohen05e0f402011-08-01 12:12:49 -07001047 // If this item is being dragged from this open folder, we have already handled
1048 // the work associated with removing the item, so we don't have to do anything here.
Adam Cohenbfbfd262011-06-13 16:55:12 -07001049 if (item == mCurrentDragInfo) return;
Adam Cohendf1e4e82011-06-24 15:57:39 -07001050 View v = getViewForInfo(item);
Adam Cohena9cf38f2011-05-02 15:36:58 -07001051 mContent.removeView(v);
Adam Cohen2801caf2011-05-13 20:57:39 -07001052 if (mState == STATE_ANIMATING) {
1053 mRearrangeOnClose = true;
1054 } else {
Sunny Goyalc3a609f2015-02-26 17:43:50 -08001055 rearrangeChildren();
Adam Cohen2801caf2011-05-13 20:57:39 -07001056 }
Adam Cohenafb01ee2011-06-23 15:38:03 -07001057 if (getItemCount() <= 1) {
1058 replaceFolderWithFinalItem();
1059 }
Adam Cohena9cf38f2011-05-02 15:36:58 -07001060 }
Adam Cohen7c693212011-05-18 15:26:57 -07001061
Adam Cohendf1e4e82011-06-24 15:57:39 -07001062 private View getViewForInfo(ShortcutInfo item) {
1063 for (int j = 0; j < mContent.getCountY(); j++) {
1064 for (int i = 0; i < mContent.getCountX(); i++) {
1065 View v = mContent.getChildAt(i, j);
1066 if (v.getTag() == item) {
1067 return v;
1068 }
1069 }
1070 }
1071 return null;
1072 }
1073
Adam Cohen76078c42011-06-09 15:06:52 -07001074 public void onItemsChanged() {
Adam Cohenac56cff2011-09-28 20:45:37 -07001075 updateTextViewFocus();
Adam Cohen76078c42011-06-09 15:06:52 -07001076 }
Adam Cohenac56cff2011-09-28 20:45:37 -07001077
Adam Cohen76fc0852011-06-17 13:26:23 -07001078 public void onTitleChanged(CharSequence title) {
1079 }
Adam Cohen76078c42011-06-09 15:06:52 -07001080
Adam Cohen7c693212011-05-18 15:26:57 -07001081 public ArrayList<View> getItemsInReadingOrder() {
1082 if (mItemsInvalidated) {
1083 mItemsInReadingOrder.clear();
1084 for (int j = 0; j < mContent.getCountY(); j++) {
1085 for (int i = 0; i < mContent.getCountX(); i++) {
1086 View v = mContent.getChildAt(i, j);
1087 if (v != null) {
Adam Cohen7a8b82b2013-05-29 18:41:50 -07001088 mItemsInReadingOrder.add(v);
Adam Cohen7c693212011-05-18 15:26:57 -07001089 }
1090 }
1091 }
1092 mItemsInvalidated = false;
1093 }
1094 return mItemsInReadingOrder;
1095 }
Adam Cohen8dfcba42011-07-07 16:38:18 -07001096
1097 public void getLocationInDragLayer(int[] loc) {
1098 mLauncher.getDragLayer().getLocationInDragLayer(this, loc);
1099 }
Adam Cohenea0818d2011-09-30 20:06:58 -07001100
1101 public void onFocusChange(View v, boolean hasFocus) {
1102 if (v == mFolderName && hasFocus) {
1103 startEditingFolderName();
1104 }
1105 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001106
1107 @Override
1108 public void getHitRectRelativeToDragLayer(Rect outRect) {
1109 getHitRect(outRect);
1110 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001111}