• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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 
17 package com.android.launcher3.folder;
18 
19 import static android.text.TextUtils.isEmpty;
20 
21 import static com.android.launcher3.LauncherAnimUtils.SPRING_LOADED_EXIT_DELAY;
22 import static com.android.launcher3.LauncherState.NORMAL;
23 import static com.android.launcher3.compat.AccessibilityManagerCompat.sendCustomAccessibilityEvent;
24 import static com.android.launcher3.config.FeatureFlags.ALWAYS_USE_HARDWARE_OPTIMIZATION_FOR_FOLDER_ANIMATIONS;
25 import static com.android.launcher3.logging.StatsLogManager.LauncherEvent.LAUNCHER_FOLDER_LABEL_UPDATED;
26 import static com.android.launcher3.logging.StatsLogManager.LauncherEvent.LAUNCHER_ITEM_DROP_COMPLETED;
27 import static com.android.launcher3.util.DisplayController.getSingleFrameMs;
28 
29 import android.animation.Animator;
30 import android.animation.AnimatorListenerAdapter;
31 import android.animation.AnimatorSet;
32 import android.annotation.SuppressLint;
33 import android.appwidget.AppWidgetHostView;
34 import android.content.Context;
35 import android.graphics.Canvas;
36 import android.graphics.Insets;
37 import android.graphics.Path;
38 import android.graphics.Rect;
39 import android.graphics.drawable.Drawable;
40 import android.graphics.drawable.GradientDrawable;
41 import android.text.InputType;
42 import android.text.Selection;
43 import android.text.TextUtils;
44 import android.util.AttributeSet;
45 import android.util.Log;
46 import android.util.Pair;
47 import android.util.TypedValue;
48 import android.view.FocusFinder;
49 import android.view.KeyEvent;
50 import android.view.LayoutInflater;
51 import android.view.MotionEvent;
52 import android.view.View;
53 import android.view.ViewDebug;
54 import android.view.WindowInsets;
55 import android.view.accessibility.AccessibilityEvent;
56 import android.view.animation.AnimationUtils;
57 import android.view.inputmethod.EditorInfo;
58 import android.widget.TextView;
59 
60 import androidx.annotation.Nullable;
61 import androidx.core.content.res.ResourcesCompat;
62 
63 import com.android.launcher3.AbstractFloatingView;
64 import com.android.launcher3.Alarm;
65 import com.android.launcher3.BubbleTextView;
66 import com.android.launcher3.CellLayout;
67 import com.android.launcher3.DeviceProfile;
68 import com.android.launcher3.DragSource;
69 import com.android.launcher3.DropTarget;
70 import com.android.launcher3.ExtendedEditText;
71 import com.android.launcher3.Launcher;
72 import com.android.launcher3.LauncherSettings;
73 import com.android.launcher3.OnAlarmListener;
74 import com.android.launcher3.PagedView;
75 import com.android.launcher3.R;
76 import com.android.launcher3.ShortcutAndWidgetContainer;
77 import com.android.launcher3.Utilities;
78 import com.android.launcher3.Workspace.ItemOperator;
79 import com.android.launcher3.accessibility.AccessibleDragListenerAdapter;
80 import com.android.launcher3.accessibility.FolderAccessibilityHelper;
81 import com.android.launcher3.anim.KeyboardInsetAnimationCallback;
82 import com.android.launcher3.config.FeatureFlags;
83 import com.android.launcher3.dragndrop.DragController;
84 import com.android.launcher3.dragndrop.DragController.DragListener;
85 import com.android.launcher3.dragndrop.DragOptions;
86 import com.android.launcher3.logger.LauncherAtom.FromState;
87 import com.android.launcher3.logger.LauncherAtom.ToState;
88 import com.android.launcher3.logging.StatsLogManager;
89 import com.android.launcher3.logging.StatsLogManager.StatsLogger;
90 import com.android.launcher3.model.data.AppInfo;
91 import com.android.launcher3.model.data.FolderInfo;
92 import com.android.launcher3.model.data.FolderInfo.FolderListener;
93 import com.android.launcher3.model.data.ItemInfo;
94 import com.android.launcher3.model.data.WorkspaceItemInfo;
95 import com.android.launcher3.pageindicators.PageIndicatorDots;
96 import com.android.launcher3.util.Executors;
97 import com.android.launcher3.util.Thunk;
98 import com.android.launcher3.views.ActivityContext;
99 import com.android.launcher3.views.BaseDragLayer;
100 import com.android.launcher3.views.ClipPathView;
101 import com.android.launcher3.widget.PendingAddShortcutInfo;
102 
103 import java.util.ArrayList;
104 import java.util.Collections;
105 import java.util.Comparator;
106 import java.util.List;
107 import java.util.Objects;
108 import java.util.StringJoiner;
109 import java.util.stream.Collectors;
110 import java.util.stream.Stream;
111 
112 /**
113  * Represents a set of icons chosen by the user or generated by the system.
114  */
115 public class Folder extends AbstractFloatingView implements ClipPathView, DragSource,
116         View.OnLongClickListener, DropTarget, FolderListener, TextView.OnEditorActionListener,
117         View.OnFocusChangeListener, DragListener, ExtendedEditText.OnBackKeyListener {
118     private static final String TAG = "Launcher.Folder";
119     private static final boolean DEBUG = false;
120 
121     /**
122      * Used for separating folder title when logging together.
123      */
124     private static final CharSequence FOLDER_LABEL_DELIMITER = "~";
125 
126     /**
127      * We avoid measuring {@link #mContent} with a 0 width or height, as this
128      * results in CellLayout being measured as UNSPECIFIED, which it does not support.
129      */
130     private static final int MIN_CONTENT_DIMEN = 5;
131 
132     static final int STATE_NONE = -1;
133     static final int STATE_SMALL = 0;
134     static final int STATE_ANIMATING = 1;
135     static final int STATE_OPEN = 2;
136 
137     /**
138      * Time for which the scroll hint is shown before automatically changing page.
139      */
140     public static final int SCROLL_HINT_DURATION = 500;
141     public static final int RESCROLL_DELAY = PagedView.PAGE_SNAP_ANIMATION_DURATION + 150;
142 
143     public static final int SCROLL_NONE = -1;
144     public static final int SCROLL_LEFT = 0;
145     public static final int SCROLL_RIGHT = 1;
146 
147     /**
148      * Fraction of icon width which behave as scroll region.
149      */
150     private static final float ICON_OVERSCROLL_WIDTH_FACTOR = 0.45f;
151 
152     private static final int FOLDER_NAME_ANIMATION_DURATION = 633;
153     private static final int FOLDER_COLOR_ANIMATION_DURATION = 200;
154 
155     private static final int REORDER_DELAY = 250;
156     private static final int ON_EXIT_CLOSE_DELAY = 400;
157     private static final Rect sTempRect = new Rect();
158     private static final int MIN_FOLDERS_FOR_HARDWARE_OPTIMIZATION = 10;
159 
160     private final Alarm mReorderAlarm = new Alarm();
161     private final Alarm mOnExitAlarm = new Alarm();
162     private final Alarm mOnScrollHintAlarm = new Alarm();
163     final Alarm mScrollPauseAlarm = new Alarm();
164 
165     final ArrayList<View> mItemsInReadingOrder = new ArrayList<View>();
166 
167     private AnimatorSet mCurrentAnimator;
168     private boolean mIsAnimatingClosed = false;
169 
170     // Folder can be displayed in Launcher's activity or a separate window (e.g. Taskbar).
171     // Anything specific to Launcher should use mLauncherDelegate, otherwise should
172     // use mActivityContext.
173     protected final LauncherDelegate mLauncherDelegate;
174     protected final ActivityContext mActivityContext;
175 
176     protected DragController mDragController;
177     public FolderInfo mInfo;
178     private CharSequence mFromTitle;
179     private FromState mFromLabelState;
180 
181     @Thunk
182     FolderIcon mFolderIcon;
183 
184     @Thunk
185     FolderPagedView mContent;
186     public FolderNameEditText mFolderName;
187     private PageIndicatorDots mPageIndicator;
188 
189     protected View mFooter;
190     private int mFooterHeight;
191 
192     // Cell ranks used for drag and drop
193     @Thunk
194     int mTargetRank, mPrevTargetRank, mEmptyCellRank;
195 
196     private Path mClipPath;
197 
198     @ViewDebug.ExportedProperty(category = "launcher",
199             mapping = {
200                     @ViewDebug.IntToString(from = STATE_NONE, to = "STATE_NONE"),
201                     @ViewDebug.IntToString(from = STATE_SMALL, to = "STATE_SMALL"),
202                     @ViewDebug.IntToString(from = STATE_ANIMATING, to = "STATE_ANIMATING"),
203                     @ViewDebug.IntToString(from = STATE_OPEN, to = "STATE_OPEN"),
204             })
205     @Thunk
206     int mState = STATE_NONE;
207     @ViewDebug.ExportedProperty(category = "launcher")
208     private boolean mRearrangeOnClose = false;
209     boolean mItemsInvalidated = false;
210     private View mCurrentDragView;
211     private boolean mIsExternalDrag;
212     private boolean mDragInProgress = false;
213     private boolean mDeleteFolderOnDropCompleted = false;
214     private boolean mSuppressFolderDeletion = false;
215     private boolean mItemAddedBackToSelfViaIcon = false;
216     private boolean mIsEditingName = false;
217 
218     @ViewDebug.ExportedProperty(category = "launcher")
219     private boolean mDestroyed;
220 
221     // Folder scrolling
222     private int mScrollAreaOffset;
223 
224     @Thunk
225     int mScrollHintDir = SCROLL_NONE;
226     @Thunk
227     int mCurrentScrollDir = SCROLL_NONE;
228 
229     private StatsLogManager mStatsLogManager;
230 
231     @Nullable
232     private KeyboardInsetAnimationCallback mKeyboardInsetAnimationCallback;
233 
234     private GradientDrawable mBackground;
235 
236     /**
237      * Used to inflate the Workspace from XML.
238      *
239      * @param context The application's context.
240      * @param attrs   The attributes set containing the Workspace's customization values.
241      */
Folder(Context context, AttributeSet attrs)242     public Folder(Context context, AttributeSet attrs) {
243         super(context, attrs);
244         setAlwaysDrawnWithCacheEnabled(false);
245 
246         mActivityContext = ActivityContext.lookupContext(context);
247         mLauncherDelegate = LauncherDelegate.from(mActivityContext);
248 
249         mStatsLogManager = StatsLogManager.newInstance(context);
250         // We need this view to be focusable in touch mode so that when text editing of the folder
251         // name is complete, we have something to focus on, thus hiding the cursor and giving
252         // reliable behavior when clicking the text field (since it will always gain focus on
253         // click).
254         setFocusableInTouchMode(true);
255 
256     }
257 
258     @Override
getBackground()259     public Drawable getBackground() {
260         return mBackground;
261     }
262 
263     @Override
onFinishInflate()264     protected void onFinishInflate() {
265         super.onFinishInflate();
266         final DeviceProfile dp = mActivityContext.getDeviceProfile();
267         final int paddingLeftRight = dp.folderContentPaddingLeftRight;
268 
269         mBackground = (GradientDrawable) ResourcesCompat.getDrawable(getResources(),
270                 R.drawable.round_rect_folder, getContext().getTheme());
271 
272         mContent = findViewById(R.id.folder_content);
273         mContent.setPadding(paddingLeftRight, dp.folderContentPaddingTop, paddingLeftRight, 0);
274         mContent.setFolder(this);
275 
276         mPageIndicator = findViewById(R.id.folder_page_indicator);
277         mFolderName = findViewById(R.id.folder_name);
278         mFolderName.setTextSize(TypedValue.COMPLEX_UNIT_PX, dp.folderLabelTextSizePx);
279         mFolderName.setOnBackKeyListener(this);
280         mFolderName.setOnFocusChangeListener(this);
281         mFolderName.setOnEditorActionListener(this);
282         mFolderName.setSelectAllOnFocus(true);
283         mFolderName.setInputType(mFolderName.getInputType()
284                 & ~InputType.TYPE_TEXT_FLAG_AUTO_CORRECT
285                 | InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS
286                 | InputType.TYPE_TEXT_FLAG_CAP_WORDS);
287         mFolderName.forceDisableSuggestions(true);
288 
289         mFooter = findViewById(R.id.folder_footer);
290         mFooterHeight = getResources().getDimensionPixelSize(R.dimen.folder_label_height);
291 
292         if (Utilities.ATLEAST_R) {
293             mKeyboardInsetAnimationCallback = new KeyboardInsetAnimationCallback(this);
294             setWindowInsetsAnimationCallback(mKeyboardInsetAnimationCallback);
295         }
296     }
297 
onLongClick(View v)298     public boolean onLongClick(View v) {
299         // Return if global dragging is not enabled
300         if (!mLauncherDelegate.isDraggingEnabled()) return true;
301         return startDrag(v, new DragOptions());
302     }
303 
startDrag(View v, DragOptions options)304     public boolean startDrag(View v, DragOptions options) {
305         Object tag = v.getTag();
306         if (tag instanceof WorkspaceItemInfo) {
307             WorkspaceItemInfo item = (WorkspaceItemInfo) tag;
308 
309             mEmptyCellRank = item.rank;
310             mCurrentDragView = v;
311 
312             mDragController.addDragListener(this);
313             if (options.isAccessibleDrag) {
314                 mDragController.addDragListener(new AccessibleDragListenerAdapter(
315                         mContent, FolderAccessibilityHelper::new) {
316                     @Override
317                     protected void enableAccessibleDrag(boolean enable) {
318                         super.enableAccessibleDrag(enable);
319                         mFooter.setImportantForAccessibility(enable
320                                 ? IMPORTANT_FOR_ACCESSIBILITY_NO_HIDE_DESCENDANTS
321                                 : IMPORTANT_FOR_ACCESSIBILITY_AUTO);
322                     }
323                 });
324             }
325 
326             mLauncherDelegate.beginDragShared(v, this, options);
327         }
328         return true;
329     }
330 
331     @Override
onDragStart(DropTarget.DragObject dragObject, DragOptions options)332     public void onDragStart(DropTarget.DragObject dragObject, DragOptions options) {
333         if (dragObject.dragSource != this) {
334             return;
335         }
336 
337         mContent.removeItem(mCurrentDragView);
338         if (dragObject.dragInfo instanceof WorkspaceItemInfo) {
339             mItemsInvalidated = true;
340 
341             // We do not want to get events for the item being removed, as they will get handled
342             // when the drop completes
343             try (SuppressInfoChanges s = new SuppressInfoChanges()) {
344                 mInfo.remove((WorkspaceItemInfo) dragObject.dragInfo, true);
345             }
346         }
347         mDragInProgress = true;
348         mItemAddedBackToSelfViaIcon = false;
349     }
350 
351     @Override
onDragEnd()352     public void onDragEnd() {
353         if (mIsExternalDrag && mDragInProgress) {
354             completeDragExit();
355         }
356         mDragInProgress = false;
357         mDragController.removeDragListener(this);
358     }
359 
isEditingName()360     public boolean isEditingName() {
361         return mIsEditingName;
362     }
363 
startEditingFolderName()364     public void startEditingFolderName() {
365         post(() -> {
366             if (FeatureFlags.FOLDER_NAME_SUGGEST.get()) {
367                 showLabelSuggestions();
368             }
369             mFolderName.setHint("");
370             mIsEditingName = true;
371         });
372     }
373 
374     @Override
onBackKey()375     public boolean onBackKey() {
376         // Convert to a string here to ensure that no other state associated with the text field
377         // gets saved.
378         String newTitle = mFolderName.getText().toString();
379         if (DEBUG) {
380             Log.d(TAG, "onBackKey newTitle=" + newTitle);
381         }
382         mInfo.setTitle(newTitle, mLauncherDelegate.getModelWriter());
383         mFolderIcon.onTitleChanged(newTitle);
384 
385         if (TextUtils.isEmpty(mInfo.title)) {
386             mFolderName.setHint(R.string.folder_hint_text);
387             mFolderName.setText("");
388         } else {
389             mFolderName.setHint(null);
390         }
391 
392         sendCustomAccessibilityEvent(
393                 this, AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED,
394                 getContext().getString(R.string.folder_renamed, newTitle));
395 
396         // This ensures that focus is gained every time the field is clicked, which selects all
397         // the text and brings up the soft keyboard if necessary.
398         mFolderName.clearFocus();
399 
400         Selection.setSelection(mFolderName.getText(), 0, 0);
401         mIsEditingName = false;
402         return true;
403     }
404 
onEditorAction(TextView v, int actionId, KeyEvent event)405     public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
406         if (DEBUG) {
407             Log.d(TAG, "onEditorAction actionId=" + actionId + " key="
408                     + (event != null ? event.getKeyCode() : "null event"));
409         }
410         if (actionId == EditorInfo.IME_ACTION_DONE) {
411             mFolderName.dispatchBackKey();
412             return true;
413         }
414         return false;
415     }
416 
417     @Override
onApplyWindowInsets(WindowInsets windowInsets)418     public WindowInsets onApplyWindowInsets(WindowInsets windowInsets) {
419         if (Utilities.ATLEAST_R) {
420             this.setTranslationY(0);
421 
422             if (windowInsets.isVisible(WindowInsets.Type.ime())) {
423                 Insets keyboardInsets = windowInsets.getInsets(WindowInsets.Type.ime());
424                 int folderHeightFromBottom = getHeightFromBottom();
425 
426                 if (keyboardInsets.bottom > folderHeightFromBottom) {
427                     // Translate this folder above the keyboard, then add the folder name's padding
428                     this.setTranslationY(folderHeightFromBottom - keyboardInsets.bottom
429                             - mFolderName.getPaddingBottom());
430                 }
431             }
432         }
433 
434         return windowInsets;
435     }
436 
getFolderIcon()437     public FolderIcon getFolderIcon() {
438         return mFolderIcon;
439     }
440 
setDragController(DragController dragController)441     public void setDragController(DragController dragController) {
442         mDragController = dragController;
443     }
444 
setFolderIcon(FolderIcon icon)445     public void setFolderIcon(FolderIcon icon) {
446         mFolderIcon = icon;
447         mLauncherDelegate.init(this, icon);
448     }
449 
450     @Override
onAttachedToWindow()451     protected void onAttachedToWindow() {
452         // requestFocus() causes the focus onto the folder itself, which doesn't cause visual
453         // effect but the next arrow key can start the keyboard focus inside of the folder, not
454         // the folder itself.
455         requestFocus();
456         super.onAttachedToWindow();
457     }
458 
459     @Override
dispatchPopulateAccessibilityEvent(AccessibilityEvent event)460     public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
461         // When the folder gets focus, we don't want to announce the list of items.
462         return true;
463     }
464 
465     @Override
focusSearch(int direction)466     public View focusSearch(int direction) {
467         // When the folder is focused, further focus search should be within the folder contents.
468         return FocusFinder.getInstance().findNextFocus(this, null, direction);
469     }
470 
471     /**
472      * @return the FolderInfo object associated with this folder
473      */
getInfo()474     public FolderInfo getInfo() {
475         return mInfo;
476     }
477 
bind(FolderInfo info)478     void bind(FolderInfo info) {
479         mInfo = info;
480         mFromTitle = info.title;
481         mFromLabelState = info.getFromLabelState();
482         ArrayList<WorkspaceItemInfo> children = info.contents;
483         Collections.sort(children, ITEM_POS_COMPARATOR);
484         updateItemLocationsInDatabaseBatch(true);
485 
486         BaseDragLayer.LayoutParams lp = (BaseDragLayer.LayoutParams) getLayoutParams();
487         if (lp == null) {
488             lp = new BaseDragLayer.LayoutParams(0, 0);
489             lp.customPosition = true;
490             setLayoutParams(lp);
491         }
492         mItemsInvalidated = true;
493         mInfo.addListener(this);
494 
495         if (!isEmpty(mInfo.title)) {
496             mFolderName.setText(mInfo.title);
497             mFolderName.setHint(null);
498         } else {
499             mFolderName.setText("");
500             mFolderName.setHint(R.string.folder_hint_text);
501         }
502         // In case any children didn't come across during loading, clean up the folder accordingly
503         mFolderIcon.post(() -> {
504             if (getItemCount() <= 1) {
505                 replaceFolderWithFinalItem();
506             }
507         });
508     }
509 
510 
511     /**
512      * Show suggested folder title in FolderEditText if the first suggestion is non-empty, push
513      * rest of the suggestions to InputMethodManager.
514      */
showLabelSuggestions()515     private void showLabelSuggestions() {
516         if (mInfo.suggestedFolderNames == null) {
517             return;
518         }
519         if (mInfo.suggestedFolderNames.hasSuggestions()) {
520             // update the primary suggestion if the folder name is empty.
521             if (isEmpty(mFolderName.getText())) {
522                 if (mInfo.suggestedFolderNames.hasPrimary()) {
523                     mFolderName.setHint("");
524                     mFolderName.setText(mInfo.suggestedFolderNames.getLabels()[0]);
525                     mFolderName.selectAll();
526                 }
527             }
528             mFolderName.showKeyboard();
529             mFolderName.displayCompletions(
530                     Stream.of(mInfo.suggestedFolderNames.getLabels())
531                             .filter(Objects::nonNull)
532                             .map(Object::toString)
533                             .filter(s -> !s.isEmpty())
534                             .filter(s -> !s.equalsIgnoreCase(mFolderName.getText().toString()))
535                             .collect(Collectors.toList()));
536         }
537     }
538 
539     /**
540      * Creates a new UserFolder, inflated from R.layout.user_folder.
541      *
542      * @param activityContext The main ActivityContext in which to inflate this Folder. It must also
543      *                        be an instance or ContextWrapper around the Launcher activity context.
544      * @return A new UserFolder.
545      */
546     @SuppressLint("InflateParams")
fromXml(T activityContext)547     static <T extends Context & ActivityContext> Folder fromXml(T activityContext) {
548         return (Folder) LayoutInflater.from(activityContext).cloneInContext(activityContext)
549                 .inflate(R.layout.user_folder_icon_normalized, null);
550     }
551 
startAnimation(final AnimatorSet a)552     private void startAnimation(final AnimatorSet a) {
553         mLauncherDelegate.forEachVisibleWorkspacePage(
554                 visiblePage -> addAnimatorListenerForPage(a, (CellLayout) visiblePage));
555 
556         a.addListener(new AnimatorListenerAdapter() {
557             @Override
558             public void onAnimationStart(Animator animation) {
559                 mState = STATE_ANIMATING;
560                 mCurrentAnimator = a;
561             }
562 
563             @Override
564             public void onAnimationEnd(Animator animation) {
565                 mCurrentAnimator = null;
566             }
567         });
568         a.start();
569     }
570 
addAnimatorListenerForPage(AnimatorSet a, CellLayout currentCellLayout)571     private void addAnimatorListenerForPage(AnimatorSet a, CellLayout currentCellLayout) {
572         final boolean useHardware = shouldUseHardwareLayerForAnimation(currentCellLayout);
573         final boolean wasHardwareAccelerated = currentCellLayout.isHardwareLayerEnabled();
574 
575         a.addListener(new AnimatorListenerAdapter() {
576             @Override
577             public void onAnimationStart(Animator animation) {
578                 if (useHardware) {
579                     currentCellLayout.enableHardwareLayer(true);
580                 }
581             }
582 
583             @Override
584             public void onAnimationEnd(Animator animation) {
585                 if (useHardware) {
586                     currentCellLayout.enableHardwareLayer(wasHardwareAccelerated);
587                 }
588             }
589         });
590     }
591 
shouldUseHardwareLayerForAnimation(CellLayout currentCellLayout)592     private boolean shouldUseHardwareLayerForAnimation(CellLayout currentCellLayout) {
593         if (ALWAYS_USE_HARDWARE_OPTIMIZATION_FOR_FOLDER_ANIMATIONS.get()) return true;
594 
595         int folderCount = 0;
596         final ShortcutAndWidgetContainer container = currentCellLayout.getShortcutsAndWidgets();
597         for (int i = container.getChildCount() - 1; i >= 0; --i) {
598             final View child = container.getChildAt(i);
599             if (child instanceof AppWidgetHostView) return false;
600             if (child instanceof FolderIcon) ++folderCount;
601         }
602         return folderCount >= MIN_FOLDERS_FOR_HARDWARE_OPTIMIZATION;
603     }
604 
605     /**
606      * Opens the folder as part of a drag operation
607      */
beginExternalDrag()608     public void beginExternalDrag() {
609         mIsExternalDrag = true;
610         mDragInProgress = true;
611 
612         // Since this folder opened by another controller, it might not get onDrop or
613         // onDropComplete. Perform cleanup once drag-n-drop ends.
614         mDragController.addDragListener(this);
615 
616         ArrayList<WorkspaceItemInfo> items = new ArrayList<>(mInfo.contents);
617         mEmptyCellRank = items.size();
618         items.add(null);    // Add an empty spot at the end
619 
620         animateOpen(items, mEmptyCellRank / mContent.itemsPerPage());
621     }
622 
623     /**
624      * Opens the user folder described by the specified tag. The opening of the folder
625      * is animated relative to the specified View. If the View is null, no animation
626      * is played.
627      */
animateOpen()628     public void animateOpen() {
629         animateOpen(mInfo.contents, 0);
630     }
631 
632     /**
633      * Opens the user folder described by the specified tag. The opening of the folder
634      * is animated relative to the specified View. If the View is null, no animation
635      * is played.
636      */
animateOpen(List<WorkspaceItemInfo> items, int pageNo)637     private void animateOpen(List<WorkspaceItemInfo> items, int pageNo) {
638         Folder openFolder = getOpen(mActivityContext);
639         if (openFolder != null && openFolder != this) {
640             // Close any open folder before opening a folder.
641             openFolder.close(true);
642         }
643 
644         mContent.bindItems(items);
645         centerAboutIcon();
646         mItemsInvalidated = true;
647         updateTextViewFocus();
648 
649         mIsOpen = true;
650 
651         BaseDragLayer dragLayer = mActivityContext.getDragLayer();
652         // Just verify that the folder hasn't already been added to the DragLayer.
653         // There was a one-off crash where the folder had a parent already.
654         if (getParent() == null) {
655             dragLayer.addView(this);
656             mDragController.addDropTarget(this);
657         } else {
658             if (FeatureFlags.IS_STUDIO_BUILD) {
659                 Log.e(TAG, "Opening folder (" + this + ") which already has a parent:"
660                         + getParent());
661             }
662         }
663 
664         mContent.completePendingPageChanges();
665         mContent.setCurrentPage(pageNo);
666 
667         // This is set to true in close(), but isn't reset to false until onDropCompleted(). This
668         // leads to an inconsistent state if you drag out of the folder and drag back in without
669         // dropping. One resulting issue is that replaceFolderWithFinalItem() can be called twice.
670         mDeleteFolderOnDropCompleted = false;
671 
672         cancelRunningAnimations();
673         FolderAnimationManager fam = new FolderAnimationManager(this, true /* isOpening */);
674         AnimatorSet anim = fam.getAnimator();
675         anim.addListener(new AnimatorListenerAdapter() {
676             @Override
677             public void onAnimationStart(Animator animation) {
678                 mFolderIcon.setIconVisible(false);
679                 mFolderIcon.drawLeaveBehindIfExists();
680             }
681 
682             @Override
683             public void onAnimationEnd(Animator animation) {
684                 mState = STATE_OPEN;
685                 announceAccessibilityChanges();
686 
687                 mContent.setFocusOnFirstChild();
688             }
689         });
690 
691         // Footer animation
692         if (mContent.getPageCount() > 1 && !mInfo.hasOption(FolderInfo.FLAG_MULTI_PAGE_ANIMATION)) {
693             int footerWidth = mContent.getDesiredWidth()
694                     - mFooter.getPaddingLeft() - mFooter.getPaddingRight();
695 
696             float textWidth = mFolderName.getPaint().measureText(mFolderName.getText().toString());
697             float translation = (footerWidth - textWidth) / 2;
698             mFolderName.setTranslationX(mContent.mIsRtl ? -translation : translation);
699             mPageIndicator.prepareEntryAnimation();
700 
701             // Do not update the flag if we are in drag mode. The flag will be updated, when we
702             // actually drop the icon.
703             final boolean updateAnimationFlag = !mDragInProgress;
704             anim.addListener(new AnimatorListenerAdapter() {
705 
706                 @SuppressLint("InlinedApi")
707                 @Override
708                 public void onAnimationEnd(Animator animation) {
709                     mFolderName.animate().setDuration(FOLDER_NAME_ANIMATION_DURATION)
710                             .translationX(0)
711                             .setInterpolator(AnimationUtils.loadInterpolator(
712                                     getContext(), android.R.interpolator.fast_out_slow_in));
713                     mPageIndicator.playEntryAnimation();
714 
715                     if (updateAnimationFlag) {
716                         mInfo.setOption(FolderInfo.FLAG_MULTI_PAGE_ANIMATION, true,
717                                 mLauncherDelegate.getModelWriter());
718                     }
719                 }
720             });
721         } else {
722             mFolderName.setTranslationX(0);
723         }
724 
725         mPageIndicator.stopAllAnimations();
726         startAnimation(anim);
727         // Because t=0 has the folder match the folder icon, we can skip the
728         // first frame and have the same movement one frame earlier.
729         anim.setCurrentPlayTime(Math.min(getSingleFrameMs(getContext()), anim.getTotalDuration()));
730 
731         // Make sure the folder picks up the last drag move even if the finger doesn't move.
732         if (mDragController.isDragging()) {
733             mDragController.forceTouchMove();
734         }
735         mContent.verifyVisibleHighResIcons(mContent.getNextPage());
736     }
737 
738     @Override
isOfType(int type)739     protected boolean isOfType(int type) {
740         return (type & TYPE_FOLDER) != 0;
741     }
742 
743     @Override
handleClose(boolean animate)744     protected void handleClose(boolean animate) {
745         mIsOpen = false;
746 
747         if (!animate && mCurrentAnimator != null && mCurrentAnimator.isRunning()) {
748             mCurrentAnimator.cancel();
749         }
750 
751         if (isEditingName()) {
752             mFolderName.dispatchBackKey();
753         }
754 
755         if (mFolderIcon != null) {
756             mFolderIcon.clearLeaveBehindIfExists();
757         }
758 
759         if (animate) {
760             animateClosed();
761         } else {
762             closeComplete(false);
763             post(this::announceAccessibilityChanges);
764         }
765 
766         // Notify the accessibility manager that this folder "window" has disappeared and no
767         // longer occludes the workspace items
768         mActivityContext.getDragLayer().sendAccessibilityEvent(
769                 AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
770     }
771 
cancelRunningAnimations()772     private void cancelRunningAnimations() {
773         if (mCurrentAnimator != null && mCurrentAnimator.isRunning()) {
774             mCurrentAnimator.cancel();
775         }
776     }
777 
animateClosed()778     private void animateClosed() {
779         if (mIsAnimatingClosed) {
780             return;
781         }
782 
783         mContent.completePendingPageChanges();
784         mContent.snapToPageImmediately(mContent.getDestinationPage());
785 
786         cancelRunningAnimations();
787         AnimatorSet a = new FolderAnimationManager(this, false /* isOpening */).getAnimator();
788         a.addListener(new AnimatorListenerAdapter() {
789             @Override
790             public void onAnimationStart(Animator animation) {
791                 if (Utilities.ATLEAST_R) {
792                     setWindowInsetsAnimationCallback(null);
793                 }
794                 mIsAnimatingClosed = true;
795             }
796 
797             @Override
798             public void onAnimationEnd(Animator animation) {
799                 if (Utilities.ATLEAST_R && mKeyboardInsetAnimationCallback != null) {
800                     setWindowInsetsAnimationCallback(mKeyboardInsetAnimationCallback);
801                 }
802                 closeComplete(true);
803                 announceAccessibilityChanges();
804                 mIsAnimatingClosed = false;
805             }
806         });
807         startAnimation(a);
808     }
809 
810     @Override
getAccessibilityTarget()811     protected Pair<View, String> getAccessibilityTarget() {
812         return Pair.create(mContent, mIsOpen ? mContent.getAccessibilityDescription()
813                 : getContext().getString(R.string.folder_closed));
814     }
815 
816     @Override
getAccessibilityInitialFocusView()817     protected View getAccessibilityInitialFocusView() {
818         View firstItem = mContent.getFirstItem();
819         return firstItem != null ? firstItem : super.getAccessibilityInitialFocusView();
820     }
821 
closeComplete(boolean wasAnimated)822     private void closeComplete(boolean wasAnimated) {
823         // TODO: Clear all active animations.
824         BaseDragLayer parent = (BaseDragLayer) getParent();
825         if (parent != null) {
826             parent.removeView(this);
827         }
828         mDragController.removeDropTarget(this);
829         clearFocus();
830         if (mFolderIcon != null) {
831             mFolderIcon.setVisibility(View.VISIBLE);
832             mFolderIcon.setIconVisible(true);
833             mFolderIcon.mFolderName.setTextVisibility(true);
834             if (wasAnimated) {
835                 mFolderIcon.animateBgShadowAndStroke();
836                 mFolderIcon.onFolderClose(mContent.getCurrentPage());
837                 if (mFolderIcon.hasDot()) {
838                     mFolderIcon.animateDotScale(0f, 1f);
839                 }
840                 mFolderIcon.requestFocus();
841             }
842         }
843 
844         if (mRearrangeOnClose) {
845             rearrangeChildren();
846             mRearrangeOnClose = false;
847         }
848         if (getItemCount() <= 1) {
849             if (!mDragInProgress && !mSuppressFolderDeletion) {
850                 replaceFolderWithFinalItem();
851             } else if (mDragInProgress) {
852                 mDeleteFolderOnDropCompleted = true;
853             }
854         } else if (!mDragInProgress) {
855             mContent.unbindItems();
856         }
857         mSuppressFolderDeletion = false;
858         clearDragInfo();
859         mState = STATE_SMALL;
860         mContent.setCurrentPage(0);
861     }
862 
863     @Override
acceptDrop(DragObject d)864     public boolean acceptDrop(DragObject d) {
865         final ItemInfo item = d.dragInfo;
866         final int itemType = item.itemType;
867         return ((itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION ||
868                 itemType == LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT ||
869                 itemType == LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT));
870     }
871 
onDragEnter(DragObject d)872     public void onDragEnter(DragObject d) {
873         mPrevTargetRank = -1;
874         mOnExitAlarm.cancelAlarm();
875         // Get the area offset such that the folder only closes if half the drag icon width
876         // is outside the folder area
877         mScrollAreaOffset = d.dragView.getDragRegionWidth() / 2 - d.xOffset;
878     }
879 
880     OnAlarmListener mReorderAlarmListener = new OnAlarmListener() {
881         public void onAlarm(Alarm alarm) {
882             mContent.realTimeReorder(mEmptyCellRank, mTargetRank);
883             mEmptyCellRank = mTargetRank;
884         }
885     };
886 
isLayoutRtl()887     public boolean isLayoutRtl() {
888         return (getLayoutDirection() == LAYOUT_DIRECTION_RTL);
889     }
890 
getTargetRank(DragObject d, float[] recycle)891     private int getTargetRank(DragObject d, float[] recycle) {
892         recycle = d.getVisualCenter(recycle);
893         return mContent.findNearestArea(
894                 (int) recycle[0] - getPaddingLeft(), (int) recycle[1] - getPaddingTop());
895     }
896 
897     @Override
onDragOver(DragObject d)898     public void onDragOver(DragObject d) {
899         if (mScrollPauseAlarm.alarmPending()) {
900             return;
901         }
902         final float[] r = new float[2];
903         mTargetRank = getTargetRank(d, r);
904 
905         if (mTargetRank != mPrevTargetRank) {
906             mReorderAlarm.cancelAlarm();
907             mReorderAlarm.setOnAlarmListener(mReorderAlarmListener);
908             mReorderAlarm.setAlarm(REORDER_DELAY);
909             mPrevTargetRank = mTargetRank;
910 
911             if (d.stateAnnouncer != null) {
912                 d.stateAnnouncer.announce(getContext().getString(R.string.move_to_position,
913                         mTargetRank + 1));
914             }
915         }
916 
917         float x = r[0];
918         int currentPage = mContent.getNextPage();
919 
920         float cellOverlap = mContent.getCurrentCellLayout().getCellWidth()
921                 * ICON_OVERSCROLL_WIDTH_FACTOR;
922         boolean isOutsideLeftEdge = x < cellOverlap;
923         boolean isOutsideRightEdge = x > (getWidth() - cellOverlap);
924 
925         if (currentPage > 0 && (mContent.mIsRtl ? isOutsideRightEdge : isOutsideLeftEdge)) {
926             showScrollHint(SCROLL_LEFT, d);
927         } else if (currentPage < (mContent.getPageCount() - 1)
928                 && (mContent.mIsRtl ? isOutsideLeftEdge : isOutsideRightEdge)) {
929             showScrollHint(SCROLL_RIGHT, d);
930         } else {
931             mOnScrollHintAlarm.cancelAlarm();
932             if (mScrollHintDir != SCROLL_NONE) {
933                 mContent.clearScrollHint();
934                 mScrollHintDir = SCROLL_NONE;
935             }
936         }
937     }
938 
showScrollHint(int direction, DragObject d)939     private void showScrollHint(int direction, DragObject d) {
940         // Show scroll hint on the right
941         if (mScrollHintDir != direction) {
942             mContent.showScrollHint(direction);
943             mScrollHintDir = direction;
944         }
945 
946         // Set alarm for when the hint is complete
947         if (!mOnScrollHintAlarm.alarmPending() || mCurrentScrollDir != direction) {
948             mCurrentScrollDir = direction;
949             mOnScrollHintAlarm.cancelAlarm();
950             mOnScrollHintAlarm.setOnAlarmListener(new OnScrollHintListener(d));
951             mOnScrollHintAlarm.setAlarm(SCROLL_HINT_DURATION);
952 
953             mReorderAlarm.cancelAlarm();
954             mTargetRank = mEmptyCellRank;
955         }
956     }
957 
958     OnAlarmListener mOnExitAlarmListener = new OnAlarmListener() {
959         public void onAlarm(Alarm alarm) {
960             completeDragExit();
961         }
962     };
963 
completeDragExit()964     public void completeDragExit() {
965         if (mIsOpen) {
966             close(true);
967             mRearrangeOnClose = true;
968         } else if (mState == STATE_ANIMATING) {
969             mRearrangeOnClose = true;
970         } else {
971             rearrangeChildren();
972             clearDragInfo();
973         }
974     }
975 
clearDragInfo()976     private void clearDragInfo() {
977         mCurrentDragView = null;
978         mIsExternalDrag = false;
979     }
980 
onDragExit(DragObject d)981     public void onDragExit(DragObject d) {
982         // We only close the folder if this is a true drag exit, ie. not because
983         // a drop has occurred above the folder.
984         if (!d.dragComplete) {
985             mOnExitAlarm.setOnAlarmListener(mOnExitAlarmListener);
986             mOnExitAlarm.setAlarm(ON_EXIT_CLOSE_DELAY);
987         }
988         mReorderAlarm.cancelAlarm();
989 
990         mOnScrollHintAlarm.cancelAlarm();
991         mScrollPauseAlarm.cancelAlarm();
992         if (mScrollHintDir != SCROLL_NONE) {
993             mContent.clearScrollHint();
994             mScrollHintDir = SCROLL_NONE;
995         }
996     }
997 
998     /**
999      * When performing an accessibility drop, onDrop is sent immediately after onDragEnter. So we
1000      * need to complete all transient states based on timers.
1001      */
1002     @Override
prepareAccessibilityDrop()1003     public void prepareAccessibilityDrop() {
1004         if (mReorderAlarm.alarmPending()) {
1005             mReorderAlarm.cancelAlarm();
1006             mReorderAlarmListener.onAlarm(mReorderAlarm);
1007         }
1008     }
1009 
1010     @Override
onDropCompleted(final View target, final DragObject d, final boolean success)1011     public void onDropCompleted(final View target, final DragObject d,
1012             final boolean success) {
1013         if (success) {
1014             if (mDeleteFolderOnDropCompleted && !mItemAddedBackToSelfViaIcon && target != this) {
1015                 replaceFolderWithFinalItem();
1016             }
1017         } else {
1018             // The drag failed, we need to return the item to the folder
1019             WorkspaceItemInfo info = (WorkspaceItemInfo) d.dragInfo;
1020             View icon = (mCurrentDragView != null && mCurrentDragView.getTag() == info)
1021                     ? mCurrentDragView : mContent.createNewView(info);
1022             ArrayList<View> views = getIconsInReadingOrder();
1023             info.rank = Utilities.boundToRange(info.rank, 0, views.size());
1024             views.add(info.rank, icon);
1025             mContent.arrangeChildren(views);
1026             mItemsInvalidated = true;
1027 
1028             try (SuppressInfoChanges s = new SuppressInfoChanges()) {
1029                 mFolderIcon.onDrop(d, true /* itemReturnedOnFailedDrop */);
1030             }
1031         }
1032 
1033         if (target != this) {
1034             if (mOnExitAlarm.alarmPending()) {
1035                 mOnExitAlarm.cancelAlarm();
1036                 if (!success) {
1037                     mSuppressFolderDeletion = true;
1038                 }
1039                 mScrollPauseAlarm.cancelAlarm();
1040                 completeDragExit();
1041             }
1042         }
1043 
1044         mDeleteFolderOnDropCompleted = false;
1045         mDragInProgress = false;
1046         mItemAddedBackToSelfViaIcon = false;
1047         mCurrentDragView = null;
1048 
1049         // Reordering may have occured, and we need to save the new item locations. We do this once
1050         // at the end to prevent unnecessary database operations.
1051         updateItemLocationsInDatabaseBatch(false);
1052         // Use the item count to check for multi-page as the folder UI may not have
1053         // been refreshed yet.
1054         if (getItemCount() <= mContent.itemsPerPage()) {
1055             // Show the animation, next time something is added to the folder.
1056             mInfo.setOption(FolderInfo.FLAG_MULTI_PAGE_ANIMATION, false,
1057                     mLauncherDelegate.getModelWriter());
1058         }
1059     }
1060 
updateItemLocationsInDatabaseBatch(boolean isBind)1061     private void updateItemLocationsInDatabaseBatch(boolean isBind) {
1062         FolderGridOrganizer verifier = new FolderGridOrganizer(
1063                 mActivityContext.getDeviceProfile().inv).setFolderInfo(mInfo);
1064 
1065         ArrayList<ItemInfo> items = new ArrayList<>();
1066         int total = mInfo.contents.size();
1067         for (int i = 0; i < total; i++) {
1068             WorkspaceItemInfo itemInfo = mInfo.contents.get(i);
1069             if (verifier.updateRankAndPos(itemInfo, i)) {
1070                 items.add(itemInfo);
1071             }
1072         }
1073 
1074         if (!items.isEmpty()) {
1075             mLauncherDelegate.getModelWriter().moveItemsInDatabase(items, mInfo.id, 0);
1076         }
1077         if (FeatureFlags.FOLDER_NAME_SUGGEST.get() && !isBind
1078                 && total > 1 /* no need to update if there's one icon */) {
1079             Executors.MODEL_EXECUTOR.post(() -> {
1080                 FolderNameInfos nameInfos = new FolderNameInfos();
1081                 FolderNameProvider fnp = FolderNameProvider.newInstance(getContext());
1082                 fnp.getSuggestedFolderName(
1083                         getContext(), mInfo.contents, nameInfos);
1084                 mInfo.suggestedFolderNames = nameInfos;
1085             });
1086         }
1087     }
1088 
notifyDrop()1089     public void notifyDrop() {
1090         if (mDragInProgress) {
1091             mItemAddedBackToSelfViaIcon = true;
1092         }
1093     }
1094 
isDropEnabled()1095     public boolean isDropEnabled() {
1096         return mState != STATE_ANIMATING;
1097     }
1098 
centerAboutIcon()1099     private void centerAboutIcon() {
1100         BaseDragLayer.LayoutParams lp = (BaseDragLayer.LayoutParams) getLayoutParams();
1101         BaseDragLayer parent = mActivityContext.getDragLayer();
1102         int width = getFolderWidth();
1103         int height = getFolderHeight();
1104 
1105         parent.getDescendantRectRelativeToSelf(mFolderIcon, sTempRect);
1106         int centerX = sTempRect.centerX();
1107         int centerY = sTempRect.centerY();
1108         int centeredLeft = centerX - width / 2;
1109         int centeredTop = centerY - height / 2;
1110 
1111         sTempRect.set(mActivityContext.getFolderBoundingBox());
1112         int left = Utilities.boundToRange(centeredLeft, sTempRect.left, sTempRect.right - width);
1113         int top = Utilities.boundToRange(centeredTop, sTempRect.top, sTempRect.bottom - height);
1114         int[] inOutPosition = new int[]{left, top};
1115         mActivityContext.updateOpenFolderPosition(inOutPosition, sTempRect, width, height);
1116         left = inOutPosition[0];
1117         top = inOutPosition[1];
1118 
1119         int folderPivotX = width / 2 + (centeredLeft - left);
1120         int folderPivotY = height / 2 + (centeredTop - top);
1121         setPivotX(folderPivotX);
1122         setPivotY(folderPivotY);
1123 
1124         lp.width = width;
1125         lp.height = height;
1126         lp.x = left;
1127         lp.y = top;
1128 
1129         mBackground.setBounds(0, 0, width, height);
1130     }
1131 
getContentAreaHeight()1132     protected int getContentAreaHeight() {
1133         DeviceProfile grid = mActivityContext.getDeviceProfile();
1134         int maxContentAreaHeight = grid.availableHeightPx - grid.getTotalWorkspacePadding().y
1135                 - mFooterHeight;
1136         int height = Math.min(maxContentAreaHeight,
1137                 mContent.getDesiredHeight());
1138         return Math.max(height, MIN_CONTENT_DIMEN);
1139     }
1140 
getContentAreaWidth()1141     private int getContentAreaWidth() {
1142         return Math.max(mContent.getDesiredWidth(), MIN_CONTENT_DIMEN);
1143     }
1144 
getFolderWidth()1145     private int getFolderWidth() {
1146         return getPaddingLeft() + getPaddingRight() + mContent.getDesiredWidth();
1147     }
1148 
getFolderHeight()1149     private int getFolderHeight() {
1150         return getFolderHeight(getContentAreaHeight());
1151     }
1152 
getFolderHeight(int contentAreaHeight)1153     private int getFolderHeight(int contentAreaHeight) {
1154         return getPaddingTop() + getPaddingBottom() + contentAreaHeight + mFooterHeight;
1155     }
1156 
onMeasure(int widthMeasureSpec, int heightMeasureSpec)1157     protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
1158         int contentWidth = getContentAreaWidth();
1159         int contentHeight = getContentAreaHeight();
1160 
1161         int contentAreaWidthSpec = MeasureSpec.makeMeasureSpec(contentWidth, MeasureSpec.EXACTLY);
1162         int contentAreaHeightSpec = MeasureSpec.makeMeasureSpec(contentHeight, MeasureSpec.EXACTLY);
1163 
1164         mContent.setFixedSize(contentWidth, contentHeight);
1165         mContent.measure(contentAreaWidthSpec, contentAreaHeightSpec);
1166 
1167         if (mContent.getChildCount() > 0) {
1168             int cellIconGap = (mContent.getPageAt(0).getCellWidth()
1169                     - mActivityContext.getDeviceProfile().iconSizePx) / 2;
1170             mFooter.setPadding(mContent.getPaddingLeft() + cellIconGap,
1171                     mFooter.getPaddingTop(),
1172                     mContent.getPaddingRight() + cellIconGap,
1173                     mFooter.getPaddingBottom());
1174         }
1175         mFooter.measure(contentAreaWidthSpec,
1176                 MeasureSpec.makeMeasureSpec(mFooterHeight, MeasureSpec.EXACTLY));
1177 
1178         int folderWidth = getPaddingLeft() + getPaddingRight() + contentWidth;
1179         int folderHeight = getFolderHeight(contentHeight);
1180         setMeasuredDimension(folderWidth, folderHeight);
1181     }
1182 
1183     /**
1184      * Rearranges the children based on their rank.
1185      */
rearrangeChildren()1186     public void rearrangeChildren() {
1187         if (!mContent.areViewsBound()) {
1188             return;
1189         }
1190         mContent.arrangeChildren(getIconsInReadingOrder());
1191         mItemsInvalidated = true;
1192     }
1193 
getItemCount()1194     public int getItemCount() {
1195         return mInfo.contents.size();
1196     }
1197 
replaceFolderWithFinalItem()1198     void replaceFolderWithFinalItem() {
1199         mLauncherDelegate.replaceFolderWithFinalItem(this);
1200         mDestroyed = true;
1201     }
1202 
isDestroyed()1203     public boolean isDestroyed() {
1204         return mDestroyed;
1205     }
1206 
1207     // This method keeps track of the first and last item in the folder for the purposes
1208     // of keyboard focus
updateTextViewFocus()1209     public void updateTextViewFocus() {
1210         final View firstChild = mContent.getFirstItem();
1211         final View lastChild = mContent.getLastItem();
1212         if (firstChild != null && lastChild != null) {
1213             mFolderName.setNextFocusDownId(lastChild.getId());
1214             mFolderName.setNextFocusRightId(lastChild.getId());
1215             mFolderName.setNextFocusLeftId(lastChild.getId());
1216             mFolderName.setNextFocusUpId(lastChild.getId());
1217             // Hitting TAB from the folder name wraps around to the first item on the current
1218             // folder page, and hitting SHIFT+TAB from that item wraps back to the folder name.
1219             mFolderName.setNextFocusForwardId(firstChild.getId());
1220             // When clicking off the folder when editing the name, this Folder gains focus. When
1221             // pressing an arrow key from that state, give the focus to the first item.
1222             this.setNextFocusDownId(firstChild.getId());
1223             this.setNextFocusRightId(firstChild.getId());
1224             this.setNextFocusLeftId(firstChild.getId());
1225             this.setNextFocusUpId(firstChild.getId());
1226             // When pressing shift+tab in the above state, give the focus to the last item.
1227             setOnKeyListener(new OnKeyListener() {
1228                 @Override
1229                 public boolean onKey(View v, int keyCode, KeyEvent event) {
1230                     boolean isShiftPlusTab = keyCode == KeyEvent.KEYCODE_TAB &&
1231                             event.hasModifiers(KeyEvent.META_SHIFT_ON);
1232                     if (isShiftPlusTab && Folder.this.isFocused()) {
1233                         return lastChild.requestFocus();
1234                     }
1235                     return false;
1236                 }
1237             });
1238         } else {
1239             setOnKeyListener(null);
1240         }
1241     }
1242 
1243     @Override
onDrop(DragObject d, DragOptions options)1244     public void onDrop(DragObject d, DragOptions options) {
1245         // If the icon was dropped while the page was being scrolled, we need to compute
1246         // the target location again such that the icon is placed of the final page.
1247         if (!mContent.rankOnCurrentPage(mEmptyCellRank)) {
1248             // Reorder again.
1249             mTargetRank = getTargetRank(d, null);
1250 
1251             // Rearrange items immediately.
1252             mReorderAlarmListener.onAlarm(mReorderAlarm);
1253 
1254             mOnScrollHintAlarm.cancelAlarm();
1255             mScrollPauseAlarm.cancelAlarm();
1256         }
1257         mContent.completePendingPageChanges();
1258         Launcher launcher = mLauncherDelegate.getLauncher();
1259         if (launcher == null) {
1260             return;
1261         }
1262 
1263         PendingAddShortcutInfo pasi = d.dragInfo instanceof PendingAddShortcutInfo
1264                 ? (PendingAddShortcutInfo) d.dragInfo : null;
1265         WorkspaceItemInfo pasiSi = pasi != null ? pasi.activityInfo.createWorkspaceItemInfo() : null;
1266         if (pasi != null && pasiSi == null) {
1267             // There is no WorkspaceItemInfo, so we have to go through a configuration activity.
1268             pasi.container = mInfo.id;
1269             pasi.rank = mEmptyCellRank;
1270 
1271             launcher.addPendingItem(pasi, pasi.container, pasi.screenId, null, pasi.spanX,
1272                     pasi.spanY);
1273             d.deferDragViewCleanupPostAnimation = false;
1274             mRearrangeOnClose = true;
1275         } else {
1276             final WorkspaceItemInfo si;
1277             if (pasiSi != null) {
1278                 si = pasiSi;
1279             } else if (d.dragInfo instanceof AppInfo) {
1280                 // Came from all apps -- make a copy.
1281                 si = ((AppInfo) d.dragInfo).makeWorkspaceItem();
1282             } else {
1283                 // WorkspaceItemInfo
1284                 si = (WorkspaceItemInfo) d.dragInfo;
1285             }
1286 
1287             View currentDragView;
1288             if (mIsExternalDrag) {
1289                 currentDragView = mContent.createAndAddViewForRank(si, mEmptyCellRank);
1290 
1291                 // Actually move the item in the database if it was an external drag. Call this
1292                 // before creating the view, so that WorkspaceItemInfo is updated appropriately.
1293                 mLauncherDelegate.getModelWriter().addOrMoveItemInDatabase(
1294                         si, mInfo.id, 0, si.cellX, si.cellY);
1295                 mIsExternalDrag = false;
1296             } else {
1297                 currentDragView = mCurrentDragView;
1298                 mContent.addViewForRank(currentDragView, si, mEmptyCellRank);
1299             }
1300 
1301             if (d.dragView.hasDrawn()) {
1302                 // Temporarily reset the scale such that the animation target gets calculated
1303                 // correctly.
1304                 float scaleX = getScaleX();
1305                 float scaleY = getScaleY();
1306                 setScaleX(1.0f);
1307                 setScaleY(1.0f);
1308                 launcher.getDragLayer().animateViewIntoPosition(d.dragView, currentDragView, null);
1309                 setScaleX(scaleX);
1310                 setScaleY(scaleY);
1311             } else {
1312                 d.deferDragViewCleanupPostAnimation = false;
1313                 currentDragView.setVisibility(VISIBLE);
1314             }
1315 
1316             mItemsInvalidated = true;
1317             rearrangeChildren();
1318 
1319             // Temporarily suppress the listener, as we did all the work already here.
1320             try (SuppressInfoChanges s = new SuppressInfoChanges()) {
1321                 mInfo.add(si, mEmptyCellRank, false);
1322             }
1323 
1324             // We only need to update the locations if it doesn't get handled in
1325             // #onDropCompleted.
1326             if (d.dragSource != this) {
1327                 updateItemLocationsInDatabaseBatch(false);
1328             }
1329         }
1330 
1331         // Clear the drag info, as it is no longer being dragged.
1332         mDragInProgress = false;
1333 
1334         if (mContent.getPageCount() > 1) {
1335             // The animation has already been shown while opening the folder.
1336             mInfo.setOption(FolderInfo.FLAG_MULTI_PAGE_ANIMATION, true,
1337                     mLauncherDelegate.getModelWriter());
1338         }
1339 
1340         launcher.getStateManager().goToState(NORMAL, SPRING_LOADED_EXIT_DELAY);
1341         if (d.stateAnnouncer != null) {
1342             d.stateAnnouncer.completeAction(R.string.item_moved);
1343         }
1344         mStatsLogManager.logger().withItemInfo(d.dragInfo).withInstanceId(d.logInstanceId)
1345                 .log(LAUNCHER_ITEM_DROP_COMPLETED);
1346     }
1347 
1348     // This is used so the item doesn't immediately appear in the folder when added. In one case
1349     // we need to create the illusion that the item isn't added back to the folder yet, to
1350     // to correspond to the animation of the icon back into the folder. This is
hideItem(WorkspaceItemInfo info)1351     public void hideItem(WorkspaceItemInfo info) {
1352         View v = getViewForInfo(info);
1353         if (v != null) {
1354             v.setVisibility(INVISIBLE);
1355         }
1356     }
1357 
showItem(WorkspaceItemInfo info)1358     public void showItem(WorkspaceItemInfo info) {
1359         View v = getViewForInfo(info);
1360         if (v != null) {
1361             v.setVisibility(VISIBLE);
1362         }
1363     }
1364 
1365     @Override
onAdd(WorkspaceItemInfo item, int rank)1366     public void onAdd(WorkspaceItemInfo item, int rank) {
1367         FolderGridOrganizer verifier = new FolderGridOrganizer(
1368                 mActivityContext.getDeviceProfile().inv).setFolderInfo(mInfo);
1369         verifier.updateRankAndPos(item, rank);
1370         mLauncherDelegate.getModelWriter().addOrMoveItemInDatabase(item, mInfo.id, 0, item.cellX,
1371                 item.cellY);
1372         updateItemLocationsInDatabaseBatch(false);
1373 
1374         if (mContent.areViewsBound()) {
1375             mContent.createAndAddViewForRank(item, rank);
1376         }
1377         mItemsInvalidated = true;
1378     }
1379 
1380     @Override
onRemove(List<WorkspaceItemInfo> items)1381     public void onRemove(List<WorkspaceItemInfo> items) {
1382         mItemsInvalidated = true;
1383         items.stream().map(this::getViewForInfo).forEach(mContent::removeItem);
1384         if (mState == STATE_ANIMATING) {
1385             mRearrangeOnClose = true;
1386         } else {
1387             rearrangeChildren();
1388         }
1389         if (getItemCount() <= 1) {
1390             if (mIsOpen) {
1391                 close(true);
1392             } else {
1393                 replaceFolderWithFinalItem();
1394             }
1395         }
1396     }
1397 
getViewForInfo(final WorkspaceItemInfo item)1398     private View getViewForInfo(final WorkspaceItemInfo item) {
1399         return mContent.iterateOverItems((info, view) -> info == item);
1400     }
1401 
1402     @Override
onItemsChanged(boolean animate)1403     public void onItemsChanged(boolean animate) {
1404         updateTextViewFocus();
1405     }
1406 
1407     /**
1408      * Utility methods to iterate over items of the view
1409      */
iterateOverItems(ItemOperator op)1410     public void iterateOverItems(ItemOperator op) {
1411         mContent.iterateOverItems(op);
1412     }
1413 
1414     /**
1415      * Returns the sorted list of all the icons in the folder
1416      */
getIconsInReadingOrder()1417     public ArrayList<View> getIconsInReadingOrder() {
1418         if (mItemsInvalidated) {
1419             mItemsInReadingOrder.clear();
1420             mContent.iterateOverItems((i, v) -> !mItemsInReadingOrder.add(v));
1421             mItemsInvalidated = false;
1422         }
1423         return mItemsInReadingOrder;
1424     }
1425 
getItemsOnPage(int page)1426     public List<BubbleTextView> getItemsOnPage(int page) {
1427         ArrayList<View> allItems = getIconsInReadingOrder();
1428         int lastPage = mContent.getPageCount() - 1;
1429         int totalItemsInFolder = allItems.size();
1430         int itemsPerPage = mContent.itemsPerPage();
1431         int numItemsOnCurrentPage = page == lastPage
1432                 ? totalItemsInFolder - (itemsPerPage * page)
1433                 : itemsPerPage;
1434 
1435         int startIndex = page * itemsPerPage;
1436         int endIndex = Math.min(startIndex + numItemsOnCurrentPage, allItems.size());
1437 
1438         List<BubbleTextView> itemsOnCurrentPage = new ArrayList<>(numItemsOnCurrentPage);
1439         for (int i = startIndex; i < endIndex; ++i) {
1440             itemsOnCurrentPage.add((BubbleTextView) allItems.get(i));
1441         }
1442         return itemsOnCurrentPage;
1443     }
1444 
1445     @Override
onFocusChange(View v, boolean hasFocus)1446     public void onFocusChange(View v, boolean hasFocus) {
1447         if (v == mFolderName) {
1448             if (hasFocus) {
1449                 mFromLabelState = mInfo.getFromLabelState();
1450                 mFromTitle = mInfo.title;
1451                 startEditingFolderName();
1452             } else {
1453                 StatsLogger statsLogger = mStatsLogManager.logger()
1454                         .withItemInfo(mInfo)
1455                         .withFromState(mFromLabelState);
1456 
1457                 // If the folder label is suggested, it is logged to improve prediction model.
1458                 // When both old and new labels are logged together delimiter is used.
1459                 StringJoiner labelInfoBuilder = new StringJoiner(FOLDER_LABEL_DELIMITER);
1460                 if (mFromLabelState.equals(FromState.FROM_SUGGESTED)) {
1461                     labelInfoBuilder.add(mFromTitle);
1462                 }
1463 
1464                 ToState toLabelState;
1465                 if (mFromTitle != null && mFromTitle.equals(mInfo.title)) {
1466                     toLabelState = ToState.UNCHANGED;
1467                 } else {
1468                     toLabelState = mInfo.getToLabelState();
1469                     if (toLabelState.toString().startsWith("TO_SUGGESTION")) {
1470                         labelInfoBuilder.add(mInfo.title);
1471                     }
1472                 }
1473                 statsLogger.withToState(toLabelState);
1474 
1475                 if (labelInfoBuilder.length() > 0) {
1476                     statsLogger.withEditText(labelInfoBuilder.toString());
1477                 }
1478 
1479                 statsLogger.log(LAUNCHER_FOLDER_LABEL_UPDATED);
1480                 mFolderName.dispatchBackKey();
1481             }
1482         }
1483     }
1484 
1485     @Override
getHitRectRelativeToDragLayer(Rect outRect)1486     public void getHitRectRelativeToDragLayer(Rect outRect) {
1487         getHitRect(outRect);
1488         outRect.left -= mScrollAreaOffset;
1489         outRect.right += mScrollAreaOffset;
1490     }
1491 
1492     private class OnScrollHintListener implements OnAlarmListener {
1493 
1494         private final DragObject mDragObject;
1495 
OnScrollHintListener(DragObject object)1496         OnScrollHintListener(DragObject object) {
1497             mDragObject = object;
1498         }
1499 
1500         /**
1501          * Scroll hint has been shown long enough. Now scroll to appropriate page.
1502          */
1503         @Override
onAlarm(Alarm alarm)1504         public void onAlarm(Alarm alarm) {
1505             if (mCurrentScrollDir == SCROLL_LEFT) {
1506                 mContent.scrollLeft();
1507                 mScrollHintDir = SCROLL_NONE;
1508             } else if (mCurrentScrollDir == SCROLL_RIGHT) {
1509                 mContent.scrollRight();
1510                 mScrollHintDir = SCROLL_NONE;
1511             } else {
1512                 // This should not happen
1513                 return;
1514             }
1515             mCurrentScrollDir = SCROLL_NONE;
1516 
1517             // Pause drag event until the scrolling is finished
1518             mScrollPauseAlarm.setOnAlarmListener(new OnScrollFinishedListener(mDragObject));
1519             mScrollPauseAlarm.setAlarm(RESCROLL_DELAY);
1520         }
1521     }
1522 
1523     private class OnScrollFinishedListener implements OnAlarmListener {
1524 
1525         private final DragObject mDragObject;
1526 
OnScrollFinishedListener(DragObject object)1527         OnScrollFinishedListener(DragObject object) {
1528             mDragObject = object;
1529         }
1530 
1531         /**
1532          * Page scroll is complete.
1533          */
1534         @Override
onAlarm(Alarm alarm)1535         public void onAlarm(Alarm alarm) {
1536             // Reorder immediately on page change.
1537             onDragOver(mDragObject);
1538         }
1539     }
1540 
1541     // Compares item position based on rank and position giving priority to the rank.
1542     public static final Comparator<ItemInfo> ITEM_POS_COMPARATOR = new Comparator<ItemInfo>() {
1543 
1544         @Override
1545         public int compare(ItemInfo lhs, ItemInfo rhs) {
1546             if (lhs.rank != rhs.rank) {
1547                 return lhs.rank - rhs.rank;
1548             } else if (lhs.cellY != rhs.cellY) {
1549                 return lhs.cellY - rhs.cellY;
1550             } else {
1551                 return lhs.cellX - rhs.cellX;
1552             }
1553         }
1554     };
1555 
1556     /**
1557      * Temporary resource held while we don't want to handle info changes
1558      */
1559     private class SuppressInfoChanges implements AutoCloseable {
1560 
SuppressInfoChanges()1561         SuppressInfoChanges() {
1562             mInfo.removeListener(Folder.this);
1563         }
1564 
1565         @Override
close()1566         public void close() {
1567             mInfo.addListener(Folder.this);
1568             updateTextViewFocus();
1569         }
1570     }
1571 
1572     /**
1573      * Returns a folder which is already open or null
1574      */
getOpen(ActivityContext activityContext)1575     public static Folder getOpen(ActivityContext activityContext) {
1576         return getOpenView(activityContext, TYPE_FOLDER);
1577     }
1578 
1579     /**
1580      * Navigation bar back key or hardware input back key has been issued.
1581      */
1582     @Override
onBackPressed()1583     public boolean onBackPressed() {
1584         if (isEditingName()) {
1585             mFolderName.dispatchBackKey();
1586         } else {
1587             super.onBackPressed();
1588         }
1589         return true;
1590     }
1591 
1592     @Override
onControllerInterceptTouchEvent(MotionEvent ev)1593     public boolean onControllerInterceptTouchEvent(MotionEvent ev) {
1594         if (ev.getAction() == MotionEvent.ACTION_DOWN) {
1595             BaseDragLayer dl = (BaseDragLayer) getParent();
1596 
1597             if (isEditingName()) {
1598                 if (!dl.isEventOverView(mFolderName, ev)) {
1599                     mFolderName.dispatchBackKey();
1600                     return true;
1601                 }
1602                 return false;
1603             } else if (!dl.isEventOverView(this, ev)
1604                     && mLauncherDelegate.interceptOutsideTouch(ev, dl, this)) {
1605                 return true;
1606             }
1607         }
1608         return false;
1609     }
1610 
1611     @Override
canInterceptEventsInSystemGestureRegion()1612     public boolean canInterceptEventsInSystemGestureRegion() {
1613         return true;
1614     }
1615 
1616     /**
1617      * Alternative to using {@link #getClipToOutline()} as it only works with derivatives of
1618      * rounded rect.
1619      */
1620     @Override
setClipPath(Path clipPath)1621     public void setClipPath(Path clipPath) {
1622         mClipPath = clipPath;
1623         invalidate();
1624     }
1625 
1626     @Override
dispatchDraw(Canvas canvas)1627     protected void dispatchDraw(Canvas canvas) {
1628         if (mClipPath != null) {
1629             int count = canvas.save();
1630             canvas.clipPath(mClipPath);
1631             mBackground.draw(canvas);
1632             canvas.restoreToCount(count);
1633             super.dispatchDraw(canvas);
1634         } else {
1635             mBackground.draw(canvas);
1636             super.dispatchDraw(canvas);
1637         }
1638     }
1639 
getContent()1640     public FolderPagedView getContent() {
1641         return mContent;
1642     }
1643 
1644     /** Returns the height of the current folder's bottom edge from the bottom of the screen. */
getHeightFromBottom()1645     private int getHeightFromBottom() {
1646         BaseDragLayer.LayoutParams layoutParams = (BaseDragLayer.LayoutParams) getLayoutParams();
1647         int folderBottomPx = layoutParams.y + layoutParams.height;
1648         int windowBottomPx = mActivityContext.getDeviceProfile().heightPx;
1649 
1650         return windowBottomPx - folderBottomPx;
1651     }
1652 }
1653