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