blob: 1e1d1eeb44723763d0f9a746b485c82b8c4b149a [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;
Sunny Goyal48461932015-03-09 17:41:09 -070051
Daniel Sandler325dc232013-06-05 22:57:57 -040052import com.android.launcher3.FolderInfo.FolderListener;
Sunny Goyalbc753352015-03-05 09:40:44 -080053import com.android.launcher3.Workspace.ItemOperator;
Adam Cohen091440a2015-03-18 14:16:05 -070054import com.android.launcher3.util.Thunk;
Romain Guyedcce092010-03-04 13:03:17 -080055
Adam Cohenc0dcf592011-06-01 15:30:43 -070056import java.util.ArrayList;
Adam Cohen3bf84d32012-05-07 20:17:14 -070057import java.util.Collections;
Adam Cohenc0dcf592011-06-01 15:30:43 -070058
The Android Open Source Project31dd5032009-03-03 19:32:27 -080059/**
60 * Represents a set of icons chosen by the user or generated by the system.
61 */
Adam Cohen8dfcba42011-07-07 16:38:18 -070062public class Folder extends LinearLayout implements DragSource, View.OnClickListener,
Adam Cohenea0818d2011-09-30 20:06:58 -070063 View.OnLongClickListener, DropTarget, FolderListener, TextView.OnEditorActionListener,
64 View.OnFocusChangeListener {
Adam Cohendf2cc412011-04-27 16:56:57 -070065 private static final String TAG = "Launcher.Folder";
66
Sunny Goyalc3a609f2015-02-26 17:43:50 -080067 /**
68 * We avoid measuring {@link #mContentWrapper} with a 0 width or height, as this
69 * results in CellLayout being measured as UNSPECIFIED, which it does not support.
70 */
71 private static final int MIN_CONTENT_DIMEN = 5;
Sunny Goyal290800b2015-03-05 11:33:33 -080072 private static final boolean ALLOW_FOLDER_SCROLL = true;
Adam Cohen4eac29a2011-07-11 17:53:37 -070073
Adam Cohendf2cc412011-04-27 16:56:57 -070074 static final int STATE_NONE = -1;
75 static final int STATE_SMALL = 0;
76 static final int STATE_ANIMATING = 1;
77 static final int STATE_OPEN = 2;
78
Sunny Goyal48461932015-03-09 17:41:09 -070079 /**
Sunny Goyal48461932015-03-09 17:41:09 -070080 * Time for which the scroll hint is shown before automatically changing page.
81 */
82 public static final int SCROLL_HINT_DURATION = DragController.SCROLL_DELAY;
83
84 /**
Sunny Goyal5d85c442015-03-10 13:14:47 -070085 * Time in milliseconds for which an icon sticks to the target position
86 * in case of a sorted folder.
87 */
88 private static final int SORTED_STICKY_REORDER_DELAY = 1500;
89
90 /**
Sunny Goyal48461932015-03-09 17:41:09 -070091 * Fraction of icon width which behave as scroll region.
92 */
93 private static final float ICON_OVERSCROLL_WIDTH_FACTOR = 0.45f;
94
Adam Cohenf0f4eda2013-06-06 21:27:03 -070095 private static final int REORDER_DELAY = 250;
Adam Cohen5d518fa2013-12-05 14:16:23 -080096 private static final int ON_EXIT_CLOSE_DELAY = 400;
Sunny Goyalc3a609f2015-02-26 17:43:50 -080097 private static final Rect sTempRect = new Rect();
98
99 private static String sDefaultFolderName;
100 private static String sHintText;
101
102 private final Alarm mReorderAlarm = new Alarm();
103 private final Alarm mOnExitAlarm = new Alarm();
104
Adam Cohen091440a2015-03-18 14:16:05 -0700105 @Thunk final ArrayList<View> mItemsInReadingOrder = new ArrayList<View>();
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800106
107 private final int mExpandDuration;
108 private final int mMaterialExpandDuration;
109 private final int mMaterialExpandStagger;
110
111 private final InputMethodManager mInputMethodManager;
112
113 protected final Launcher mLauncher;
114 protected DragController mDragController;
115 protected FolderInfo mInfo;
116
Adam Cohen091440a2015-03-18 14:16:05 -0700117 @Thunk FolderIcon mFolderIcon;
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800118
Adam Cohen091440a2015-03-18 14:16:05 -0700119 @Thunk FolderContent mContent;
120 @Thunk View mContentWrapper;
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800121 FolderEditText mFolderName;
122
Sunny Goyal290800b2015-03-05 11:33:33 -0800123 private View mFooter;
124 private int mFooterHeight;
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800125
126 // Cell ranks used for drag and drop
Adam Cohen091440a2015-03-18 14:16:05 -0700127 @Thunk int mTargetRank, mPrevTargetRank, mEmptyCellRank;
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800128
Adam Cohen091440a2015-03-18 14:16:05 -0700129 @Thunk int mState = STATE_NONE;
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800130 private boolean mRearrangeOnClose = false;
Adam Cohen7c693212011-05-18 15:26:57 -0700131 boolean mItemsInvalidated = false;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700132 private ShortcutInfo mCurrentDragInfo;
133 private View mCurrentDragView;
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800134 private boolean mIsExternalDrag;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700135 boolean mSuppressOnAdd = false;
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700136 private boolean mDragInProgress = false;
137 private boolean mDeleteFolderOnDropCompleted = false;
138 private boolean mSuppressFolderDeletion = false;
Adam Cohen05e0f402011-08-01 12:12:49 -0700139 private boolean mItemAddedBackToSelfViaIcon = false;
Adam Cohen091440a2015-03-18 14:16:05 -0700140 @Thunk float mFolderIconPivotX;
141 @Thunk float mFolderIconPivotY;
Adam Cohen76fc0852011-06-17 13:26:23 -0700142 private boolean mIsEditingName = false;
Adam Cohen1960ea42013-11-12 11:33:14 +0000143
Adam Cohenfb91f302012-06-11 15:45:18 -0700144 private boolean mDestroyed;
145
Adam Cohen091440a2015-03-18 14:16:05 -0700146 @Thunk Runnable mDeferredAction;
Michael Jurka1e2f4652013-07-08 18:03:46 -0700147 private boolean mDeferDropAfterUninstall;
148 private boolean mUninstallSuccessful;
149
Sunny Goyal48461932015-03-09 17:41:09 -0700150 // Folder scrolling
151 private int mScrollAreaOffset;
152 private Alarm mOnScrollHintAlarm;
Adam Cohen091440a2015-03-18 14:16:05 -0700153 @Thunk Alarm mScrollPauseAlarm;
Sunny Goyal48461932015-03-09 17:41:09 -0700154
155 // TODO: Use {@link #mContent} once {@link #ALLOW_FOLDER_SCROLL} is removed.
Adam Cohen091440a2015-03-18 14:16:05 -0700156 @Thunk FolderPagedView mPagedView;
Sunny Goyal48461932015-03-09 17:41:09 -0700157
Adam Cohen091440a2015-03-18 14:16:05 -0700158 @Thunk int mScrollHintDir = DragController.SCROLL_NONE;
159 @Thunk int mCurrentScrollDir = DragController.SCROLL_NONE;
Sunny Goyal48461932015-03-09 17:41:09 -0700160
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800161 /**
162 * Used to inflate the Workspace from XML.
163 *
164 * @param context The application's context.
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800165 * @param attrs The attributes set containing the Workspace's customization values.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800166 */
167 public Folder(Context context, AttributeSet attrs) {
168 super(context, attrs);
169 setAlwaysDrawnWithCacheEnabled(false);
Adam Cohen76fc0852011-06-17 13:26:23 -0700170 mInputMethodManager = (InputMethodManager)
Michael Jurka8b805b12012-04-18 14:23:14 -0700171 getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
Adam Cohen76fc0852011-06-17 13:26:23 -0700172
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800173 Resources res = getResources();
Adam Cohenc4fe9ea2014-08-18 18:54:10 -0700174 mExpandDuration = res.getInteger(R.integer.config_folderExpandDuration);
175 mMaterialExpandDuration = res.getInteger(R.integer.config_materialFolderExpandDuration);
176 mMaterialExpandStagger = res.getInteger(R.integer.config_materialFolderExpandStagger);
Adam Cohen4ef610f2011-06-21 22:37:36 -0700177
178 if (sDefaultFolderName == null) {
179 sDefaultFolderName = res.getString(R.string.folder_name);
180 }
Adam Cohena65beee2011-06-27 21:32:23 -0700181 if (sHintText == null) {
182 sHintText = res.getString(R.string.folder_hint_text);
183 }
Adam Cohen4eac29a2011-07-11 17:53:37 -0700184 mLauncher = (Launcher) context;
Adam Cohenac56cff2011-09-28 20:45:37 -0700185 // We need this view to be focusable in touch mode so that when text editing of the folder
186 // name is complete, we have something to focus on, thus hiding the cursor and giving
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800187 // reliable behavior when clicking the text field (since it will always gain focus on click).
Adam Cohenac56cff2011-09-28 20:45:37 -0700188 setFocusableInTouchMode(true);
Sunny Goyal48461932015-03-09 17:41:09 -0700189
190 if (ALLOW_FOLDER_SCROLL) {
191 mOnScrollHintAlarm = new Alarm();
192 mScrollPauseAlarm = new Alarm();
193 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800194 }
195
196 @Override
197 protected void onFinishInflate() {
198 super.onFinishInflate();
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800199 mContentWrapper = findViewById(R.id.folder_content_wrapper);
Sunny Goyalbc753352015-03-05 09:40:44 -0800200 mContent = (FolderContent) findViewById(R.id.folder_content);
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800201 mContent.setFolder(this);
Sunny Goyaldcbcc862014-08-12 15:58:36 -0700202
Adam Cohenac56cff2011-09-28 20:45:37 -0700203 mFolderName = (FolderEditText) findViewById(R.id.folder_name);
204 mFolderName.setFolder(this);
Adam Cohenea0818d2011-09-30 20:06:58 -0700205 mFolderName.setOnFocusChangeListener(this);
Adam Cohen76fc0852011-06-17 13:26:23 -0700206
Adam Cohen76fc0852011-06-17 13:26:23 -0700207 // We disable action mode for now since it messes up the view on phones
208 mFolderName.setCustomSelectionActionModeCallback(mActionModeCallback);
Adam Cohen76fc0852011-06-17 13:26:23 -0700209 mFolderName.setOnEditorActionListener(this);
Adam Cohen4ef610f2011-06-21 22:37:36 -0700210 mFolderName.setSelectAllOnFocus(true);
Adam Cohen7a14d0b2011-06-24 11:36:35 -0700211 mFolderName.setInputType(mFolderName.getInputType() |
212 InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS | InputType.TYPE_TEXT_FLAG_CAP_WORDS);
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800213
Sunny Goyal290800b2015-03-05 11:33:33 -0800214 mFooter = ALLOW_FOLDER_SCROLL ? findViewById(R.id.folder_footer) : mFolderName;
215 // We find out how tall footer wants to be (it is set to wrap_content), so that
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800216 // we can allocate the appropriate amount of space for it.
217 int measureSpec = MeasureSpec.UNSPECIFIED;
Sunny Goyal290800b2015-03-05 11:33:33 -0800218 mFooter.measure(measureSpec, measureSpec);
219 mFooterHeight = mFooter.getMeasuredHeight();
Sunny Goyal48461932015-03-09 17:41:09 -0700220
221 if (ALLOW_FOLDER_SCROLL) {
222 mPagedView = (FolderPagedView) mContent;
223 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800224 }
Adam Cohen2801caf2011-05-13 20:57:39 -0700225
Adam Cohen76fc0852011-06-17 13:26:23 -0700226 private ActionMode.Callback mActionModeCallback = new ActionMode.Callback() {
227 public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
228 return false;
229 }
230
231 public boolean onCreateActionMode(ActionMode mode, Menu menu) {
232 return false;
233 }
234
235 public void onDestroyActionMode(ActionMode mode) {
236 }
237
238 public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
239 return false;
240 }
241 };
242
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800243 public void onClick(View v) {
Adam Cohendf2cc412011-04-27 16:56:57 -0700244 Object tag = v.getTag();
245 if (tag instanceof ShortcutInfo) {
Adam Cohenb5fe60c2013-06-06 22:03:51 -0700246 mLauncher.onClick(v);
Adam Cohendf2cc412011-04-27 16:56:57 -0700247 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800248 }
249
250 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -0700251 // Return if global dragging is not enabled
252 if (!mLauncher.isDraggingEnabled()) return true;
253
Adam Cohendf2cc412011-04-27 16:56:57 -0700254 Object tag = v.getTag();
255 if (tag instanceof ShortcutInfo) {
Adam Cohendf2cc412011-04-27 16:56:57 -0700256 ShortcutInfo item = (ShortcutInfo) tag;
257 if (!v.isInTouchMode()) {
258 return false;
259 }
260
Winson Chungb745afb2015-03-02 11:51:23 -0800261 mLauncher.getWorkspace().beginDragShared(v, new Point(), this, false);
Adam Cohen76078c42011-06-09 15:06:52 -0700262
263 mCurrentDragInfo = item;
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800264 mEmptyCellRank = item.rank;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700265 mCurrentDragView = v;
Adam Cohenfc53cd22011-07-20 15:45:11 -0700266
Sunny Goyal290800b2015-03-05 11:33:33 -0800267 mContent.removeItem(mCurrentDragView);
Adam Cohenfc53cd22011-07-20 15:45:11 -0700268 mInfo.remove(mCurrentDragInfo);
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700269 mDragInProgress = true;
Adam Cohen05e0f402011-08-01 12:12:49 -0700270 mItemAddedBackToSelfViaIcon = false;
Adam Cohendf2cc412011-04-27 16:56:57 -0700271 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800272 return true;
273 }
274
Adam Cohen76fc0852011-06-17 13:26:23 -0700275 public boolean isEditingName() {
276 return mIsEditingName;
277 }
278
279 public void startEditingFolderName() {
Adam Cohena65beee2011-06-27 21:32:23 -0700280 mFolderName.setHint("");
Adam Cohen76fc0852011-06-17 13:26:23 -0700281 mIsEditingName = true;
282 }
283
284 public void dismissEditingName() {
285 mInputMethodManager.hideSoftInputFromWindow(getWindowToken(), 0);
286 doneEditingFolderName(true);
287 }
288
289 public void doneEditingFolderName(boolean commit) {
Adam Cohena65beee2011-06-27 21:32:23 -0700290 mFolderName.setHint(sHintText);
Adam Cohen1df26a32011-08-12 16:15:23 -0700291 // Convert to a string here to ensure that no other state associated with the text field
292 // gets saved.
Adam Cohen3371da02011-10-25 21:38:29 -0700293 String newTitle = mFolderName.getText().toString();
294 mInfo.setTitle(newTitle);
Adam Cohen76fc0852011-06-17 13:26:23 -0700295 LauncherModel.updateItemInDatabase(mLauncher, mInfo);
Adam Cohenac56cff2011-09-28 20:45:37 -0700296
Adam Cohen3371da02011-10-25 21:38:29 -0700297 if (commit) {
298 sendCustomAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED,
Michael Jurka8b805b12012-04-18 14:23:14 -0700299 String.format(getContext().getString(R.string.folder_renamed), newTitle));
Adam Cohen3371da02011-10-25 21:38:29 -0700300 }
Adam Cohenac56cff2011-09-28 20:45:37 -0700301 // In order to clear the focus from the text field, we set the focus on ourself. This
302 // ensures that every time the field is clicked, focus is gained, giving reliable behavior.
303 requestFocus();
304
Adam Cohene601a432011-07-26 21:51:30 -0700305 Selection.setSelection((Spannable) mFolderName.getText(), 0, 0);
Adam Cohen76fc0852011-06-17 13:26:23 -0700306 mIsEditingName = false;
307 }
308
309 public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
310 if (actionId == EditorInfo.IME_ACTION_DONE) {
311 dismissEditingName();
312 return true;
313 }
314 return false;
315 }
316
317 public View getEditTextRegion() {
318 return mFolderName;
319 }
320
Adam Cohen0c872ba2011-05-05 10:34:16 -0700321 /**
322 * We need to handle touch events to prevent them from falling through to the workspace below.
323 */
324 @Override
325 public boolean onTouchEvent(MotionEvent ev) {
326 return true;
327 }
328
Joe Onorato00acb122009-08-04 16:04:30 -0400329 public void setDragController(DragController dragController) {
330 mDragController = dragController;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800331 }
332
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800333 public void setFolderIcon(FolderIcon icon) {
Adam Cohen2801caf2011-05-13 20:57:39 -0700334 mFolderIcon = icon;
335 }
336
Adam Cohen3371da02011-10-25 21:38:29 -0700337 @Override
338 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
339 // When the folder gets focus, we don't want to announce the list of items.
340 return true;
341 }
342
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800343 /**
344 * @return the FolderInfo object associated with this folder
345 */
346 FolderInfo getInfo() {
347 return mInfo;
348 }
349
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800350 void bind(FolderInfo info) {
351 mInfo = info;
Adam Cohendf2cc412011-04-27 16:56:57 -0700352 ArrayList<ShortcutInfo> children = info.contents;
Sunny Goyal08f72612015-01-05 13:41:43 -0800353 Collections.sort(children, Utilities.RANK_COMPARATOR);
Sunny Goyal08f72612015-01-05 13:41:43 -0800354
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800355 ArrayList<ShortcutInfo> overflow = mContent.bindItems(children);
Adam Cohen0057bbc2011-08-12 18:30:51 -0700356
Jason Monk4ff73882014-04-24 16:48:07 -0400357 // If our folder has too many items we prune them from the list. This is an issue
Adam Cohenc508b2d2011-06-28 14:41:44 -0700358 // when upgrading from the old Folders implementation which could contain an unlimited
359 // number of items.
360 for (ShortcutInfo item: overflow) {
361 mInfo.remove(item);
362 LauncherModel.deleteItemFromDatabase(mLauncher, item);
363 }
364
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800365 DragLayer.LayoutParams lp = (DragLayer.LayoutParams) getLayoutParams();
366 if (lp == null) {
367 lp = new DragLayer.LayoutParams(0, 0);
368 lp.customPosition = true;
369 setLayoutParams(lp);
370 }
371 centerAboutIcon();
372
Adam Cohen4dbe6d92011-05-18 17:14:15 -0700373 mItemsInvalidated = true;
Adam Cohenac56cff2011-09-28 20:45:37 -0700374 updateTextViewFocus();
Adam Cohena9cf38f2011-05-02 15:36:58 -0700375 mInfo.addListener(this);
Adam Cohen4ef610f2011-06-21 22:37:36 -0700376
Adam Cohenafb01ee2011-06-23 15:38:03 -0700377 if (!sDefaultFolderName.contentEquals(mInfo.title)) {
Adam Cohen4ef610f2011-06-21 22:37:36 -0700378 mFolderName.setText(mInfo.title);
379 } else {
380 mFolderName.setText("");
381 }
Winson Chung33231f52013-12-09 16:57:45 -0800382
383 // In case any children didn't come across during loading, clean up the folder accordingly
384 mFolderIcon.post(new Runnable() {
385 public void run() {
386 if (getItemCount() <= 1) {
387 replaceFolderWithFinalItem();
388 }
389 }
390 });
Adam Cohendf2cc412011-04-27 16:56:57 -0700391 }
392
393 /**
394 * Creates a new UserFolder, inflated from R.layout.user_folder.
395 *
396 * @param context The application's context.
397 *
398 * @return A new UserFolder.
399 */
400 static Folder fromXml(Context context) {
Sunny Goyal290800b2015-03-05 11:33:33 -0800401 return (Folder) LayoutInflater.from(context).inflate(
402 ALLOW_FOLDER_SCROLL ? R.layout.user_folder_scroll : R.layout.user_folder, null);
Adam Cohendf2cc412011-04-27 16:56:57 -0700403 }
404
405 /**
406 * This method is intended to make the UserFolder to be visually identical in size and position
407 * to its associated FolderIcon. This allows for a seamless transition into the expanded state.
408 */
409 private void positionAndSizeAsIcon() {
Adam Cohen3e8f8112011-07-02 18:03:00 -0700410 if (!(getParent() instanceof DragLayer)) return;
Adam Cohen662b5982011-12-13 17:45:21 -0800411 setScaleX(0.8f);
412 setScaleY(0.8f);
413 setAlpha(0f);
Adam Cohendf2cc412011-04-27 16:56:57 -0700414 mState = STATE_SMALL;
415 }
416
Adam Cohenc4fe9ea2014-08-18 18:54:10 -0700417 private void prepareReveal() {
418 setScaleX(1f);
419 setScaleY(1f);
420 setAlpha(1f);
421 mState = STATE_SMALL;
422 }
423
Adam Cohendf2cc412011-04-27 16:56:57 -0700424 public void animateOpen() {
Adam Cohen8e776a62011-06-28 18:10:06 -0700425 if (!(getParent() instanceof DragLayer)) return;
Adam Cohen6441de02011-12-14 14:25:32 -0800426
Sunny Goyal48461932015-03-09 17:41:09 -0700427 if (ALLOW_FOLDER_SCROLL) {
Sunny Goyal5d85c442015-03-10 13:14:47 -0700428 mPagedView.completePendingPageChanges();
429 if (!(mDragInProgress && mPagedView.mIsSorted)) {
430 // Open on the first page.
431 mPagedView.snapToPageImmediately(0);
432 }
Sunny Goyal48461932015-03-09 17:41:09 -0700433 }
434
Adam Cohenc4fe9ea2014-08-18 18:54:10 -0700435 Animator openFolderAnim = null;
436 final Runnable onCompleteRunnable;
Kenny Guyd794a3f2014-09-16 15:17:58 +0100437 if (!Utilities.isLmpOrAbove()) {
Adam Cohenc4fe9ea2014-08-18 18:54:10 -0700438 positionAndSizeAsIcon();
439 centerAboutIcon();
440
441 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1);
442 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.0f);
443 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.0f);
444 final ObjectAnimator oa =
445 LauncherAnimUtils.ofPropertyValuesHolder(this, alpha, scaleX, scaleY);
446 oa.setDuration(mExpandDuration);
447 openFolderAnim = oa;
448
449 setLayerType(LAYER_TYPE_HARDWARE, null);
450 onCompleteRunnable = new Runnable() {
451 @Override
452 public void run() {
453 setLayerType(LAYER_TYPE_NONE, null);
454 }
455 };
456 } else {
457 prepareReveal();
458 centerAboutIcon();
459
460 int width = getPaddingLeft() + getPaddingRight() + mContent.getDesiredWidth();
461 int height = getFolderHeight();
462
463 float transX = - 0.075f * (width / 2 - getPivotX());
464 float transY = - 0.075f * (height / 2 - getPivotY());
465 setTranslationX(transX);
466 setTranslationY(transY);
467 PropertyValuesHolder tx = PropertyValuesHolder.ofFloat("translationX", transX, 0);
468 PropertyValuesHolder ty = PropertyValuesHolder.ofFloat("translationY", transY, 0);
469
470 int rx = (int) Math.max(Math.max(width - getPivotX(), 0), getPivotX());
471 int ry = (int) Math.max(Math.max(height - getPivotY(), 0), getPivotY());
472 float radius = (float) Math.sqrt(rx * rx + ry * ry);
473 AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
474 Animator reveal = LauncherAnimUtils.createCircularReveal(this, (int) getPivotX(),
475 (int) getPivotY(), 0, radius);
476 reveal.setDuration(mMaterialExpandDuration);
477 reveal.setInterpolator(new LogDecelerateInterpolator(100, 0));
478
Sunny Goyalbc753352015-03-05 09:40:44 -0800479 mContentWrapper.setAlpha(0f);
480 Animator iconsAlpha = LauncherAnimUtils.ofFloat(mContentWrapper, "alpha", 0f, 1f);
Adam Cohenc4fe9ea2014-08-18 18:54:10 -0700481 iconsAlpha.setDuration(mMaterialExpandDuration);
482 iconsAlpha.setStartDelay(mMaterialExpandStagger);
483 iconsAlpha.setInterpolator(new AccelerateInterpolator(1.5f));
484
Sunny Goyal290800b2015-03-05 11:33:33 -0800485 mFooter.setAlpha(0f);
486 Animator textAlpha = LauncherAnimUtils.ofFloat(mFooter, "alpha", 0f, 1f);
Adam Cohenc4fe9ea2014-08-18 18:54:10 -0700487 textAlpha.setDuration(mMaterialExpandDuration);
488 textAlpha.setStartDelay(mMaterialExpandStagger);
489 textAlpha.setInterpolator(new AccelerateInterpolator(1.5f));
490
491 Animator drift = LauncherAnimUtils.ofPropertyValuesHolder(this, tx, ty);
492 drift.setDuration(mMaterialExpandDuration);
493 drift.setStartDelay(mMaterialExpandStagger);
494 drift.setInterpolator(new LogDecelerateInterpolator(60, 0));
495
496 anim.play(drift);
497 anim.play(iconsAlpha);
498 anim.play(textAlpha);
499 anim.play(reveal);
500
501 openFolderAnim = anim;
502
Sunny Goyalbc753352015-03-05 09:40:44 -0800503 mContentWrapper.setLayerType(LAYER_TYPE_HARDWARE, null);
Adam Cohenc4fe9ea2014-08-18 18:54:10 -0700504 onCompleteRunnable = new Runnable() {
505 @Override
506 public void run() {
Sunny Goyalbc753352015-03-05 09:40:44 -0800507 mContentWrapper.setLayerType(LAYER_TYPE_NONE, null);
Adam Cohenc4fe9ea2014-08-18 18:54:10 -0700508 }
509 };
510 }
511 openFolderAnim.addListener(new AnimatorListenerAdapter() {
Adam Cohendf2cc412011-04-27 16:56:57 -0700512 @Override
513 public void onAnimationStart(Animator animation) {
Adam Cohen3371da02011-10-25 21:38:29 -0700514 sendCustomAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED,
Sunny Goyalbc753352015-03-05 09:40:44 -0800515 mContent.getAccessibilityDescription());
Adam Cohendf2cc412011-04-27 16:56:57 -0700516 mState = STATE_ANIMATING;
517 }
518 @Override
519 public void onAnimationEnd(Animator animation) {
Adam Cohen2801caf2011-05-13 20:57:39 -0700520 mState = STATE_OPEN;
Adam Cohenc4fe9ea2014-08-18 18:54:10 -0700521
522 if (onCompleteRunnable != null) {
523 onCompleteRunnable.run();
524 }
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800525
Sunny Goyalbc753352015-03-05 09:40:44 -0800526 mContent.setFocusOnFirstChild();
Adam Cohendf2cc412011-04-27 16:56:57 -0700527 }
528 });
Adam Cohenc4fe9ea2014-08-18 18:54:10 -0700529 openFolderAnim.start();
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800530
531 // Make sure the folder picks up the last drag move even if the finger doesn't move.
532 if (mDragController.isDragging()) {
533 mDragController.forceTouchMove();
534 }
Sunny Goyal34b65272015-03-11 16:56:52 -0700535
536 if (ALLOW_FOLDER_SCROLL) {
537 FolderPagedView pages = (FolderPagedView) mContent;
538 pages.verifyVisibleHighResIcons(pages.getNextPage());
539 }
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800540 }
541
542 public void beginExternalDrag(ShortcutInfo item) {
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800543 mCurrentDragInfo = item;
Sunny Goyal5d85c442015-03-10 13:14:47 -0700544 mEmptyCellRank = mContent.allocateRankForNewItem(item);
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800545 mIsExternalDrag = true;
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800546 mDragInProgress = true;
Sunny Goyal5d85c442015-03-10 13:14:47 -0700547 if (ALLOW_FOLDER_SCROLL && mPagedView.mIsSorted) {
548 mScrollPauseAlarm.setOnAlarmListener(null);
549 mScrollPauseAlarm.cancelAlarm();
550 mScrollPauseAlarm.setAlarm(SORTED_STICKY_REORDER_DELAY);
551 }
552
Adam Cohendf2cc412011-04-27 16:56:57 -0700553 }
554
Adam Cohen091440a2015-03-18 14:16:05 -0700555 @Thunk void sendCustomAccessibilityEvent(int type, String text) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700556 AccessibilityManager accessibilityManager = (AccessibilityManager)
557 getContext().getSystemService(Context.ACCESSIBILITY_SERVICE);
558 if (accessibilityManager.isEnabled()) {
Adam Cohen3371da02011-10-25 21:38:29 -0700559 AccessibilityEvent event = AccessibilityEvent.obtain(type);
560 onInitializeAccessibilityEvent(event);
561 event.getText().add(text);
Michael Jurka8b805b12012-04-18 14:23:14 -0700562 accessibilityManager.sendAccessibilityEvent(event);
Adam Cohen3371da02011-10-25 21:38:29 -0700563 }
564 }
565
Adam Cohendf2cc412011-04-27 16:56:57 -0700566 public void animateClosed() {
Adam Cohen8e776a62011-06-28 18:10:06 -0700567 if (!(getParent() instanceof DragLayer)) return;
Adam Cohen662b5982011-12-13 17:45:21 -0800568 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0);
569 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 0.9f);
570 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 0.9f);
Michael Jurka032e6ba2013-04-22 15:08:42 +0200571 final ObjectAnimator oa =
Michael Jurka2ecf9952012-06-18 12:52:28 -0700572 LauncherAnimUtils.ofPropertyValuesHolder(this, alpha, scaleX, scaleY);
Adam Cohendf2cc412011-04-27 16:56:57 -0700573
Adam Cohen2801caf2011-05-13 20:57:39 -0700574 oa.addListener(new AnimatorListenerAdapter() {
Adam Cohendf2cc412011-04-27 16:56:57 -0700575 @Override
576 public void onAnimationEnd(Animator animation) {
Adam Cohen2801caf2011-05-13 20:57:39 -0700577 onCloseComplete();
Michael Jurka0121c3e2012-05-31 08:36:04 -0700578 setLayerType(LAYER_TYPE_NONE, null);
Adam Cohen2801caf2011-05-13 20:57:39 -0700579 mState = STATE_SMALL;
Adam Cohendf2cc412011-04-27 16:56:57 -0700580 }
581 @Override
582 public void onAnimationStart(Animator animation) {
Adam Cohen3371da02011-10-25 21:38:29 -0700583 sendCustomAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED,
Michael Jurka8b805b12012-04-18 14:23:14 -0700584 getContext().getString(R.string.folder_closed));
Adam Cohendf2cc412011-04-27 16:56:57 -0700585 mState = STATE_ANIMATING;
586 }
587 });
Adam Cohen2801caf2011-05-13 20:57:39 -0700588 oa.setDuration(mExpandDuration);
Michael Jurka0121c3e2012-05-31 08:36:04 -0700589 setLayerType(LAYER_TYPE_HARDWARE, null);
Michael Jurkaf1ad6082013-03-13 12:55:46 +0100590 oa.start();
Adam Cohendf2cc412011-04-27 16:56:57 -0700591 }
592
Adam Cohencb3382b2011-05-24 14:07:08 -0700593 public boolean acceptDrop(DragObject d) {
594 final ItemInfo item = (ItemInfo) d.dragInfo;
Adam Cohendf2cc412011-04-27 16:56:57 -0700595 final int itemType = item.itemType;
Adam Cohen2801caf2011-05-13 20:57:39 -0700596 return ((itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION ||
597 itemType == LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT) &&
598 !isFull());
Adam Cohendf2cc412011-04-27 16:56:57 -0700599 }
600
Adam Cohencb3382b2011-05-24 14:07:08 -0700601 public void onDragEnter(DragObject d) {
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800602 mPrevTargetRank = -1;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700603 mOnExitAlarm.cancelAlarm();
Sunny Goyal48461932015-03-09 17:41:09 -0700604 if (ALLOW_FOLDER_SCROLL) {
605 // Get the area offset such that the folder only closes if half the drag icon width
606 // is outside the folder area
607 mScrollAreaOffset = d.dragView.getDragRegionWidth() / 2 - d.xOffset;
608 }
Adam Cohenbfbfd262011-06-13 16:55:12 -0700609 }
610
611 OnAlarmListener mReorderAlarmListener = new OnAlarmListener() {
612 public void onAlarm(Alarm alarm) {
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800613 mContent.realTimeReorder(mEmptyCellRank, mTargetRank);
614 mEmptyCellRank = mTargetRank;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700615 }
616 };
617
Sunny Goyalc46bfef2015-01-05 12:40:08 -0800618 @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
Adam Cohen2374abf2013-04-16 14:56:57 -0700619 public boolean isLayoutRtl() {
620 return (getLayoutDirection() == LAYOUT_DIRECTION_RTL);
621 }
622
Sunny Goyal48461932015-03-09 17:41:09 -0700623 @Override
Adam Cohencb3382b2011-05-24 14:07:08 -0700624 public void onDragOver(DragObject d) {
Sunny Goyal48461932015-03-09 17:41:09 -0700625 onDragOver(d, REORDER_DELAY);
626 }
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700627
Sunny Goyal48461932015-03-09 17:41:09 -0700628 private int getTargetRank(DragObject d, float[] recycle) {
629 recycle = d.getVisualCenter(recycle);
630 return mContent.findNearestArea(
631 (int) recycle[0] - getPaddingLeft(), (int) recycle[1] - getPaddingTop());
632 }
633
Adam Cohen091440a2015-03-18 14:16:05 -0700634 @Thunk void onDragOver(DragObject d, int reorderDelay) {
Sunny Goyal48461932015-03-09 17:41:09 -0700635 if (ALLOW_FOLDER_SCROLL && mScrollPauseAlarm.alarmPending()) {
636 return;
637 }
638 final float[] r = new float[2];
639 mTargetRank = getTargetRank(d, r);
640
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800641 if (mTargetRank != mPrevTargetRank) {
Alan Viverette4cda5b72013-08-28 17:53:41 -0700642 mReorderAlarm.cancelAlarm();
Sunny Goyalc46bfef2015-01-05 12:40:08 -0800643 mReorderAlarm.setOnAlarmListener(mReorderAlarmListener);
644 mReorderAlarm.setAlarm(REORDER_DELAY);
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800645 mPrevTargetRank = mTargetRank;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700646 }
Sunny Goyal48461932015-03-09 17:41:09 -0700647
648 if (!ALLOW_FOLDER_SCROLL) {
649 return;
650 }
651
652 float x = r[0];
653 int currentPage = mPagedView.getNextPage();
Sunny Goyal48461932015-03-09 17:41:09 -0700654
Sunny Goyal3b0883f2015-04-07 09:27:07 -0700655 float cellOverlap = mPagedView.getCurrentCellLayout().getCellWidth()
656 * ICON_OVERSCROLL_WIDTH_FACTOR;
657 boolean isOutsideLeftEdge = x < cellOverlap;
658 boolean isOutsideRightEdge = x > (getWidth() - cellOverlap);
Sunny Goyal48461932015-03-09 17:41:09 -0700659
Sunny Goyal3b0883f2015-04-07 09:27:07 -0700660 if (currentPage > 0 && (mPagedView.rtlLayout ? isOutsideRightEdge : isOutsideLeftEdge)) {
661 showScrollHint(DragController.SCROLL_LEFT, d);
662 } else if (currentPage < (mPagedView.getPageCount() - 1)
663 && (mPagedView.rtlLayout ? isOutsideLeftEdge : isOutsideRightEdge)) {
664 showScrollHint(DragController.SCROLL_RIGHT, d);
Sunny Goyal48461932015-03-09 17:41:09 -0700665 } else {
666 mOnScrollHintAlarm.cancelAlarm();
667 if (mScrollHintDir != DragController.SCROLL_NONE) {
668 mPagedView.clearScrollHint();
669 mScrollHintDir = DragController.SCROLL_NONE;
670 }
671 }
Adam Cohendf2cc412011-04-27 16:56:57 -0700672 }
673
Sunny Goyal3b0883f2015-04-07 09:27:07 -0700674 private void showScrollHint(int direction, DragObject d) {
675 // Show scroll hint on the right
676 if (mScrollHintDir != direction) {
677 mPagedView.showScrollHint(direction);
678 mScrollHintDir = direction;
679 }
680
681 // Set alarm for when the hint is complete
682 if (!mOnScrollHintAlarm.alarmPending() || mCurrentScrollDir != direction) {
683 mCurrentScrollDir = direction;
684 mOnScrollHintAlarm.cancelAlarm();
685 mOnScrollHintAlarm.setOnAlarmListener(new OnScrollHintListener(d));
686 mOnScrollHintAlarm.setAlarm(SCROLL_HINT_DURATION);
687
688 mReorderAlarm.cancelAlarm();
689 mTargetRank = mEmptyCellRank;
690 }
691 }
692
Adam Cohenbfbfd262011-06-13 16:55:12 -0700693 OnAlarmListener mOnExitAlarmListener = new OnAlarmListener() {
694 public void onAlarm(Alarm alarm) {
Adam Cohen3e8f8112011-07-02 18:03:00 -0700695 completeDragExit();
Adam Cohenbfbfd262011-06-13 16:55:12 -0700696 }
697 };
698
Adam Cohen95bb8002011-07-03 23:40:28 -0700699 public void completeDragExit() {
Adam Cohen3e8f8112011-07-02 18:03:00 -0700700 mLauncher.closeFolder();
701 mCurrentDragInfo = null;
702 mCurrentDragView = null;
703 mSuppressOnAdd = false;
704 mRearrangeOnClose = true;
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800705 mIsExternalDrag = false;
Adam Cohen3e8f8112011-07-02 18:03:00 -0700706 }
707
Adam Cohencb3382b2011-05-24 14:07:08 -0700708 public void onDragExit(DragObject d) {
Mindy DelliCarpini53b8d072013-07-03 08:23:13 -0700709 // We only close the folder if this is a true drag exit, ie. not because
710 // a drop has occurred above the folder.
Adam Cohenbfbfd262011-06-13 16:55:12 -0700711 if (!d.dragComplete) {
712 mOnExitAlarm.setOnAlarmListener(mOnExitAlarmListener);
713 mOnExitAlarm.setAlarm(ON_EXIT_CLOSE_DELAY);
714 }
715 mReorderAlarm.cancelAlarm();
Sunny Goyal48461932015-03-09 17:41:09 -0700716
717 if (ALLOW_FOLDER_SCROLL) {
718 mOnScrollHintAlarm.cancelAlarm();
719 mScrollPauseAlarm.cancelAlarm();
720 if (mScrollHintDir != DragController.SCROLL_NONE) {
721 mPagedView.clearScrollHint();
722 mScrollHintDir = DragController.SCROLL_NONE;
723 }
724 }
Adam Cohen2801caf2011-05-13 20:57:39 -0700725 }
726
Michael Jurka1e2f4652013-07-08 18:03:46 -0700727 public void onDropCompleted(final View target, final DragObject d,
728 final boolean isFlingToDelete, final boolean success) {
729 if (mDeferDropAfterUninstall) {
Michael Jurkaf3007582013-08-21 14:33:57 +0200730 Log.d(TAG, "Deferred handling drop because waiting for uninstall.");
Michael Jurka1e2f4652013-07-08 18:03:46 -0700731 mDeferredAction = new Runnable() {
732 public void run() {
733 onDropCompleted(target, d, isFlingToDelete, success);
734 mDeferredAction = null;
735 }
736 };
737 return;
738 }
739
740 boolean beingCalledAfterUninstall = mDeferredAction != null;
741 boolean successfulDrop =
742 success && (!beingCalledAfterUninstall || mUninstallSuccessful);
Winson Chung5f8afe62013-08-12 16:19:28 -0700743
Michael Jurka1e2f4652013-07-08 18:03:46 -0700744 if (successfulDrop) {
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800745 if (mDeleteFolderOnDropCompleted && !mItemAddedBackToSelfViaIcon && target != this) {
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700746 replaceFolderWithFinalItem();
747 }
748 } else {
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800749 rearrangeChildren();
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700750 // The drag failed, we need to return the item to the folder
751 mFolderIcon.onDrop(d);
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700752 }
753
754 if (target != this) {
755 if (mOnExitAlarm.alarmPending()) {
756 mOnExitAlarm.cancelAlarm();
Michael Jurka54554252013-08-01 12:52:23 +0200757 if (!successfulDrop) {
Adam Cohen7a8b82b2013-05-29 18:41:50 -0700758 mSuppressFolderDeletion = true;
759 }
Sunny Goyal5d85c442015-03-10 13:14:47 -0700760 mScrollPauseAlarm.cancelAlarm();
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700761 completeDragExit();
762 }
Adam Cohen9c58d822013-12-13 17:18:10 -0800763 }
764
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700765 mDeleteFolderOnDropCompleted = false;
766 mDragInProgress = false;
Adam Cohen05e0f402011-08-01 12:12:49 -0700767 mItemAddedBackToSelfViaIcon = false;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700768 mCurrentDragInfo = null;
769 mCurrentDragView = null;
770 mSuppressOnAdd = false;
Adam Cohen4045eb72011-10-06 11:44:26 -0700771
772 // Reordering may have occured, and we need to save the new item locations. We do this once
773 // at the end to prevent unnecessary database operations.
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700774 updateItemLocationsInDatabaseBatch();
Adam Cohen4045eb72011-10-06 11:44:26 -0700775 }
776
Michael Jurka1e2f4652013-07-08 18:03:46 -0700777 public void deferCompleteDropAfterUninstallActivity() {
778 mDeferDropAfterUninstall = true;
779 }
780
781 public void onUninstallActivityReturned(boolean success) {
782 mDeferDropAfterUninstall = false;
783 mUninstallSuccessful = success;
784 if (mDeferredAction != null) {
785 mDeferredAction.run();
786 }
787 }
788
Winson Chunga48487a2012-03-20 16:19:37 -0700789 @Override
Winson Chungeeb5bbc2013-11-13 15:47:05 -0800790 public float getIntrinsicIconScaleFactor() {
791 return 1f;
792 }
793
794 @Override
Winson Chung043f2af2012-03-01 16:09:54 -0800795 public boolean supportsFlingToDelete() {
796 return true;
797 }
798
Mathew Inwood1eeb3fc2013-11-25 17:01:34 +0000799 @Override
800 public boolean supportsAppInfoDropTarget() {
801 return false;
802 }
803
804 @Override
805 public boolean supportsDeleteDropTarget() {
806 return true;
807 }
808
Winson Chunga48487a2012-03-20 16:19:37 -0700809 public void onFlingToDelete(DragObject d, int x, int y, PointF vec) {
810 // Do nothing
811 }
812
813 @Override
814 public void onFlingToDeleteCompleted() {
815 // Do nothing
816 }
817
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700818 private void updateItemLocationsInDatabaseBatch() {
819 ArrayList<View> list = getItemsInReadingOrder();
820 ArrayList<ItemInfo> items = new ArrayList<ItemInfo>();
821 for (int i = 0; i < list.size(); i++) {
822 View v = list.get(i);
823 ItemInfo info = (ItemInfo) v.getTag();
Sunny Goyal08f72612015-01-05 13:41:43 -0800824 info.rank = i;
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700825 items.add(info);
826 }
827
828 LauncherModel.moveItemsInDatabase(mLauncher, items, mInfo.id, 0);
829 }
830
Adam Cohene25af792013-06-06 23:08:25 -0700831 public void addItemLocationsInDatabase() {
832 ArrayList<View> list = getItemsInReadingOrder();
833 for (int i = 0; i < list.size(); i++) {
834 View v = list.get(i);
835 ItemInfo info = (ItemInfo) v.getTag();
836 LauncherModel.addItemToDatabase(mLauncher, info, mInfo.id, 0,
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700837 info.cellX, info.cellY);
Adam Cohene25af792013-06-06 23:08:25 -0700838 }
839 }
840
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700841 public void notifyDrop() {
842 if (mDragInProgress) {
Adam Cohen05e0f402011-08-01 12:12:49 -0700843 mItemAddedBackToSelfViaIcon = true;
Adam Cohen76078c42011-06-09 15:06:52 -0700844 }
Adam Cohendf2cc412011-04-27 16:56:57 -0700845 }
846
847 public boolean isDropEnabled() {
848 return true;
849 }
850
Adam Cohen2801caf2011-05-13 20:57:39 -0700851 public boolean isFull() {
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800852 return mContent.isFull();
Adam Cohen2801caf2011-05-13 20:57:39 -0700853 }
854
855 private void centerAboutIcon() {
Adam Cohen8e776a62011-06-28 18:10:06 -0700856 DragLayer.LayoutParams lp = (DragLayer.LayoutParams) getLayoutParams();
Adam Cohen2801caf2011-05-13 20:57:39 -0700857
Winson Chung892c74d2013-08-22 16:15:50 -0700858 DragLayer parent = (DragLayer) mLauncher.findViewById(R.id.drag_layer);
Adam Cohen2801caf2011-05-13 20:57:39 -0700859 int width = getPaddingLeft() + getPaddingRight() + mContent.getDesiredWidth();
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700860 int height = getFolderHeight();
Adam Cohen2801caf2011-05-13 20:57:39 -0700861
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800862 float scale = parent.getDescendantRectRelativeToSelf(mFolderIcon, sTempRect);
Adam Cohen8e776a62011-06-28 18:10:06 -0700863
Winson Chungaf40f202013-09-18 18:26:31 -0700864 LauncherAppState app = LauncherAppState.getInstance();
865 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
866
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800867 int centerX = (int) (sTempRect.left + sTempRect.width() * scale / 2);
868 int centerY = (int) (sTempRect.top + sTempRect.height() * scale / 2);
Adam Cohen2801caf2011-05-13 20:57:39 -0700869 int centeredLeft = centerX - width / 2;
870 int centeredTop = centerY - height / 2;
Winson Chung3057b1c2013-10-10 17:35:35 -0700871 int currentPage = mLauncher.getWorkspace().getNextPage();
Adam Cohen7cc1bc42011-12-13 21:28:43 -0800872 // In case the workspace is scrolling, we need to use the final scroll to compute
873 // the folders bounds.
874 mLauncher.getWorkspace().setFinalScrollForPageChange(currentPage);
Adam Cohen35e7e642011-07-17 14:47:18 -0700875 // We first fetch the currently visible CellLayoutChildren
Adam Cohen7cc1bc42011-12-13 21:28:43 -0800876 CellLayout currentLayout = (CellLayout) mLauncher.getWorkspace().getChildAt(currentPage);
Michael Jurkaa52570f2012-03-20 03:18:20 -0700877 ShortcutAndWidgetContainer boundingLayout = currentLayout.getShortcutsAndWidgets();
Adam Cohen35e7e642011-07-17 14:47:18 -0700878 Rect bounds = new Rect();
879 parent.getDescendantRectRelativeToSelf(boundingLayout, bounds);
Adam Cohen7cc1bc42011-12-13 21:28:43 -0800880 // We reset the workspaces scroll
881 mLauncher.getWorkspace().resetFinalScrollForPageChange(currentPage);
Adam Cohen2801caf2011-05-13 20:57:39 -0700882
Adam Cohen35e7e642011-07-17 14:47:18 -0700883 // We need to bound the folder to the currently visible CellLayoutChildren
884 int left = Math.min(Math.max(bounds.left, centeredLeft),
885 bounds.left + bounds.width() - width);
886 int top = Math.min(Math.max(bounds.top, centeredTop),
887 bounds.top + bounds.height() - height);
Winson Chungaf40f202013-09-18 18:26:31 -0700888 if (grid.isPhone() && (grid.availableWidthPx - width) < grid.iconSizePx) {
889 // Center the folder if it is full (on phones only)
890 left = (grid.availableWidthPx - width) / 2;
891 } else if (width >= bounds.width()) {
892 // If the folder doesn't fit within the bounds, center it about the desired bounds
Adam Cohen35e7e642011-07-17 14:47:18 -0700893 left = bounds.left + (bounds.width() - width) / 2;
Adam Cohen0e4857c2011-06-30 17:05:14 -0700894 }
Adam Cohen35e7e642011-07-17 14:47:18 -0700895 if (height >= bounds.height()) {
896 top = bounds.top + (bounds.height() - height) / 2;
Adam Cohen0e4857c2011-06-30 17:05:14 -0700897 }
Adam Cohen2801caf2011-05-13 20:57:39 -0700898
899 int folderPivotX = width / 2 + (centeredLeft - left);
900 int folderPivotY = height / 2 + (centeredTop - top);
901 setPivotX(folderPivotX);
902 setPivotY(folderPivotY);
Adam Cohen268c4752012-06-06 17:47:33 -0700903 mFolderIconPivotX = (int) (mFolderIcon.getMeasuredWidth() *
Adam Cohen2801caf2011-05-13 20:57:39 -0700904 (1.0f * folderPivotX / width));
Adam Cohen268c4752012-06-06 17:47:33 -0700905 mFolderIconPivotY = (int) (mFolderIcon.getMeasuredHeight() *
Adam Cohen2801caf2011-05-13 20:57:39 -0700906 (1.0f * folderPivotY / height));
Adam Cohen3bf84d32012-05-07 20:17:14 -0700907
Adam Cohen662b5982011-12-13 17:45:21 -0800908 lp.width = width;
909 lp.height = height;
910 lp.x = left;
911 lp.y = top;
Adam Cohen2801caf2011-05-13 20:57:39 -0700912 }
913
Adam Cohen268c4752012-06-06 17:47:33 -0700914 float getPivotXForIconAnimation() {
915 return mFolderIconPivotX;
916 }
917 float getPivotYForIconAnimation() {
918 return mFolderIconPivotY;
919 }
920
Winson Chung892c74d2013-08-22 16:15:50 -0700921 private int getContentAreaHeight() {
922 LauncherAppState app = LauncherAppState.getInstance();
923 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
924 Rect workspacePadding = grid.getWorkspacePadding(grid.isLandscape ?
925 CellLayout.LANDSCAPE : CellLayout.PORTRAIT);
926 int maxContentAreaHeight = grid.availableHeightPx -
Winson Chung892c74d2013-08-22 16:15:50 -0700927 workspacePadding.top - workspacePadding.bottom -
Sunny Goyal290800b2015-03-05 11:33:33 -0800928 mFooterHeight;
Adam Cohen1960ea42013-11-12 11:33:14 +0000929 int height = Math.min(maxContentAreaHeight,
Winson Chung892c74d2013-08-22 16:15:50 -0700930 mContent.getDesiredHeight());
Adam Cohen1960ea42013-11-12 11:33:14 +0000931 return Math.max(height, MIN_CONTENT_DIMEN);
932 }
933
934 private int getContentAreaWidth() {
935 return Math.max(mContent.getDesiredWidth(), MIN_CONTENT_DIMEN);
Winson Chung892c74d2013-08-22 16:15:50 -0700936 }
937
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700938 private int getFolderHeight() {
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800939 return getFolderHeight(getContentAreaHeight());
940 }
941
942 private int getFolderHeight(int contentAreaHeight) {
Sunny Goyal290800b2015-03-05 11:33:33 -0800943 return getPaddingTop() + getPaddingBottom() + contentAreaHeight + mFooterHeight;
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700944 }
Adam Cohen234c4cd2011-07-17 21:03:04 -0700945
Adam Cohenf0f4eda2013-06-06 21:27:03 -0700946 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800947 int contentWidth = getContentAreaWidth();
948 int contentHeight = getContentAreaHeight();
Adam Cohen1960ea42013-11-12 11:33:14 +0000949
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800950 int contentAreaWidthSpec = MeasureSpec.makeMeasureSpec(contentWidth, MeasureSpec.EXACTLY);
951 int contentAreaHeightSpec = MeasureSpec.makeMeasureSpec(contentHeight, MeasureSpec.EXACTLY);
952
953 mContent.setFixedSize(contentWidth, contentHeight);
954 mContentWrapper.measure(contentAreaWidthSpec, contentAreaHeightSpec);
Sunny Goyal290800b2015-03-05 11:33:33 -0800955 mFooter.measure(contentAreaWidthSpec,
956 MeasureSpec.makeMeasureSpec(mFooterHeight, MeasureSpec.EXACTLY));
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800957
958 int folderWidth = getPaddingLeft() + getPaddingRight() + contentWidth;
959 int folderHeight = getFolderHeight(contentHeight);
960 setMeasuredDimension(folderWidth, folderHeight);
Adam Cohen234c4cd2011-07-17 21:03:04 -0700961 }
962
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800963 /**
964 * Rearranges the children based on their rank.
965 */
966 public void rearrangeChildren() {
967 rearrangeChildren(-1);
968 }
Adam Cohen2801caf2011-05-13 20:57:39 -0700969
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800970 /**
971 * Rearranges the children based on their rank.
972 * @param itemCount if greater than the total children count, empty spaces are left at the end,
973 * otherwise it is ignored.
974 */
975 public void rearrangeChildren(int itemCount) {
976 ArrayList<View> views = getItemsInReadingOrder();
977 mContent.arrangeChildren(views, Math.max(itemCount, views.size()));
Adam Cohen7c693212011-05-18 15:26:57 -0700978 mItemsInvalidated = true;
Adam Cohen2801caf2011-05-13 20:57:39 -0700979 }
980
Sunny Goyalc4918352015-03-10 18:15:48 -0700981 // TODO remove this once GSA code fix is submitted
982 public ViewGroup getContent() {
983 return (ViewGroup) mContent;
984 }
985
Adam Cohena9cf38f2011-05-02 15:36:58 -0700986 public int getItemCount() {
Sunny Goyalc3a609f2015-02-26 17:43:50 -0800987 return mContent.getItemCount();
Adam Cohena9cf38f2011-05-02 15:36:58 -0700988 }
989
Adam Cohen091440a2015-03-18 14:16:05 -0700990 @Thunk void onCloseComplete() {
Adam Cohen05e0f402011-08-01 12:12:49 -0700991 DragLayer parent = (DragLayer) getParent();
Michael Jurka5649c282012-06-18 10:33:21 -0700992 if (parent != null) {
993 parent.removeView(this);
994 }
Adam Cohen4554ee12011-08-03 16:13:21 -0700995 mDragController.removeDropTarget((DropTarget) this);
Adam Cohen05e0f402011-08-01 12:12:49 -0700996 clearFocus();
Adam Cohenac56cff2011-09-28 20:45:37 -0700997 mFolderIcon.requestFocus();
Adam Cohen05e0f402011-08-01 12:12:49 -0700998
Adam Cohen2801caf2011-05-13 20:57:39 -0700999 if (mRearrangeOnClose) {
Sunny Goyalc3a609f2015-02-26 17:43:50 -08001000 rearrangeChildren();
Adam Cohen2801caf2011-05-13 20:57:39 -07001001 mRearrangeOnClose = false;
1002 }
Adam Cohenafb01ee2011-06-23 15:38:03 -07001003 if (getItemCount() <= 1) {
Adam Cohen67bd9cc2011-07-29 14:07:04 -07001004 if (!mDragInProgress && !mSuppressFolderDeletion) {
1005 replaceFolderWithFinalItem();
1006 } else if (mDragInProgress) {
1007 mDeleteFolderOnDropCompleted = true;
1008 }
Adam Cohenafb01ee2011-06-23 15:38:03 -07001009 }
Adam Cohen67bd9cc2011-07-29 14:07:04 -07001010 mSuppressFolderDeletion = false;
Adam Cohenafb01ee2011-06-23 15:38:03 -07001011 }
1012
Adam Cohen091440a2015-03-18 14:16:05 -07001013 @Thunk void replaceFolderWithFinalItem() {
Adam Cohenafb01ee2011-06-23 15:38:03 -07001014 // Add the last remaining child to the workspace in place of the folder
Adam Cohenfb91f302012-06-11 15:45:18 -07001015 Runnable onCompleteRunnable = new Runnable() {
1016 @Override
1017 public void run() {
Adam Cohendcd297f2013-06-18 13:13:40 -07001018 CellLayout cellLayout = mLauncher.getCellLayout(mInfo.container, mInfo.screenId);
Adam Cohenafb01ee2011-06-23 15:38:03 -07001019
Winson Chung33231f52013-12-09 16:57:45 -08001020 View child = null;
Adam Cohenfb91f302012-06-11 15:45:18 -07001021 // Move the item from the folder to the workspace, in the position of the folder
1022 if (getItemCount() == 1) {
1023 ShortcutInfo finalItem = mInfo.contents.get(0);
Adam Cohenc5e63f32012-07-12 16:16:57 -07001024 child = mLauncher.createShortcut(R.layout.application, cellLayout,
Adam Cohenfb91f302012-06-11 15:45:18 -07001025 finalItem);
1026 LauncherModel.addOrMoveItemInDatabase(mLauncher, finalItem, mInfo.container,
Adam Cohendcd297f2013-06-18 13:13:40 -07001027 mInfo.screenId, mInfo.cellX, mInfo.cellY);
Adam Cohenfb91f302012-06-11 15:45:18 -07001028 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07001029 if (getItemCount() <= 1) {
1030 // Remove the folder
1031 LauncherModel.deleteItemFromDatabase(mLauncher, mInfo);
Dan Sandler0eb687f2014-01-10 13:24:55 -05001032 if (cellLayout != null) {
1033 // b/12446428 -- sometimes the cell layout has already gone away?
1034 cellLayout.removeView(mFolderIcon);
1035 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07001036 if (mFolderIcon instanceof DropTarget) {
1037 mDragController.removeDropTarget((DropTarget) mFolderIcon);
1038 }
1039 mLauncher.removeFolder(mInfo);
1040 }
Adam Cohenc5e63f32012-07-12 16:16:57 -07001041 // We add the child after removing the folder to prevent both from existing at
Winson Chung0e6a7132013-08-23 12:55:10 -07001042 // the same time in the CellLayout. We need to add the new item with addInScreenFromBind()
1043 // to ensure that hotseat items are placed correctly.
Adam Cohenc5e63f32012-07-12 16:16:57 -07001044 if (child != null) {
Winson Chung0e6a7132013-08-23 12:55:10 -07001045 mLauncher.getWorkspace().addInScreenFromBind(child, mInfo.container, mInfo.screenId,
Adam Cohenc5e63f32012-07-12 16:16:57 -07001046 mInfo.cellX, mInfo.cellY, mInfo.spanX, mInfo.spanY);
1047 }
Adam Cohenfb91f302012-06-11 15:45:18 -07001048 }
1049 };
Sunny Goyalbc753352015-03-05 09:40:44 -08001050 View finalChild = mContent.getLastItem();
Adam Cohenfb91f302012-06-11 15:45:18 -07001051 if (finalChild != null) {
1052 mFolderIcon.performDestroyAnimation(finalChild, onCompleteRunnable);
Winson Chung33231f52013-12-09 16:57:45 -08001053 } else {
1054 onCompleteRunnable.run();
Adam Cohenafb01ee2011-06-23 15:38:03 -07001055 }
Adam Cohenfb91f302012-06-11 15:45:18 -07001056 mDestroyed = true;
1057 }
1058
1059 boolean isDestroyed() {
1060 return mDestroyed;
Adam Cohen2801caf2011-05-13 20:57:39 -07001061 }
1062
Adam Cohenac56cff2011-09-28 20:45:37 -07001063 // This method keeps track of the last item in the folder for the purposes
1064 // of keyboard focus
Sunny Goyal290800b2015-03-05 11:33:33 -08001065 public void updateTextViewFocus() {
Sunny Goyalbc753352015-03-05 09:40:44 -08001066 View lastChild = mContent.getLastItem();
Adam Cohenac56cff2011-09-28 20:45:37 -07001067 if (lastChild != null) {
1068 mFolderName.setNextFocusDownId(lastChild.getId());
1069 mFolderName.setNextFocusRightId(lastChild.getId());
1070 mFolderName.setNextFocusLeftId(lastChild.getId());
1071 mFolderName.setNextFocusUpId(lastChild.getId());
1072 }
1073 }
1074
Adam Cohenbfbfd262011-06-13 16:55:12 -07001075 public void onDrop(DragObject d) {
Jorim Jaggi55bd9722014-01-16 15:30:42 -08001076 Runnable cleanUpRunnable = null;
1077
Adam Cohen689ff162014-05-08 17:27:56 -07001078 // If we are coming from All Apps space, we defer removing the extra empty screen
1079 // until the folder closes
Jorim Jaggi55bd9722014-01-16 15:30:42 -08001080 if (d.dragSource != mLauncher.getWorkspace() && !(d.dragSource instanceof Folder)) {
1081 cleanUpRunnable = new Runnable() {
1082 @Override
1083 public void run() {
Adam Cohen689ff162014-05-08 17:27:56 -07001084 mLauncher.exitSpringLoadedDragModeDelayed(true,
1085 Launcher.EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT,
1086 null);
Jorim Jaggi55bd9722014-01-16 15:30:42 -08001087 }
1088 };
Adam Cohenbfbfd262011-06-13 16:55:12 -07001089 }
Jorim Jaggi55bd9722014-01-16 15:30:42 -08001090
Sunny Goyal48461932015-03-09 17:41:09 -07001091 if (ALLOW_FOLDER_SCROLL) {
1092 // If the icon was dropped while the page was being scrolled, we need to compute
1093 // the target location again such that the icon is placed of the final page.
1094 if (!mPagedView.rankOnCurrentPage(mEmptyCellRank)) {
1095 // Reorder again.
1096 mTargetRank = getTargetRank(d, null);
1097
1098 // Rearrange items immediately.
1099 mReorderAlarmListener.onAlarm(mReorderAlarm);
1100
1101 mOnScrollHintAlarm.cancelAlarm();
1102 mScrollPauseAlarm.cancelAlarm();
1103 }
1104 mPagedView.completePendingPageChanges();
1105 }
1106
Jorim Jaggi55bd9722014-01-16 15:30:42 -08001107 View currentDragView;
1108 ShortcutInfo si = mCurrentDragInfo;
1109 if (mIsExternalDrag) {
Sunny Goyalc3a609f2015-02-26 17:43:50 -08001110 currentDragView = mContent.createAndAddViewForRank(si, mEmptyCellRank);
Sunny Goyal95abbb32014-08-04 10:53:22 -07001111 // Actually move the item in the database if it was an external drag. Call this
1112 // before creating the view, so that ShortcutInfo is updated appropriately.
1113 LauncherModel.addOrMoveItemInDatabase(
1114 mLauncher, si, mInfo.id, 0, si.cellX, si.cellY);
1115
1116 // We only need to update the locations if it doesn't get handled in #onDropCompleted.
1117 if (d.dragSource != this) {
1118 updateItemLocationsInDatabaseBatch();
1119 }
1120 mIsExternalDrag = false;
Jorim Jaggi55bd9722014-01-16 15:30:42 -08001121 } else {
1122 currentDragView = mCurrentDragView;
Sunny Goyalc3a609f2015-02-26 17:43:50 -08001123 mContent.addViewForRank(currentDragView, si, mEmptyCellRank);
Adam Cohenbfbfd262011-06-13 16:55:12 -07001124 }
Jorim Jaggi55bd9722014-01-16 15:30:42 -08001125
1126 if (d.dragView.hasDrawn()) {
1127
1128 // Temporarily reset the scale such that the animation target gets calculated correctly.
1129 float scaleX = getScaleX();
1130 float scaleY = getScaleY();
1131 setScaleX(1.0f);
1132 setScaleY(1.0f);
1133 mLauncher.getDragLayer().animateViewIntoPosition(d.dragView, currentDragView,
1134 cleanUpRunnable, null);
1135 setScaleX(scaleX);
1136 setScaleY(scaleY);
1137 } else {
1138 d.deferDragViewCleanupPostAnimation = false;
1139 currentDragView.setVisibility(VISIBLE);
1140 }
1141 mItemsInvalidated = true;
Sunny Goyalc3a609f2015-02-26 17:43:50 -08001142 rearrangeChildren();
Jorim Jaggi55bd9722014-01-16 15:30:42 -08001143
Jorim Jaggi55bd9722014-01-16 15:30:42 -08001144 // Temporarily suppress the listener, as we did all the work already here.
1145 mSuppressOnAdd = true;
1146 mInfo.add(si);
1147 mSuppressOnAdd = false;
Sunny Goyal4b020172014-08-28 14:51:14 -07001148 // Clear the drag info, as it is no longer being dragged.
1149 mCurrentDragInfo = null;
Adam Cohenbfbfd262011-06-13 16:55:12 -07001150 }
1151
Adam Cohen7a8b82b2013-05-29 18:41:50 -07001152 // This is used so the item doesn't immediately appear in the folder when added. In one case
1153 // we need to create the illusion that the item isn't added back to the folder yet, to
1154 // to correspond to the animation of the icon back into the folder. This is
1155 public void hideItem(ShortcutInfo info) {
1156 View v = getViewForInfo(info);
1157 v.setVisibility(INVISIBLE);
1158 }
1159 public void showItem(ShortcutInfo info) {
1160 View v = getViewForInfo(info);
1161 v.setVisibility(VISIBLE);
1162 }
1163
Adam Cohenbfbfd262011-06-13 16:55:12 -07001164 public void onAdd(ShortcutInfo item) {
Adam Cohen05e0f402011-08-01 12:12:49 -07001165 // If the item was dropped onto this open folder, we have done the work associated
1166 // with adding the item to the folder, as indicated by mSuppressOnAdd being set
Adam Cohenbfbfd262011-06-13 16:55:12 -07001167 if (mSuppressOnAdd) return;
Sunny Goyal5d85c442015-03-10 13:14:47 -07001168 mContent.createAndAddViewForRank(item, mContent.allocateRankForNewItem(item));
Sunny Goyal2e688a82015-03-18 10:23:39 -07001169 mItemsInvalidated = true;
Adam Cohenbfbfd262011-06-13 16:55:12 -07001170 LauncherModel.addOrMoveItemInDatabase(
1171 mLauncher, item, mInfo.id, 0, item.cellX, item.cellY);
1172 }
1173
Adam Cohena9cf38f2011-05-02 15:36:58 -07001174 public void onRemove(ShortcutInfo item) {
Adam Cohen7c693212011-05-18 15:26:57 -07001175 mItemsInvalidated = true;
Adam Cohen05e0f402011-08-01 12:12:49 -07001176 // If this item is being dragged from this open folder, we have already handled
1177 // the work associated with removing the item, so we don't have to do anything here.
Adam Cohenbfbfd262011-06-13 16:55:12 -07001178 if (item == mCurrentDragInfo) return;
Adam Cohendf1e4e82011-06-24 15:57:39 -07001179 View v = getViewForInfo(item);
Sunny Goyal290800b2015-03-05 11:33:33 -08001180 mContent.removeItem(v);
Adam Cohen2801caf2011-05-13 20:57:39 -07001181 if (mState == STATE_ANIMATING) {
1182 mRearrangeOnClose = true;
1183 } else {
Sunny Goyalc3a609f2015-02-26 17:43:50 -08001184 rearrangeChildren();
Adam Cohen2801caf2011-05-13 20:57:39 -07001185 }
Adam Cohenafb01ee2011-06-23 15:38:03 -07001186 if (getItemCount() <= 1) {
1187 replaceFolderWithFinalItem();
1188 }
Adam Cohena9cf38f2011-05-02 15:36:58 -07001189 }
Adam Cohen7c693212011-05-18 15:26:57 -07001190
Sunny Goyalbc753352015-03-05 09:40:44 -08001191 private View getViewForInfo(final ShortcutInfo item) {
1192 return mContent.iterateOverItems(new ItemOperator() {
1193
1194 @Override
1195 public boolean evaluate(ItemInfo info, View view, View parent) {
1196 return info == item;
Adam Cohendf1e4e82011-06-24 15:57:39 -07001197 }
Sunny Goyalbc753352015-03-05 09:40:44 -08001198 });
Adam Cohendf1e4e82011-06-24 15:57:39 -07001199 }
1200
Adam Cohen76078c42011-06-09 15:06:52 -07001201 public void onItemsChanged() {
Adam Cohenac56cff2011-09-28 20:45:37 -07001202 updateTextViewFocus();
Adam Cohen76078c42011-06-09 15:06:52 -07001203 }
Adam Cohenac56cff2011-09-28 20:45:37 -07001204
Adam Cohen76fc0852011-06-17 13:26:23 -07001205 public void onTitleChanged(CharSequence title) {
1206 }
Adam Cohen76078c42011-06-09 15:06:52 -07001207
Adam Cohen7c693212011-05-18 15:26:57 -07001208 public ArrayList<View> getItemsInReadingOrder() {
1209 if (mItemsInvalidated) {
1210 mItemsInReadingOrder.clear();
Sunny Goyalbc753352015-03-05 09:40:44 -08001211 mContent.iterateOverItems(new ItemOperator() {
1212
1213 @Override
1214 public boolean evaluate(ItemInfo info, View view, View parent) {
1215 mItemsInReadingOrder.add(view);
1216 return false;
Adam Cohen7c693212011-05-18 15:26:57 -07001217 }
Sunny Goyalbc753352015-03-05 09:40:44 -08001218 });
Adam Cohen7c693212011-05-18 15:26:57 -07001219 mItemsInvalidated = false;
1220 }
1221 return mItemsInReadingOrder;
1222 }
Adam Cohen8dfcba42011-07-07 16:38:18 -07001223
1224 public void getLocationInDragLayer(int[] loc) {
1225 mLauncher.getDragLayer().getLocationInDragLayer(this, loc);
1226 }
Adam Cohenea0818d2011-09-30 20:06:58 -07001227
1228 public void onFocusChange(View v, boolean hasFocus) {
1229 if (v == mFolderName && hasFocus) {
1230 startEditingFolderName();
1231 }
1232 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001233
1234 @Override
1235 public void getHitRectRelativeToDragLayer(Rect outRect) {
1236 getHitRect(outRect);
Sunny Goyal48461932015-03-09 17:41:09 -07001237 outRect.left -= mScrollAreaOffset;
1238 outRect.right += mScrollAreaOffset;
1239 }
1240
1241 private class OnScrollHintListener implements OnAlarmListener {
1242
1243 private final DragObject mDragObject;
1244
1245 OnScrollHintListener(DragObject object) {
1246 mDragObject = object;
1247 }
1248
1249 /**
1250 * Scroll hint has been shown long enough. Now scroll to appropriate page.
1251 */
1252 @Override
1253 public void onAlarm(Alarm alarm) {
1254 if (mCurrentScrollDir == DragController.SCROLL_LEFT) {
1255 mPagedView.scrollLeft();
1256 mScrollHintDir = DragController.SCROLL_NONE;
1257 } else if (mCurrentScrollDir == DragController.SCROLL_RIGHT) {
1258 mPagedView.scrollRight();
1259 mScrollHintDir = DragController.SCROLL_NONE;
1260 } else {
1261 // This should not happen
1262 return;
1263 }
1264 mCurrentScrollDir = DragController.SCROLL_NONE;
1265
1266 // Pause drag event until the scrolling is finished
1267 mScrollPauseAlarm.setOnAlarmListener(new OnScrollFinishedListener(mDragObject));
1268 mScrollPauseAlarm.setAlarm(DragController.RESCROLL_DELAY);
1269 }
1270 }
1271
1272 private class OnScrollFinishedListener implements OnAlarmListener {
1273
1274 private final DragObject mDragObject;
1275
1276 OnScrollFinishedListener(DragObject object) {
1277 mDragObject = object;
1278 }
1279
1280 /**
1281 * Page scroll is complete.
1282 */
1283 @Override
1284 public void onAlarm(Alarm alarm) {
1285 // Reorder immediately on page change.
1286 onDragOver(mDragObject, 1);
1287 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001288 }
Sunny Goyalbc753352015-03-05 09:40:44 -08001289
1290 public static interface FolderContent {
1291 void setFolder(Folder f);
1292
Sunny Goyal290800b2015-03-05 11:33:33 -08001293 void removeItem(View v);
Sunny Goyalbc753352015-03-05 09:40:44 -08001294
1295 boolean isFull();
1296 int getItemCount();
1297
1298 int getDesiredWidth();
1299 int getDesiredHeight();
1300 void setFixedSize(int width, int height);
1301
1302 /**
1303 * Iterates over all its items in a reading order.
1304 * @return the view for which the operator returned true.
1305 */
1306 View iterateOverItems(ItemOperator op);
1307 View getLastItem();
1308
1309 String getAccessibilityDescription();
1310
1311 /**
1312 * Binds items to the layout.
1313 * @return list of items that could not be bound, probably because we hit the max size limit.
1314 */
1315 ArrayList<ShortcutInfo> bindItems(ArrayList<ShortcutInfo> children);
1316
1317 /**
Sunny Goyal5d85c442015-03-10 13:14:47 -07001318 * Create space for a new item, and returns the rank for that item.
Sunny Goyalbc753352015-03-05 09:40:44 -08001319 * Resizes the content if necessary.
1320 */
Sunny Goyal5d85c442015-03-10 13:14:47 -07001321 int allocateRankForNewItem(ShortcutInfo info);
Sunny Goyalbc753352015-03-05 09:40:44 -08001322
1323 View createAndAddViewForRank(ShortcutInfo item, int rank);
1324
1325 /**
1326 * Adds the {@param view} to the layout based on {@param rank} and updated the position
1327 * related attributes. It assumes that {@param item} is already attached to the view.
1328 */
1329 void addViewForRank(View view, ShortcutInfo item, int rank);
1330
1331 /**
1332 * Reorders the items such that the {@param empty} spot moves to {@param target}
1333 */
1334 void realTimeReorder(int empty, int target);
1335
1336 /**
1337 * @return the rank of the cell nearest to the provided pixel position.
1338 */
1339 int findNearestArea(int pixelX, int pixelY);
1340
1341 /**
1342 * Updates position and rank of all the children in the view based.
1343 * @param list the ordered list of children.
1344 * @param itemCount if greater than the total children count, empty spaces are left
1345 * at the end.
1346 */
1347 void arrangeChildren(ArrayList<View> list, int itemCount);
1348
1349 /**
1350 * Sets the focus on the first visible child.
1351 */
1352 void setFocusOnFirstChild();
1353 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001354}