131dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project/*
231dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project * Copyright (C) 2008 The Android Open Source Project
331dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project *
431dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project * Licensed under the Apache License, Version 2.0 (the "License");
531dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project * you may not use this file except in compliance with the License.
631dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project * You may obtain a copy of the License at
731dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project *
831dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project *      http://www.apache.org/licenses/LICENSE-2.0
931dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project *
1031dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project * Unless required by applicable law or agreed to in writing, software
1131dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project * distributed under the License is distributed on an "AS IS" BASIS,
1231dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1331dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project * See the License for the specific language governing permissions and
1431dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project * limitations under the License.
1531dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project */
1631dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
17261194387beebaa7927ec4e310274218b651494dSunny Goyalpackage com.android.launcher3.folder;
1831dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
19df2cc41acbfacd576f99483a4af1cda32ebd3d09Adam Cohenimport android.animation.Animator;
20df2cc41acbfacd576f99483a4af1cda32ebd3d09Adam Cohenimport android.animation.AnimatorListenerAdapter;
21c4fe9ea11e82fe16d60525323464fd50cf391378Adam Cohenimport android.animation.AnimatorSet;
22df2cc41acbfacd576f99483a4af1cda32ebd3d09Adam Cohenimport android.animation.ObjectAnimator;
23df2cc41acbfacd576f99483a4af1cda32ebd3d09Adam Cohenimport android.animation.PropertyValuesHolder;
247066003b2032a49ae5e59dab9b706259bdeb7e6eSunny Goyalimport android.annotation.SuppressLint;
2531dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Projectimport android.content.Context;
2676fc085d28178a5d4fb3787ede956281a2cc3179Adam Cohenimport android.content.res.Resources;
27fb5411e98278d461b78aea0663d3e85f7a9e8a15Romain Guyimport android.graphics.Rect;
287a14d0bddea652ddca855af0c413e47a6c1360b2Adam Cohenimport android.text.InputType;
29e601a43345224e3e386abc68bbea8b08b2df9570Adam Cohenimport android.text.Selection;
3031dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Projectimport android.util.AttributeSet;
313bf84d3250bf45f2a5b041245e9bd327e03e22c0Adam Cohenimport android.util.Log;
3276fc085d28178a5d4fb3787ede956281a2cc3179Adam Cohenimport android.view.ActionMode;
33de6241e08615d09485c28a1cb0ab64ad1d934346Jun Mukaiimport android.view.FocusFinder;
3476fc085d28178a5d4fb3787ede956281a2cc3179Adam Cohenimport android.view.KeyEvent;
3576fc085d28178a5d4fb3787ede956281a2cc3179Adam Cohenimport android.view.Menu;
3676fc085d28178a5d4fb3787ede956281a2cc3179Adam Cohenimport android.view.MenuItem;
3731dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Projectimport android.view.View;
384ffec48dec5da7bcf719ac0c37ee5e58f9ea2c1aSunny Goyalimport android.view.ViewDebug;
393371da0159cc54ff8ae1b1b26effb96445f208d5Adam Cohenimport android.view.accessibility.AccessibilityEvent;
40c4fe9ea11e82fe16d60525323464fd50cf391378Adam Cohenimport android.view.animation.AccelerateInterpolator;
41c1cd23b4a7744105c124c4d9f642134b5f49694fSunny Goyalimport android.view.animation.AnimationUtils;
4276fc085d28178a5d4fb3787ede956281a2cc3179Adam Cohenimport android.view.inputmethod.EditorInfo;
43df2cc41acbfacd576f99483a4af1cda32ebd3d09Adam Cohenimport android.widget.TextView;
4483a8f042adda926489494dff217c15ab696139b4Sunny Goyal
45740ac7f00e0b847b8e392800f7948d93493e11d6Sunny Goyalimport com.android.launcher3.AbstractFloatingView;
46261194387beebaa7927ec4e310274218b651494dSunny Goyalimport com.android.launcher3.Alarm;
47e393d3af369c452f3b5f5d9c0a23a83aa3aa736bSunny Goyalimport com.android.launcher3.AppInfo;
48471701162642d4cbfc5cebdd448e9a7af4c7c51fJon Mirandaimport com.android.launcher3.BubbleTextView;
49261194387beebaa7927ec4e310274218b651494dSunny Goyalimport com.android.launcher3.CellLayout;
50261194387beebaa7927ec4e310274218b651494dSunny Goyalimport com.android.launcher3.DeviceProfile;
51261194387beebaa7927ec4e310274218b651494dSunny Goyalimport com.android.launcher3.DragSource;
52261194387beebaa7927ec4e310274218b651494dSunny Goyalimport com.android.launcher3.DropTarget;
53261194387beebaa7927ec4e310274218b651494dSunny Goyalimport com.android.launcher3.ExtendedEditText;
54261194387beebaa7927ec4e310274218b651494dSunny Goyalimport com.android.launcher3.FolderInfo;
55325dc23624160689e59fbac708cf6f222b20d025Daniel Sandlerimport com.android.launcher3.FolderInfo.FolderListener;
56261194387beebaa7927ec4e310274218b651494dSunny Goyalimport com.android.launcher3.ItemInfo;
57261194387beebaa7927ec4e310274218b651494dSunny Goyalimport com.android.launcher3.Launcher;
58261194387beebaa7927ec4e310274218b651494dSunny Goyalimport com.android.launcher3.LauncherAnimUtils;
59261194387beebaa7927ec4e310274218b651494dSunny Goyalimport com.android.launcher3.LauncherSettings;
60261194387beebaa7927ec4e310274218b651494dSunny Goyalimport com.android.launcher3.LogDecelerateInterpolator;
61261194387beebaa7927ec4e310274218b651494dSunny Goyalimport com.android.launcher3.OnAlarmListener;
62f5440cbd6c0525769d24b890e16313a728831e04Sunny Goyalimport com.android.launcher3.PagedView;
63261194387beebaa7927ec4e310274218b651494dSunny Goyalimport com.android.launcher3.R;
64261194387beebaa7927ec4e310274218b651494dSunny Goyalimport com.android.launcher3.ShortcutInfo;
65d5bd67dfa9ee5fda2384a75231b7a68ceb8e9bd5Sunny Goyalimport com.android.launcher3.UninstallDropTarget.DropTargetSource;
66261194387beebaa7927ec4e310274218b651494dSunny Goyalimport com.android.launcher3.Utilities;
67bc753359f8b08e0813016eebc8392b83a4d2bd6eSunny Goyalimport com.android.launcher3.Workspace.ItemOperator;
680de011705b132eacdd10f7bfb5cdc4bd79a147e8Hyunyoung Songimport com.android.launcher3.accessibility.AccessibleDragListenerAdapter;
69fc0ce83745ea40d03b312eade8a88a07ed990030Jon Mirandaimport com.android.launcher3.anim.AnimationLayerSet;
70c06af333cbcebb183d13d004cccf5c9d69a70af0Mario Bertschlerimport com.android.launcher3.anim.CircleRevealOutlineProvider;
715c556e7226d0c78b9e2747891bea5e64c1ca20dfSunny Goyalimport com.android.launcher3.config.FeatureFlags;
72fedca43d396d6fd7c46fbb2f37dfa7cfe3b31834Vadim Tryshevimport com.android.launcher3.dragndrop.DragController;
73fedca43d396d6fd7c46fbb2f37dfa7cfe3b31834Vadim Tryshevimport com.android.launcher3.dragndrop.DragController.DragListener;
74fedca43d396d6fd7c46fbb2f37dfa7cfe3b31834Vadim Tryshevimport com.android.launcher3.dragndrop.DragLayer;
7594b510cc683a6436ae82c6d323cbd8b429561b06Sunny Goyalimport com.android.launcher3.dragndrop.DragOptions;
76c64cfdd8fa18de45fc1646c8ef2449f39ef83022Sunny Goyalimport com.android.launcher3.pageindicators.PageIndicatorDots;
776c46a6d324310bd2fc0ea223c9782ba6394512e7Sunny Goyalimport com.android.launcher3.userevent.nano.LauncherLogProto.ContainerType;
78ddec1c739ef37c3a042982b8943fe42e04b65f4cHyunyoung Songimport com.android.launcher3.userevent.nano.LauncherLogProto.Target;
79091440a9cb9d4f42406631004aa484cbb79214caAdam Cohenimport com.android.launcher3.util.Thunk;
80c476d6e192494b14b91e76cbce5c9cec01588059Jon Mirandaimport com.android.launcher3.widget.PendingAddShortcutInfo;
8183a8f042adda926489494dff217c15ab696139b4Sunny Goyal
82c0dcf597084d00e4c23a7fea5fd0738f6c095a6bAdam Cohenimport java.util.ArrayList;
833bf84d3250bf45f2a5b041245e9bd327e03e22c0Adam Cohenimport java.util.Collections;
841dd0f8bf51d4e2befe0caece0e361a96ae697f41Sunny Goyalimport java.util.Comparator;
85471701162642d4cbfc5cebdd448e9a7af4c7c51fJon Mirandaimport java.util.List;
86c0dcf597084d00e4c23a7fea5fd0738f6c095a6bAdam Cohen
8731dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project/**
8831dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project * Represents a set of icons chosen by the user or generated by the system.
8931dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project */
90740ac7f00e0b847b8e392800f7948d93493e11d6Sunny Goyalpublic class Folder extends AbstractFloatingView implements DragSource, View.OnClickListener,
91ea0818d475df26d498ce08105bc2bc5582fce5a9Adam Cohen        View.OnLongClickListener, DropTarget, FolderListener, TextView.OnEditorActionListener,
92740ac7f00e0b847b8e392800f7948d93493e11d6Sunny Goyal        View.OnFocusChangeListener, DragListener, DropTargetSource,
93740ac7f00e0b847b8e392800f7948d93493e11d6Sunny Goyal        ExtendedEditText.OnBackKeyListener {
944eac29a80b9a73465c8de54f1caec2a8098a73c6Adam Cohen    private static final String TAG = "Launcher.Folder";
95df2cc41acbfacd576f99483a4af1cda32ebd3d09Adam Cohen
96c3a609f950a713d995f1968574d8ed7b4449f415Sunny Goyal    /**
973333b0ced8e6743c41909f6f6b916f1f9ec5a004Sunny Goyal     * We avoid measuring {@link #mContent} with a 0 width or height, as this
98c3a609f950a713d995f1968574d8ed7b4449f415Sunny Goyal     * results in CellLayout being measured as UNSPECIFIED, which it does not support.
99c3a609f950a713d995f1968574d8ed7b4449f415Sunny Goyal     */
100c3a609f950a713d995f1968574d8ed7b4449f415Sunny Goyal    private static final int MIN_CONTENT_DIMEN = 5;
10131dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
102df2cc41acbfacd576f99483a4af1cda32ebd3d09Adam Cohen    static final int STATE_NONE = -1;
103df2cc41acbfacd576f99483a4af1cda32ebd3d09Adam Cohen    static final int STATE_SMALL = 0;
104df2cc41acbfacd576f99483a4af1cda32ebd3d09Adam Cohen    static final int STATE_ANIMATING = 1;
105df2cc41acbfacd576f99483a4af1cda32ebd3d09Adam Cohen    static final int STATE_OPEN = 2;
106df2cc41acbfacd576f99483a4af1cda32ebd3d09Adam Cohen
1074846193300245c8c0a1f9bde3175f273df044309Sunny Goyal    /**
1084846193300245c8c0a1f9bde3175f273df044309Sunny Goyal     * Time for which the scroll hint is shown before automatically changing page.
1094846193300245c8c0a1f9bde3175f273df044309Sunny Goyal     */
110f5440cbd6c0525769d24b890e16313a728831e04Sunny Goyal    public static final int SCROLL_HINT_DURATION = 500;
111f5440cbd6c0525769d24b890e16313a728831e04Sunny Goyal    public static final int RESCROLL_DELAY = PagedView.PAGE_SNAP_ANIMATION_DURATION + 150;
112f5440cbd6c0525769d24b890e16313a728831e04Sunny Goyal
113f5440cbd6c0525769d24b890e16313a728831e04Sunny Goyal    public static final int SCROLL_NONE = -1;
114f5440cbd6c0525769d24b890e16313a728831e04Sunny Goyal    public static final int SCROLL_LEFT = 0;
115f5440cbd6c0525769d24b890e16313a728831e04Sunny Goyal    public static final int SCROLL_RIGHT = 1;
1164846193300245c8c0a1f9bde3175f273df044309Sunny Goyal
1174846193300245c8c0a1f9bde3175f273df044309Sunny Goyal    /**
1184846193300245c8c0a1f9bde3175f273df044309Sunny Goyal     * Fraction of icon width which behave as scroll region.
1194846193300245c8c0a1f9bde3175f273df044309Sunny Goyal     */
1204846193300245c8c0a1f9bde3175f273df044309Sunny Goyal    private static final float ICON_OVERSCROLL_WIDTH_FACTOR = 0.45f;
1214846193300245c8c0a1f9bde3175f273df044309Sunny Goyal
122a07c2f5c80908e8a612f4681314a7068b92baa9fSunny Goyal    private static final int FOLDER_NAME_ANIMATION_DURATION = 633;
123b7e15adc7b67cb1d70ca72014b200dd6cbbf166aSunny Goyal
124f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen    private static final int REORDER_DELAY = 250;
1255d518fa77c56a65104f02f40269ad8e43f3431b2Adam Cohen    private static final int ON_EXIT_CLOSE_DELAY = 400;
126c3a609f950a713d995f1968574d8ed7b4449f415Sunny Goyal    private static final Rect sTempRect = new Rect();
127c3a609f950a713d995f1968574d8ed7b4449f415Sunny Goyal
128c3a609f950a713d995f1968574d8ed7b4449f415Sunny Goyal    private static String sDefaultFolderName;
129c3a609f950a713d995f1968574d8ed7b4449f415Sunny Goyal    private static String sHintText;
130c3a609f950a713d995f1968574d8ed7b4449f415Sunny Goyal
131c3a609f950a713d995f1968574d8ed7b4449f415Sunny Goyal    private final Alarm mReorderAlarm = new Alarm();
132c3a609f950a713d995f1968574d8ed7b4449f415Sunny Goyal    private final Alarm mOnExitAlarm = new Alarm();
133b863415c17aaaf6012647df5ed14803f89f94bcbSunny Goyal    private final Alarm mOnScrollHintAlarm = new Alarm();
134b863415c17aaaf6012647df5ed14803f89f94bcbSunny Goyal    @Thunk final Alarm mScrollPauseAlarm = new Alarm();
135c3a609f950a713d995f1968574d8ed7b4449f415Sunny Goyal
1368701cd58cf3b32c9d2b9269e3956b32f4726a81aJonathan Miranda    @Thunk final ArrayList<View> mItemsInReadingOrder = new ArrayList<View>();
137c3a609f950a713d995f1968574d8ed7b4449f415Sunny Goyal
138a7cddd2c9649a8bab7c860374c5d95c0a4b7135cJon Miranda    private AnimatorSet mCurrentAnimator;
139471701162642d4cbfc5cebdd448e9a7af4c7c51fJon Miranda
140c3a609f950a713d995f1968574d8ed7b4449f415Sunny Goyal    private final int mExpandDuration;
141471701162642d4cbfc5cebdd448e9a7af4c7c51fJon Miranda    public final int mMaterialExpandDuration;
142c3a609f950a713d995f1968574d8ed7b4449f415Sunny Goyal    private final int mMaterialExpandStagger;
143c3a609f950a713d995f1968574d8ed7b4449f415Sunny Goyal
144261194387beebaa7927ec4e310274218b651494dSunny Goyal    protected final Launcher mLauncher;
145c3a609f950a713d995f1968574d8ed7b4449f415Sunny Goyal    protected DragController mDragController;
146f9c184a619e4e4b82cf9e0bf318ca6d8deaaaee7Adam Cohen    public FolderInfo mInfo;
147c3a609f950a713d995f1968574d8ed7b4449f415Sunny Goyal
148261194387beebaa7927ec4e310274218b651494dSunny Goyal    @Thunk FolderIcon mFolderIcon;
149c3a609f950a713d995f1968574d8ed7b4449f415Sunny Goyal
150261194387beebaa7927ec4e310274218b651494dSunny Goyal    @Thunk FolderPagedView mContent;
151f9c184a619e4e4b82cf9e0bf318ca6d8deaaaee7Adam Cohen    public ExtendedEditText mFolderName;
152c64cfdd8fa18de45fc1646c8ef2449f39ef83022Sunny Goyal    private PageIndicatorDots mPageIndicator;
153c3a609f950a713d995f1968574d8ed7b4449f415Sunny Goyal
154290800b5b7d575fd709f244f54a5fa5b63b58876Sunny Goyal    private View mFooter;
155290800b5b7d575fd709f244f54a5fa5b63b58876Sunny Goyal    private int mFooterHeight;
156c3a609f950a713d995f1968574d8ed7b4449f415Sunny Goyal
157c3a609f950a713d995f1968574d8ed7b4449f415Sunny Goyal    // Cell ranks used for drag and drop
158091440a9cb9d4f42406631004aa484cbb79214caAdam Cohen    @Thunk int mTargetRank, mPrevTargetRank, mEmptyCellRank;
159c3a609f950a713d995f1968574d8ed7b4449f415Sunny Goyal
1604ffec48dec5da7bcf719ac0c37ee5e58f9ea2c1aSunny Goyal    @ViewDebug.ExportedProperty(category = "launcher",
1614ffec48dec5da7bcf719ac0c37ee5e58f9ea2c1aSunny Goyal            mapping = {
1624ffec48dec5da7bcf719ac0c37ee5e58f9ea2c1aSunny Goyal                    @ViewDebug.IntToString(from = STATE_NONE, to = "STATE_NONE"),
1634ffec48dec5da7bcf719ac0c37ee5e58f9ea2c1aSunny Goyal                    @ViewDebug.IntToString(from = STATE_SMALL, to = "STATE_SMALL"),
1644ffec48dec5da7bcf719ac0c37ee5e58f9ea2c1aSunny Goyal                    @ViewDebug.IntToString(from = STATE_ANIMATING, to = "STATE_ANIMATING"),
1654ffec48dec5da7bcf719ac0c37ee5e58f9ea2c1aSunny Goyal                    @ViewDebug.IntToString(from = STATE_OPEN, to = "STATE_OPEN"),
1664ffec48dec5da7bcf719ac0c37ee5e58f9ea2c1aSunny Goyal            })
167091440a9cb9d4f42406631004aa484cbb79214caAdam Cohen    @Thunk int mState = STATE_NONE;
1684ffec48dec5da7bcf719ac0c37ee5e58f9ea2c1aSunny Goyal    @ViewDebug.ExportedProperty(category = "launcher")
169c3a609f950a713d995f1968574d8ed7b4449f415Sunny Goyal    private boolean mRearrangeOnClose = false;
1707c6932140dcbd2db88d7a809ba72ee04abb5bf26Adam Cohen    boolean mItemsInvalidated = false;
171bfbfd26c627a18f8e1e3e6d0e53e78feab360203Adam Cohen    private View mCurrentDragView;
17255bd9725d5c0373b89f7b9bbd9547550ea3bbc63Jorim Jaggi    private boolean mIsExternalDrag;
17367bd9ccf856840856944815a382e4bbb0e71e949Adam Cohen    private boolean mDragInProgress = false;
17467bd9ccf856840856944815a382e4bbb0e71e949Adam Cohen    private boolean mDeleteFolderOnDropCompleted = false;
17567bd9ccf856840856944815a382e4bbb0e71e949Adam Cohen    private boolean mSuppressFolderDeletion = false;
17605e0f40e50574f50678a9e1461cb245cddc46abbAdam Cohen    private boolean mItemAddedBackToSelfViaIcon = false;
177091440a9cb9d4f42406631004aa484cbb79214caAdam Cohen    @Thunk float mFolderIconPivotX;
178091440a9cb9d4f42406631004aa484cbb79214caAdam Cohen    @Thunk float mFolderIconPivotY;
17976fc085d28178a5d4fb3787ede956281a2cc3179Adam Cohen    private boolean mIsEditingName = false;
1801960ea4a7102bd0fa03cad1ea1e3a91f8e1bd9cdAdam Cohen
1814ffec48dec5da7bcf719ac0c37ee5e58f9ea2c1aSunny Goyal    @ViewDebug.ExportedProperty(category = "launcher")
182fb91f303f5e672f9b4c4f9f8a03486b224d60cd6Adam Cohen    private boolean mDestroyed;
183fb91f303f5e672f9b4c4f9f8a03486b224d60cd6Adam Cohen
184091440a9cb9d4f42406631004aa484cbb79214caAdam Cohen    @Thunk Runnable mDeferredAction;
1851e2f465f46ded990ea30516cdb7f0fcf3280411fMichael Jurka    private boolean mDeferDropAfterUninstall;
1861e2f465f46ded990ea30516cdb7f0fcf3280411fMichael Jurka    private boolean mUninstallSuccessful;
1871e2f465f46ded990ea30516cdb7f0fcf3280411fMichael Jurka
1884846193300245c8c0a1f9bde3175f273df044309Sunny Goyal    // Folder scrolling
1894846193300245c8c0a1f9bde3175f273df044309Sunny Goyal    private int mScrollAreaOffset;
1904846193300245c8c0a1f9bde3175f273df044309Sunny Goyal
191f5440cbd6c0525769d24b890e16313a728831e04Sunny Goyal    @Thunk int mScrollHintDir = SCROLL_NONE;
192f5440cbd6c0525769d24b890e16313a728831e04Sunny Goyal    @Thunk int mCurrentScrollDir = SCROLL_NONE;
1934846193300245c8c0a1f9bde3175f273df044309Sunny Goyal
19431dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    /**
19531dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project     * Used to inflate the Workspace from XML.
19631dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project     *
19731dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project     * @param context The application's context.
198c3a609f950a713d995f1968574d8ed7b4449f415Sunny Goyal     * @param attrs The attributes set containing the Workspace's customization values.
19931dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project     */
20031dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    public Folder(Context context, AttributeSet attrs) {
20131dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        super(context, attrs);
20231dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        setAlwaysDrawnWithCacheEnabled(false);
203c3a609f950a713d995f1968574d8ed7b4449f415Sunny Goyal        Resources res = getResources();
204c4fe9ea11e82fe16d60525323464fd50cf391378Adam Cohen        mExpandDuration = res.getInteger(R.integer.config_folderExpandDuration);
205c4fe9ea11e82fe16d60525323464fd50cf391378Adam Cohen        mMaterialExpandDuration = res.getInteger(R.integer.config_materialFolderExpandDuration);
206c4fe9ea11e82fe16d60525323464fd50cf391378Adam Cohen        mMaterialExpandStagger = res.getInteger(R.integer.config_materialFolderExpandStagger);
2074ef610fc88e447ff6d6cdd382a7d064be9f739d6Adam Cohen
2084ef610fc88e447ff6d6cdd382a7d064be9f739d6Adam Cohen        if (sDefaultFolderName == null) {
2094ef610fc88e447ff6d6cdd382a7d064be9f739d6Adam Cohen            sDefaultFolderName = res.getString(R.string.folder_name);
2104ef610fc88e447ff6d6cdd382a7d064be9f739d6Adam Cohen        }
211a65beeea13c18c443f15f8d51c4c6e153a6c4ef9Adam Cohen        if (sHintText == null) {
212a65beeea13c18c443f15f8d51c4c6e153a6c4ef9Adam Cohen            sHintText = res.getString(R.string.folder_hint_text);
213a65beeea13c18c443f15f8d51c4c6e153a6c4ef9Adam Cohen        }
2142fd020860533e18c64a93d14d11cb2d34bc9cbafTony        mLauncher = Launcher.getLauncher(context);
215ac56cff1860b71d3f164aedd268703936e08fdc0Adam Cohen        // We need this view to be focusable in touch mode so that when text editing of the folder
216ac56cff1860b71d3f164aedd268703936e08fdc0Adam Cohen        // name is complete, we have something to focus on, thus hiding the cursor and giving
217c3a609f950a713d995f1968574d8ed7b4449f415Sunny Goyal        // reliable behavior when clicking the text field (since it will always gain focus on click).
218ac56cff1860b71d3f164aedd268703936e08fdc0Adam Cohen        setFocusableInTouchMode(true);
21931dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    }
22031dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
22131dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    @Override
22231dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    protected void onFinishInflate() {
22331dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        super.onFinishInflate();
224b863415c17aaaf6012647df5ed14803f89f94bcbSunny Goyal        mContent = (FolderPagedView) findViewById(R.id.folder_content);
225c3a609f950a713d995f1968574d8ed7b4449f415Sunny Goyal        mContent.setFolder(this);
226dcbcc86353e9ed52daac87f292aece667cd0ac71Sunny Goyal
227c64cfdd8fa18de45fc1646c8ef2449f39ef83022Sunny Goyal        mPageIndicator = (PageIndicatorDots) findViewById(R.id.folder_page_indicator);
22897b0d08d92c64034ba03ae8da5a8531edbd60d52Winson        mFolderName = (ExtendedEditText) findViewById(R.id.folder_name);
229740ac7f00e0b847b8e392800f7948d93493e11d6Sunny Goyal        mFolderName.setOnBackKeyListener(this);
230ea0818d475df26d498ce08105bc2bc5582fce5a9Adam Cohen        mFolderName.setOnFocusChangeListener(this);
23176fc085d28178a5d4fb3787ede956281a2cc3179Adam Cohen
2321bd9a430edd15a540cb62b5141ebfa29456add3fYohei Yukawa        if (!Utilities.ATLEAST_MARSHMALLOW) {
2331bd9a430edd15a540cb62b5141ebfa29456add3fYohei Yukawa            // We disable action mode in older OSes where floating selection menu is not yet
2341bd9a430edd15a540cb62b5141ebfa29456add3fYohei Yukawa            // available.
2351bd9a430edd15a540cb62b5141ebfa29456add3fYohei Yukawa            mFolderName.setCustomSelectionActionModeCallback(new ActionMode.Callback() {
2361bd9a430edd15a540cb62b5141ebfa29456add3fYohei Yukawa                public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
2371bd9a430edd15a540cb62b5141ebfa29456add3fYohei Yukawa                    return false;
2381bd9a430edd15a540cb62b5141ebfa29456add3fYohei Yukawa                }
2391bd9a430edd15a540cb62b5141ebfa29456add3fYohei Yukawa
2401bd9a430edd15a540cb62b5141ebfa29456add3fYohei Yukawa                public boolean onCreateActionMode(ActionMode mode, Menu menu) {
2411bd9a430edd15a540cb62b5141ebfa29456add3fYohei Yukawa                    return false;
2421bd9a430edd15a540cb62b5141ebfa29456add3fYohei Yukawa                }
2431bd9a430edd15a540cb62b5141ebfa29456add3fYohei Yukawa
2441bd9a430edd15a540cb62b5141ebfa29456add3fYohei Yukawa                public void onDestroyActionMode(ActionMode mode) {
2451bd9a430edd15a540cb62b5141ebfa29456add3fYohei Yukawa                }
2461bd9a430edd15a540cb62b5141ebfa29456add3fYohei Yukawa
2471bd9a430edd15a540cb62b5141ebfa29456add3fYohei Yukawa                public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
2481bd9a430edd15a540cb62b5141ebfa29456add3fYohei Yukawa                    return false;
2491bd9a430edd15a540cb62b5141ebfa29456add3fYohei Yukawa                }
2501bd9a430edd15a540cb62b5141ebfa29456add3fYohei Yukawa            });
2511bd9a430edd15a540cb62b5141ebfa29456add3fYohei Yukawa        }
25276fc085d28178a5d4fb3787ede956281a2cc3179Adam Cohen        mFolderName.setOnEditorActionListener(this);
2534ef610fc88e447ff6d6cdd382a7d064be9f739d6Adam Cohen        mFolderName.setSelectAllOnFocus(true);
25454d4e645b3c2b2c153e97f776e33685b292f8c2bJon Miranda        mFolderName.setInputType(mFolderName.getInputType()
25554d4e645b3c2b2c153e97f776e33685b292f8c2bJon Miranda                & ~InputType.TYPE_TEXT_FLAG_AUTO_CORRECT
25654d4e645b3c2b2c153e97f776e33685b292f8c2bJon Miranda                & ~InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS
25754d4e645b3c2b2c153e97f776e33685b292f8c2bJon Miranda                | InputType.TYPE_TEXT_FLAG_CAP_WORDS);
25854d4e645b3c2b2c153e97f776e33685b292f8c2bJon Miranda        mFolderName.forceDisableSuggestions(true);
259c3a609f950a713d995f1968574d8ed7b4449f415Sunny Goyal
260b863415c17aaaf6012647df5ed14803f89f94bcbSunny Goyal        mFooter = findViewById(R.id.folder_footer);
261b863415c17aaaf6012647df5ed14803f89f94bcbSunny Goyal
262290800b5b7d575fd709f244f54a5fa5b63b58876Sunny Goyal        // We find out how tall footer wants to be (it is set to wrap_content), so that
263c3a609f950a713d995f1968574d8ed7b4449f415Sunny Goyal        // we can allocate the appropriate amount of space for it.
264c3a609f950a713d995f1968574d8ed7b4449f415Sunny Goyal        int measureSpec = MeasureSpec.UNSPECIFIED;
265290800b5b7d575fd709f244f54a5fa5b63b58876Sunny Goyal        mFooter.measure(measureSpec, measureSpec);
266290800b5b7d575fd709f244f54a5fa5b63b58876Sunny Goyal        mFooterHeight = mFooter.getMeasuredHeight();
26731dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    }
2682801cafe62653131fdc9da402e5c44e5ffd0bf47Adam Cohen
26931dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    public void onClick(View v) {
270df2cc41acbfacd576f99483a4af1cda32ebd3d09Adam Cohen        Object tag = v.getTag();
271df2cc41acbfacd576f99483a4af1cda32ebd3d09Adam Cohen        if (tag instanceof ShortcutInfo) {
272b5fe60c8a502fb7e265e3183550478b2b2a49264Adam Cohen            mLauncher.onClick(v);
273df2cc41acbfacd576f99483a4af1cda32ebd3d09Adam Cohen        }
27431dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    }
27531dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
27631dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    public boolean onLongClick(View v) {
27736a62fe917be0a2520c457f985075fb5d3d09d1cWinson Chung        // Return if global dragging is not enabled
27836a62fe917be0a2520c457f985075fb5d3d09d1cWinson Chung        if (!mLauncher.isDraggingEnabled()) return true;
27910236d6ac7f5f787ca21736494ccb5eaf2bd7c27Tony Wickham        return startDrag(v, new DragOptions());
280e9b651eef1b9f3647eba94f833bff3fc52f5956bSunny Goyal    }
28136a62fe917be0a2520c457f985075fb5d3d09d1cWinson Chung
28294b510cc683a6436ae82c6d323cbd8b429561b06Sunny Goyal    public boolean startDrag(View v, DragOptions options) {
283df2cc41acbfacd576f99483a4af1cda32ebd3d09Adam Cohen        Object tag = v.getTag();
284df2cc41acbfacd576f99483a4af1cda32ebd3d09Adam Cohen        if (tag instanceof ShortcutInfo) {
285df2cc41acbfacd576f99483a4af1cda32ebd3d09Adam Cohen            ShortcutInfo item = (ShortcutInfo) tag;
286df2cc41acbfacd576f99483a4af1cda32ebd3d09Adam Cohen
2878701cd58cf3b32c9d2b9269e3956b32f4726a81aJonathan Miranda            mEmptyCellRank = item.rank;
288bfbfd26c627a18f8e1e3e6d0e53e78feab360203Adam Cohen            mCurrentDragView = v;
289fc53cd22c9a78708c78c85946443f23ec8c59502Adam Cohen
29094b510cc683a6436ae82c6d323cbd8b429561b06Sunny Goyal            mDragController.addDragListener(this);
29194b510cc683a6436ae82c6d323cbd8b429561b06Sunny Goyal            if (options.isAccessibleDrag) {
2920de011705b132eacdd10f7bfb5cdc4bd79a147e8Hyunyoung Song                mDragController.addDragListener(new AccessibleDragListenerAdapter(
29394b510cc683a6436ae82c6d323cbd8b429561b06Sunny Goyal                        mContent, CellLayout.FOLDER_ACCESSIBILITY_DRAG) {
29494b510cc683a6436ae82c6d323cbd8b429561b06Sunny Goyal
29594b510cc683a6436ae82c6d323cbd8b429561b06Sunny Goyal                    @Override
29694b510cc683a6436ae82c6d323cbd8b429561b06Sunny Goyal                    protected void enableAccessibleDrag(boolean enable) {
29794b510cc683a6436ae82c6d323cbd8b429561b06Sunny Goyal                        super.enableAccessibleDrag(enable);
29894b510cc683a6436ae82c6d323cbd8b429561b06Sunny Goyal                        mFooter.setImportantForAccessibility(enable
29994b510cc683a6436ae82c6d323cbd8b429561b06Sunny Goyal                                ? IMPORTANT_FOR_ACCESSIBILITY_NO_HIDE_DESCENDANTS
30094b510cc683a6436ae82c6d323cbd8b429561b06Sunny Goyal                                : IMPORTANT_FOR_ACCESSIBILITY_AUTO);
30194b510cc683a6436ae82c6d323cbd8b429561b06Sunny Goyal                    }
30294b510cc683a6436ae82c6d323cbd8b429561b06Sunny Goyal                });
30394b510cc683a6436ae82c6d323cbd8b429561b06Sunny Goyal            }
30494b510cc683a6436ae82c6d323cbd8b429561b06Sunny Goyal
30594b510cc683a6436ae82c6d323cbd8b429561b06Sunny Goyal            mLauncher.getWorkspace().beginDragShared(v, this, options);
306df2cc41acbfacd576f99483a4af1cda32ebd3d09Adam Cohen        }
30731dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        return true;
30831dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    }
30931dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
310e9b651eef1b9f3647eba94f833bff3fc52f5956bSunny Goyal    @Override
31194b510cc683a6436ae82c6d323cbd8b429561b06Sunny Goyal    public void onDragStart(DropTarget.DragObject dragObject, DragOptions options) {
31294b510cc683a6436ae82c6d323cbd8b429561b06Sunny Goyal        if (dragObject.dragSource != this) {
31394b510cc683a6436ae82c6d323cbd8b429561b06Sunny Goyal            return;
31494b510cc683a6436ae82c6d323cbd8b429561b06Sunny Goyal        }
31594b510cc683a6436ae82c6d323cbd8b429561b06Sunny Goyal
31694b510cc683a6436ae82c6d323cbd8b429561b06Sunny Goyal        mContent.removeItem(mCurrentDragView);
317e393d3af369c452f3b5f5d9c0a23a83aa3aa736bSunny Goyal        if (dragObject.dragInfo instanceof ShortcutInfo) {
318e393d3af369c452f3b5f5d9c0a23a83aa3aa736bSunny Goyal            mItemsInvalidated = true;
319e393d3af369c452f3b5f5d9c0a23a83aa3aa736bSunny Goyal
320e393d3af369c452f3b5f5d9c0a23a83aa3aa736bSunny Goyal            // We do not want to get events for the item being removed, as they will get handled
321e393d3af369c452f3b5f5d9c0a23a83aa3aa736bSunny Goyal            // when the drop completes
322e393d3af369c452f3b5f5d9c0a23a83aa3aa736bSunny Goyal            try (SuppressInfoChanges s = new SuppressInfoChanges()) {
323e393d3af369c452f3b5f5d9c0a23a83aa3aa736bSunny Goyal                mInfo.remove((ShortcutInfo) dragObject.dragInfo, true);
324e393d3af369c452f3b5f5d9c0a23a83aa3aa736bSunny Goyal            }
325e393d3af369c452f3b5f5d9c0a23a83aa3aa736bSunny Goyal        }
32694b510cc683a6436ae82c6d323cbd8b429561b06Sunny Goyal        mDragInProgress = true;
32794b510cc683a6436ae82c6d323cbd8b429561b06Sunny Goyal        mItemAddedBackToSelfViaIcon = false;
328e9b651eef1b9f3647eba94f833bff3fc52f5956bSunny Goyal    }
329e9b651eef1b9f3647eba94f833bff3fc52f5956bSunny Goyal
330e9b651eef1b9f3647eba94f833bff3fc52f5956bSunny Goyal    @Override
33194b510cc683a6436ae82c6d323cbd8b429561b06Sunny Goyal    public void onDragEnd() {
33294b510cc683a6436ae82c6d323cbd8b429561b06Sunny Goyal        if (mIsExternalDrag && mDragInProgress) {
33394b510cc683a6436ae82c6d323cbd8b429561b06Sunny Goyal            completeDragExit();
334e9b651eef1b9f3647eba94f833bff3fc52f5956bSunny Goyal        }
335544946ae80af996e6f13f559964fabb23e4feae4Jon Miranda        mDragInProgress = false;
33694b510cc683a6436ae82c6d323cbd8b429561b06Sunny Goyal        mDragController.removeDragListener(this);
337e9b651eef1b9f3647eba94f833bff3fc52f5956bSunny Goyal    }
338e9b651eef1b9f3647eba94f833bff3fc52f5956bSunny Goyal
33976fc085d28178a5d4fb3787ede956281a2cc3179Adam Cohen    public boolean isEditingName() {
34076fc085d28178a5d4fb3787ede956281a2cc3179Adam Cohen        return mIsEditingName;
34176fc085d28178a5d4fb3787ede956281a2cc3179Adam Cohen    }
34276fc085d28178a5d4fb3787ede956281a2cc3179Adam Cohen
34376fc085d28178a5d4fb3787ede956281a2cc3179Adam Cohen    public void startEditingFolderName() {
344b0efe3f9b20645b75a212cea67d24717d6908104Adam Cohen        post(new Runnable() {
345b0efe3f9b20645b75a212cea67d24717d6908104Adam Cohen            @Override
346b0efe3f9b20645b75a212cea67d24717d6908104Adam Cohen            public void run() {
347b0efe3f9b20645b75a212cea67d24717d6908104Adam Cohen                mFolderName.setHint("");
348b0efe3f9b20645b75a212cea67d24717d6908104Adam Cohen                mIsEditingName = true;
349b0efe3f9b20645b75a212cea67d24717d6908104Adam Cohen            }
350b0efe3f9b20645b75a212cea67d24717d6908104Adam Cohen        });
35176fc085d28178a5d4fb3787ede956281a2cc3179Adam Cohen    }
35276fc085d28178a5d4fb3787ede956281a2cc3179Adam Cohen
35376fc085d28178a5d4fb3787ede956281a2cc3179Adam Cohen
354740ac7f00e0b847b8e392800f7948d93493e11d6Sunny Goyal    @Override
355740ac7f00e0b847b8e392800f7948d93493e11d6Sunny Goyal    public boolean onBackKey() {
3561df26a368a135395b7bd61e9b109044b33b1b0beAdam Cohen        // Convert to a string here to ensure that no other state associated with the text field
3571df26a368a135395b7bd61e9b109044b33b1b0beAdam Cohen        // gets saved.
35890cb3e51af988d526912aed7a532745d729a844cSunny Goyal        String newTitle = mFolderName.getText().toString();
35990cb3e51af988d526912aed7a532745d729a844cSunny Goyal        mInfo.setTitle(newTitle);
36043bf11d9c95f76c2dfeb625b23cb458df81252b3Sunny Goyal        mLauncher.getModelWriter().updateItemInDatabase(mInfo);
361ac56cff1860b71d3f164aedd268703936e08fdc0Adam Cohen
3627d18dd8f07151262cb7dcbb3f70f4d98d88f2fa0Jon Miranda        mFolderName.setHint(sDefaultFolderName.contentEquals(newTitle) ? sHintText : null);
3637d18dd8f07151262cb7dcbb3f70f4d98d88f2fa0Jon Miranda
364740ac7f00e0b847b8e392800f7948d93493e11d6Sunny Goyal        Utilities.sendCustomAccessibilityEvent(
365740ac7f00e0b847b8e392800f7948d93493e11d6Sunny Goyal                this, AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED,
366740ac7f00e0b847b8e392800f7948d93493e11d6Sunny Goyal                getContext().getString(R.string.folder_renamed, newTitle));
367feec2171f84f465feab96631c9d067d032aa8f88Tony Wickham
368feec2171f84f465feab96631c9d067d032aa8f88Tony Wickham        // This ensures that focus is gained every time the field is clicked, which selects all
369feec2171f84f465feab96631c9d067d032aa8f88Tony Wickham        // the text and brings up the soft keyboard if necessary.
370feec2171f84f465feab96631c9d067d032aa8f88Tony Wickham        mFolderName.clearFocus();
371ac56cff1860b71d3f164aedd268703936e08fdc0Adam Cohen
372740ac7f00e0b847b8e392800f7948d93493e11d6Sunny Goyal        Selection.setSelection(mFolderName.getText(), 0, 0);
37376fc085d28178a5d4fb3787ede956281a2cc3179Adam Cohen        mIsEditingName = false;
374740ac7f00e0b847b8e392800f7948d93493e11d6Sunny Goyal        return true;
37576fc085d28178a5d4fb3787ede956281a2cc3179Adam Cohen    }
37676fc085d28178a5d4fb3787ede956281a2cc3179Adam Cohen
37776fc085d28178a5d4fb3787ede956281a2cc3179Adam Cohen    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
37876fc085d28178a5d4fb3787ede956281a2cc3179Adam Cohen        if (actionId == EditorInfo.IME_ACTION_DONE) {
379740ac7f00e0b847b8e392800f7948d93493e11d6Sunny Goyal            mFolderName.dispatchBackKey();
38076fc085d28178a5d4fb3787ede956281a2cc3179Adam Cohen            return true;
38176fc085d28178a5d4fb3787ede956281a2cc3179Adam Cohen        }
38276fc085d28178a5d4fb3787ede956281a2cc3179Adam Cohen        return false;
38376fc085d28178a5d4fb3787ede956281a2cc3179Adam Cohen    }
38476fc085d28178a5d4fb3787ede956281a2cc3179Adam Cohen
385740ac7f00e0b847b8e392800f7948d93493e11d6Sunny Goyal    @Override
386740ac7f00e0b847b8e392800f7948d93493e11d6Sunny Goyal    public ExtendedEditText getActiveTextView() {
387740ac7f00e0b847b8e392800f7948d93493e11d6Sunny Goyal        return isEditingName() ? mFolderName : null;
38876fc085d28178a5d4fb3787ede956281a2cc3179Adam Cohen    }
38976fc085d28178a5d4fb3787ede956281a2cc3179Adam Cohen
390feba90fe802cb54e02dd961dbea265c044ad5f9eJon Miranda    public FolderIcon getFolderIcon() {
391feba90fe802cb54e02dd961dbea265c044ad5f9eJon Miranda        return mFolderIcon;
392feba90fe802cb54e02dd961dbea265c044ad5f9eJon Miranda    }
393feba90fe802cb54e02dd961dbea265c044ad5f9eJon Miranda
39400acb123c5100f06b8e89e8ec8978ebafc6f6d26Joe Onorato    public void setDragController(DragController dragController) {
39500acb123c5100f06b8e89e8ec8978ebafc6f6d26Joe Onorato        mDragController = dragController;
39631dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    }
39731dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
398c3a609f950a713d995f1968574d8ed7b4449f415Sunny Goyal    public void setFolderIcon(FolderIcon icon) {
3992801cafe62653131fdc9da402e5c44e5ffd0bf47Adam Cohen        mFolderIcon = icon;
4002801cafe62653131fdc9da402e5c44e5ffd0bf47Adam Cohen    }
4012801cafe62653131fdc9da402e5c44e5ffd0bf47Adam Cohen
4023371da0159cc54ff8ae1b1b26effb96445f208d5Adam Cohen    @Override
403de6241e08615d09485c28a1cb0ab64ad1d934346Jun Mukai    protected void onAttachedToWindow() {
404de6241e08615d09485c28a1cb0ab64ad1d934346Jun Mukai        // requestFocus() causes the focus onto the folder itself, which doesn't cause visual
405de6241e08615d09485c28a1cb0ab64ad1d934346Jun Mukai        // effect but the next arrow key can start the keyboard focus inside of the folder, not
406de6241e08615d09485c28a1cb0ab64ad1d934346Jun Mukai        // the folder itself.
407de6241e08615d09485c28a1cb0ab64ad1d934346Jun Mukai        requestFocus();
408de6241e08615d09485c28a1cb0ab64ad1d934346Jun Mukai        super.onAttachedToWindow();
409de6241e08615d09485c28a1cb0ab64ad1d934346Jun Mukai    }
410de6241e08615d09485c28a1cb0ab64ad1d934346Jun Mukai
411de6241e08615d09485c28a1cb0ab64ad1d934346Jun Mukai    @Override
4123371da0159cc54ff8ae1b1b26effb96445f208d5Adam Cohen    public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
4133371da0159cc54ff8ae1b1b26effb96445f208d5Adam Cohen        // When the folder gets focus, we don't want to announce the list of items.
4143371da0159cc54ff8ae1b1b26effb96445f208d5Adam Cohen        return true;
4153371da0159cc54ff8ae1b1b26effb96445f208d5Adam Cohen    }
4163371da0159cc54ff8ae1b1b26effb96445f208d5Adam Cohen
417de6241e08615d09485c28a1cb0ab64ad1d934346Jun Mukai    @Override
418de6241e08615d09485c28a1cb0ab64ad1d934346Jun Mukai    public View focusSearch(int direction) {
419de6241e08615d09485c28a1cb0ab64ad1d934346Jun Mukai        // When the folder is focused, further focus search should be within the folder contents.
420de6241e08615d09485c28a1cb0ab64ad1d934346Jun Mukai        return FocusFinder.getInstance().findNextFocus(this, null, direction);
421de6241e08615d09485c28a1cb0ab64ad1d934346Jun Mukai    }
422de6241e08615d09485c28a1cb0ab64ad1d934346Jun Mukai
42331dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    /**
42431dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project     * @return the FolderInfo object associated with this folder
42531dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project     */
42683a8f042adda926489494dff217c15ab696139b4Sunny Goyal    public FolderInfo getInfo() {
42731dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        return mInfo;
42831dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    }
42931dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
43031dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    void bind(FolderInfo info) {
43131dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        mInfo = info;
432df2cc41acbfacd576f99483a4af1cda32ebd3d09Adam Cohen        ArrayList<ShortcutInfo> children = info.contents;
4331dd0f8bf51d4e2befe0caece0e361a96ae697f41Sunny Goyal        Collections.sort(children, ITEM_POS_COMPARATOR);
434c508b2d701ef980875f7ebc71bf2d87781159478Adam Cohen
435c3a609f950a713d995f1968574d8ed7b4449f415Sunny Goyal        ArrayList<ShortcutInfo> overflow = mContent.bindItems(children);
4360057bbcfa4a2ee12c5e87cba8c3fac8eadb078edAdam Cohen
4374ff73887f28b831e8da38f2c417d55350ad88956Jason Monk        // If our folder has too many items we prune them from the list. This is an issue
438c508b2d701ef980875f7ebc71bf2d87781159478Adam Cohen        // when upgrading from the old Folders implementation which could contain an unlimited
439c508b2d701ef980875f7ebc71bf2d87781159478Adam Cohen        // number of items.
440c52ba710053e4c6927937dd1a26d1abe06b6fa99Sunny Goyal        // TODO: Remove this, as with multi-page folders, there will never be any overflow
441c508b2d701ef980875f7ebc71bf2d87781159478Adam Cohen        for (ShortcutInfo item: overflow) {
442c52ba710053e4c6927937dd1a26d1abe06b6fa99Sunny Goyal            mInfo.remove(item, false);
44343bf11d9c95f76c2dfeb625b23cb458df81252b3Sunny Goyal            mLauncher.getModelWriter().deleteItemFromDatabase(item);
444c508b2d701ef980875f7ebc71bf2d87781159478Adam Cohen        }
445c508b2d701ef980875f7ebc71bf2d87781159478Adam Cohen
446c3a609f950a713d995f1968574d8ed7b4449f415Sunny Goyal        DragLayer.LayoutParams lp = (DragLayer.LayoutParams) getLayoutParams();
447c3a609f950a713d995f1968574d8ed7b4449f415Sunny Goyal        if (lp == null) {
448c3a609f950a713d995f1968574d8ed7b4449f415Sunny Goyal            lp = new DragLayer.LayoutParams(0, 0);
449c3a609f950a713d995f1968574d8ed7b4449f415Sunny Goyal            lp.customPosition = true;
450c3a609f950a713d995f1968574d8ed7b4449f415Sunny Goyal            setLayoutParams(lp);
451c3a609f950a713d995f1968574d8ed7b4449f415Sunny Goyal        }
452c3a609f950a713d995f1968574d8ed7b4449f415Sunny Goyal        centerAboutIcon();
453c3a609f950a713d995f1968574d8ed7b4449f415Sunny Goyal
4544dbe6d993485a702505ee4895db94aaa89655f2cAdam Cohen        mItemsInvalidated = true;
455ac56cff1860b71d3f164aedd268703936e08fdc0Adam Cohen        updateTextViewFocus();
45690cb3e51af988d526912aed7a532745d729a844cSunny Goyal        mInfo.addListener(this);
4574ef610fc88e447ff6d6cdd382a7d064be9f739d6Adam Cohen
458afb01ee74243cede19088e694ca82cea5983c603Adam Cohen        if (!sDefaultFolderName.contentEquals(mInfo.title)) {
4594ef610fc88e447ff6d6cdd382a7d064be9f739d6Adam Cohen            mFolderName.setText(mInfo.title);
4607d18dd8f07151262cb7dcbb3f70f4d98d88f2fa0Jon Miranda            mFolderName.setHint(null);
4614ef610fc88e447ff6d6cdd382a7d064be9f739d6Adam Cohen        } else {
4624ef610fc88e447ff6d6cdd382a7d064be9f739d6Adam Cohen            mFolderName.setText("");
4637d18dd8f07151262cb7dcbb3f70f4d98d88f2fa0Jon Miranda            mFolderName.setHint(sHintText);
4644ef610fc88e447ff6d6cdd382a7d064be9f739d6Adam Cohen        }
46533231f587e1a6f716af68e6198df774d2847a8e5Winson Chung
46633231f587e1a6f716af68e6198df774d2847a8e5Winson Chung        // In case any children didn't come across during loading, clean up the folder accordingly
46733231f587e1a6f716af68e6198df774d2847a8e5Winson Chung        mFolderIcon.post(new Runnable() {
46833231f587e1a6f716af68e6198df774d2847a8e5Winson Chung            public void run() {
46933231f587e1a6f716af68e6198df774d2847a8e5Winson Chung                if (getItemCount() <= 1) {
47033231f587e1a6f716af68e6198df774d2847a8e5Winson Chung                    replaceFolderWithFinalItem();
47133231f587e1a6f716af68e6198df774d2847a8e5Winson Chung                }
47233231f587e1a6f716af68e6198df774d2847a8e5Winson Chung            }
47333231f587e1a6f716af68e6198df774d2847a8e5Winson Chung        });
474df2cc41acbfacd576f99483a4af1cda32ebd3d09Adam Cohen    }
475df2cc41acbfacd576f99483a4af1cda32ebd3d09Adam Cohen
476df2cc41acbfacd576f99483a4af1cda32ebd3d09Adam Cohen    /**
477df2cc41acbfacd576f99483a4af1cda32ebd3d09Adam Cohen     * Creates a new UserFolder, inflated from R.layout.user_folder.
478df2cc41acbfacd576f99483a4af1cda32ebd3d09Adam Cohen     *
479935fca185741e51ab634e5df63c6369d2d55dba5Sunny Goyal     * @param launcher The main activity.
480df2cc41acbfacd576f99483a4af1cda32ebd3d09Adam Cohen     *
481df2cc41acbfacd576f99483a4af1cda32ebd3d09Adam Cohen     * @return A new UserFolder.
482df2cc41acbfacd576f99483a4af1cda32ebd3d09Adam Cohen     */
483c23da84a870c12c06fb61fd066073332155f48edSunny Goyal    @SuppressLint("InflateParams")
4847066003b2032a49ae5e59dab9b706259bdeb7e6eSunny Goyal    static Folder fromXml(Launcher launcher) {
4855c556e7226d0c78b9e2747891bea5e64c1ca20dfSunny Goyal        return (Folder) launcher.getLayoutInflater().inflate(
486531cda693d07eba9dce6b793848749acaf28c0fdSunny Goyal                FeatureFlags.LAUNCHER3_DISABLE_ICON_NORMALIZATION
487531cda693d07eba9dce6b793848749acaf28c0fdSunny Goyal                        ? R.layout.user_folder : R.layout.user_folder_icon_normalized, null);
488df2cc41acbfacd576f99483a4af1cda32ebd3d09Adam Cohen    }
489df2cc41acbfacd576f99483a4af1cda32ebd3d09Adam Cohen
490df2cc41acbfacd576f99483a4af1cda32ebd3d09Adam Cohen    /**
491df2cc41acbfacd576f99483a4af1cda32ebd3d09Adam Cohen     * This method is intended to make the UserFolder to be visually identical in size and position
492df2cc41acbfacd576f99483a4af1cda32ebd3d09Adam Cohen     * to its associated FolderIcon. This allows for a seamless transition into the expanded state.
493df2cc41acbfacd576f99483a4af1cda32ebd3d09Adam Cohen     */
494df2cc41acbfacd576f99483a4af1cda32ebd3d09Adam Cohen    private void positionAndSizeAsIcon() {
4953e8f811f31a2275e778244427271f77f4116c08bAdam Cohen        if (!(getParent() instanceof DragLayer)) return;
496662b598c86a1772d95303ef9c921d4bbe342387bAdam Cohen        setScaleX(0.8f);
497662b598c86a1772d95303ef9c921d4bbe342387bAdam Cohen        setScaleY(0.8f);
498662b598c86a1772d95303ef9c921d4bbe342387bAdam Cohen        setAlpha(0f);
499df2cc41acbfacd576f99483a4af1cda32ebd3d09Adam Cohen        mState = STATE_SMALL;
500df2cc41acbfacd576f99483a4af1cda32ebd3d09Adam Cohen    }
501df2cc41acbfacd576f99483a4af1cda32ebd3d09Adam Cohen
502c4fe9ea11e82fe16d60525323464fd50cf391378Adam Cohen    private void prepareReveal() {
503c4fe9ea11e82fe16d60525323464fd50cf391378Adam Cohen        setScaleX(1f);
504c4fe9ea11e82fe16d60525323464fd50cf391378Adam Cohen        setScaleY(1f);
505c4fe9ea11e82fe16d60525323464fd50cf391378Adam Cohen        setAlpha(1f);
506c4fe9ea11e82fe16d60525323464fd50cf391378Adam Cohen        mState = STATE_SMALL;
507c4fe9ea11e82fe16d60525323464fd50cf391378Adam Cohen    }
5083e8f811f31a2275e778244427271f77f4116c08bAdam Cohen
509a7cddd2c9649a8bab7c860374c5d95c0a4b7135cJon Miranda    private void startAnimation(final AnimatorSet a) {
510a7cddd2c9649a8bab7c860374c5d95c0a4b7135cJon Miranda        if (mCurrentAnimator != null && mCurrentAnimator.isRunning()) {
511a7cddd2c9649a8bab7c860374c5d95c0a4b7135cJon Miranda            mCurrentAnimator.cancel();
512a7cddd2c9649a8bab7c860374c5d95c0a4b7135cJon Miranda        }
513a7cddd2c9649a8bab7c860374c5d95c0a4b7135cJon Miranda        a.addListener(new AnimatorListenerAdapter() {
514a7cddd2c9649a8bab7c860374c5d95c0a4b7135cJon Miranda            @Override
515a7cddd2c9649a8bab7c860374c5d95c0a4b7135cJon Miranda            public void onAnimationStart(Animator animation) {
516a7cddd2c9649a8bab7c860374c5d95c0a4b7135cJon Miranda                mState = STATE_ANIMATING;
517a7cddd2c9649a8bab7c860374c5d95c0a4b7135cJon Miranda                mCurrentAnimator = a;
518a7cddd2c9649a8bab7c860374c5d95c0a4b7135cJon Miranda            }
519a7cddd2c9649a8bab7c860374c5d95c0a4b7135cJon Miranda
520a7cddd2c9649a8bab7c860374c5d95c0a4b7135cJon Miranda            @Override
521a7cddd2c9649a8bab7c860374c5d95c0a4b7135cJon Miranda            public void onAnimationEnd(Animator animation) {
522a7cddd2c9649a8bab7c860374c5d95c0a4b7135cJon Miranda                mCurrentAnimator = null;
523a7cddd2c9649a8bab7c860374c5d95c0a4b7135cJon Miranda            }
524a7cddd2c9649a8bab7c860374c5d95c0a4b7135cJon Miranda        });
525a7cddd2c9649a8bab7c860374c5d95c0a4b7135cJon Miranda        a.start();
526a7cddd2c9649a8bab7c860374c5d95c0a4b7135cJon Miranda    }
527a7cddd2c9649a8bab7c860374c5d95c0a4b7135cJon Miranda
528a7cddd2c9649a8bab7c860374c5d95c0a4b7135cJon Miranda    private AnimatorSet getOpeningAnimator() {
529471701162642d4cbfc5cebdd448e9a7af4c7c51fJon Miranda        prepareReveal();
530471701162642d4cbfc5cebdd448e9a7af4c7c51fJon Miranda        mFolderIcon.growAndFadeOut();
531471701162642d4cbfc5cebdd448e9a7af4c7c51fJon Miranda
532fc0ce83745ea40d03b312eade8a88a07ed990030Jon Miranda        AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
533fc0ce83745ea40d03b312eade8a88a07ed990030Jon Miranda
534fc0ce83745ea40d03b312eade8a88a07ed990030Jon Miranda        int width = getFolderWidth();
535fc0ce83745ea40d03b312eade8a88a07ed990030Jon Miranda        int height = getFolderHeight();
536fc0ce83745ea40d03b312eade8a88a07ed990030Jon Miranda
537fc0ce83745ea40d03b312eade8a88a07ed990030Jon Miranda        float transX = - 0.075f * (width / 2 - getPivotX());
538fc0ce83745ea40d03b312eade8a88a07ed990030Jon Miranda        float transY = - 0.075f * (height / 2 - getPivotY());
539fc0ce83745ea40d03b312eade8a88a07ed990030Jon Miranda        setTranslationX(transX);
540fc0ce83745ea40d03b312eade8a88a07ed990030Jon Miranda        setTranslationY(transY);
541fc0ce83745ea40d03b312eade8a88a07ed990030Jon Miranda        PropertyValuesHolder tx = PropertyValuesHolder.ofFloat(TRANSLATION_X, transX, 0);
542fc0ce83745ea40d03b312eade8a88a07ed990030Jon Miranda        PropertyValuesHolder ty = PropertyValuesHolder.ofFloat(TRANSLATION_Y, transY, 0);
543fc0ce83745ea40d03b312eade8a88a07ed990030Jon Miranda
544fc0ce83745ea40d03b312eade8a88a07ed990030Jon Miranda        Animator drift = ObjectAnimator.ofPropertyValuesHolder(this, tx, ty);
545fc0ce83745ea40d03b312eade8a88a07ed990030Jon Miranda        drift.setDuration(mMaterialExpandDuration);
546fc0ce83745ea40d03b312eade8a88a07ed990030Jon Miranda        drift.setStartDelay(mMaterialExpandStagger);
547fc0ce83745ea40d03b312eade8a88a07ed990030Jon Miranda        drift.setInterpolator(new LogDecelerateInterpolator(100, 0));
548fc0ce83745ea40d03b312eade8a88a07ed990030Jon Miranda
549fc0ce83745ea40d03b312eade8a88a07ed990030Jon Miranda        int rx = (int) Math.max(Math.max(width - getPivotX(), 0), getPivotX());
550fc0ce83745ea40d03b312eade8a88a07ed990030Jon Miranda        int ry = (int) Math.max(Math.max(height - getPivotY(), 0), getPivotY());
551fc0ce83745ea40d03b312eade8a88a07ed990030Jon Miranda        float radius = (float) Math.hypot(rx, ry);
552fc0ce83745ea40d03b312eade8a88a07ed990030Jon Miranda
553fc0ce83745ea40d03b312eade8a88a07ed990030Jon Miranda        Animator reveal = new CircleRevealOutlineProvider((int) getPivotX(),
554fc0ce83745ea40d03b312eade8a88a07ed990030Jon Miranda                (int) getPivotY(), 0, radius).createRevealAnimator(this);
555fc0ce83745ea40d03b312eade8a88a07ed990030Jon Miranda        reveal.setDuration(mMaterialExpandDuration);
556fc0ce83745ea40d03b312eade8a88a07ed990030Jon Miranda        reveal.setInterpolator(new LogDecelerateInterpolator(100, 0));
557fc0ce83745ea40d03b312eade8a88a07ed990030Jon Miranda
558fc0ce83745ea40d03b312eade8a88a07ed990030Jon Miranda        mContent.setAlpha(0f);
559fc0ce83745ea40d03b312eade8a88a07ed990030Jon Miranda        Animator iconsAlpha = ObjectAnimator.ofFloat(mContent, "alpha", 0f, 1f);
560fc0ce83745ea40d03b312eade8a88a07ed990030Jon Miranda        iconsAlpha.setDuration(mMaterialExpandDuration);
561fc0ce83745ea40d03b312eade8a88a07ed990030Jon Miranda        iconsAlpha.setStartDelay(mMaterialExpandStagger);
562fc0ce83745ea40d03b312eade8a88a07ed990030Jon Miranda        iconsAlpha.setInterpolator(new AccelerateInterpolator(1.5f));
563fc0ce83745ea40d03b312eade8a88a07ed990030Jon Miranda
564fc0ce83745ea40d03b312eade8a88a07ed990030Jon Miranda        mFooter.setAlpha(0f);
565fc0ce83745ea40d03b312eade8a88a07ed990030Jon Miranda        Animator textAlpha = ObjectAnimator.ofFloat(mFooter, "alpha", 0f, 1f);
566fc0ce83745ea40d03b312eade8a88a07ed990030Jon Miranda        textAlpha.setDuration(mMaterialExpandDuration);
567fc0ce83745ea40d03b312eade8a88a07ed990030Jon Miranda        textAlpha.setStartDelay(mMaterialExpandStagger);
568fc0ce83745ea40d03b312eade8a88a07ed990030Jon Miranda        textAlpha.setInterpolator(new AccelerateInterpolator(1.5f));
569fc0ce83745ea40d03b312eade8a88a07ed990030Jon Miranda
570fc0ce83745ea40d03b312eade8a88a07ed990030Jon Miranda        anim.play(drift);
571fc0ce83745ea40d03b312eade8a88a07ed990030Jon Miranda        anim.play(iconsAlpha);
572fc0ce83745ea40d03b312eade8a88a07ed990030Jon Miranda        anim.play(textAlpha);
573fc0ce83745ea40d03b312eade8a88a07ed990030Jon Miranda        anim.play(reveal);
574fc0ce83745ea40d03b312eade8a88a07ed990030Jon Miranda
575fc0ce83745ea40d03b312eade8a88a07ed990030Jon Miranda        AnimationLayerSet layerSet = new AnimationLayerSet();
576fc0ce83745ea40d03b312eade8a88a07ed990030Jon Miranda        layerSet.addView(mContent);
577fc0ce83745ea40d03b312eade8a88a07ed990030Jon Miranda        layerSet.addView(mFooter);
578fc0ce83745ea40d03b312eade8a88a07ed990030Jon Miranda        anim.addListener(layerSet);
579fc0ce83745ea40d03b312eade8a88a07ed990030Jon Miranda
580fc0ce83745ea40d03b312eade8a88a07ed990030Jon Miranda        return anim;
581fc0ce83745ea40d03b312eade8a88a07ed990030Jon Miranda    }
582fc0ce83745ea40d03b312eade8a88a07ed990030Jon Miranda
583740ac7f00e0b847b8e392800f7948d93493e11d6Sunny Goyal    /**
584740ac7f00e0b847b8e392800f7948d93493e11d6Sunny Goyal     * Opens the user folder described by the specified tag. The opening of the folder
585740ac7f00e0b847b8e392800f7948d93493e11d6Sunny Goyal     * is animated relative to the specified View. If the View is null, no animation
586740ac7f00e0b847b8e392800f7948d93493e11d6Sunny Goyal     * is played.
587740ac7f00e0b847b8e392800f7948d93493e11d6Sunny Goyal     */
588c4fe9ea11e82fe16d60525323464fd50cf391378Adam Cohen    public void animateOpen() {
589740ac7f00e0b847b8e392800f7948d93493e11d6Sunny Goyal        Folder openFolder = getOpen(mLauncher);
590740ac7f00e0b847b8e392800f7948d93493e11d6Sunny Goyal        if (openFolder != null && openFolder != this) {
591740ac7f00e0b847b8e392800f7948d93493e11d6Sunny Goyal            // Close any open folder before opening a folder.
592740ac7f00e0b847b8e392800f7948d93493e11d6Sunny Goyal            openFolder.close(true);
593740ac7f00e0b847b8e392800f7948d93493e11d6Sunny Goyal        }
594740ac7f00e0b847b8e392800f7948d93493e11d6Sunny Goyal
595740ac7f00e0b847b8e392800f7948d93493e11d6Sunny Goyal        DragLayer dragLayer = mLauncher.getDragLayer();
596740ac7f00e0b847b8e392800f7948d93493e11d6Sunny Goyal        // Just verify that the folder hasn't already been added to the DragLayer.
597740ac7f00e0b847b8e392800f7948d93493e11d6Sunny Goyal        // There was a one-off crash where the folder had a parent already.
598740ac7f00e0b847b8e392800f7948d93493e11d6Sunny Goyal        if (getParent() == null) {
599740ac7f00e0b847b8e392800f7948d93493e11d6Sunny Goyal            dragLayer.addView(this);
600740ac7f00e0b847b8e392800f7948d93493e11d6Sunny Goyal            mDragController.addDropTarget(this);
601740ac7f00e0b847b8e392800f7948d93493e11d6Sunny Goyal        } else {
6023d706ad70365052e3224fc4f4b0e7d1f5e8abf22Sunny Goyal            if (FeatureFlags.IS_DOGFOOD_BUILD) {
603740ac7f00e0b847b8e392800f7948d93493e11d6Sunny Goyal                Log.e(TAG, "Opening folder (" + this + ") which already has a parent:"
604740ac7f00e0b847b8e392800f7948d93493e11d6Sunny Goyal                        + getParent());
605740ac7f00e0b847b8e392800f7948d93493e11d6Sunny Goyal            }
606740ac7f00e0b847b8e392800f7948d93493e11d6Sunny Goyal        }
607740ac7f00e0b847b8e392800f7948d93493e11d6Sunny Goyal
608740ac7f00e0b847b8e392800f7948d93493e11d6Sunny Goyal        mIsOpen = true;
609c4fe9ea11e82fe16d60525323464fd50cf391378Adam Cohen
610b863415c17aaaf6012647df5ed14803f89f94bcbSunny Goyal        mContent.completePendingPageChanges();
6118167dc2dcffbf4f68724bc1db2f5cfc1caf6a848Sunny Goyal        if (!mDragInProgress) {
612b863415c17aaaf6012647df5ed14803f89f94bcbSunny Goyal            // Open on the first page.
613b863415c17aaaf6012647df5ed14803f89f94bcbSunny Goyal            mContent.snapToPageImmediately(0);
6144846193300245c8c0a1f9bde3175f273df044309Sunny Goyal        }
6154846193300245c8c0a1f9bde3175f273df044309Sunny Goyal
6161a690cb785a1b1644ad4c97e8b755528b2222cfcTony Wickham        // This is set to true in close(), but isn't reset to false until onDropCompleted(). This
617bfbf7f9f4a0b300613f0ff27a4eb592d88c08325Tony Wickham        // leads to an inconsistent state if you drag out of the folder and drag back in without
6181a690cb785a1b1644ad4c97e8b755528b2222cfcTony Wickham        // dropping. One resulting issue is that replaceFolderWithFinalItem() can be called twice.
6191a690cb785a1b1644ad4c97e8b755528b2222cfcTony Wickham        mDeleteFolderOnDropCompleted = false;
6201a690cb785a1b1644ad4c97e8b755528b2222cfcTony Wickham
621c4fe9ea11e82fe16d60525323464fd50cf391378Adam Cohen        final Runnable onCompleteRunnable;
622740ac7f00e0b847b8e392800f7948d93493e11d6Sunny Goyal        centerAboutIcon();
623c4fe9ea11e82fe16d60525323464fd50cf391378Adam Cohen
624471701162642d4cbfc5cebdd448e9a7af4c7c51fJon Miranda        AnimatorSet anim = FeatureFlags.LAUNCHER3_NEW_FOLDER_ANIMATION
625a7cddd2c9649a8bab7c860374c5d95c0a4b7135cJon Miranda                ? new FolderAnimationManager(this, true /* isOpening */).getAnimator()
626a7cddd2c9649a8bab7c860374c5d95c0a4b7135cJon Miranda                : getOpeningAnimator();
627740ac7f00e0b847b8e392800f7948d93493e11d6Sunny Goyal        onCompleteRunnable = new Runnable() {
628740ac7f00e0b847b8e392800f7948d93493e11d6Sunny Goyal            @Override
629740ac7f00e0b847b8e392800f7948d93493e11d6Sunny Goyal            public void run() {
630740ac7f00e0b847b8e392800f7948d93493e11d6Sunny Goyal                mLauncher.getUserEventDispatcher().resetElapsedContainerMillis();
631740ac7f00e0b847b8e392800f7948d93493e11d6Sunny Goyal            }
632740ac7f00e0b847b8e392800f7948d93493e11d6Sunny Goyal        };
633740ac7f00e0b847b8e392800f7948d93493e11d6Sunny Goyal        anim.addListener(new AnimatorListenerAdapter() {
634df2cc41acbfacd576f99483a4af1cda32ebd3d09Adam Cohen            @Override
635df2cc41acbfacd576f99483a4af1cda32ebd3d09Adam Cohen            public void onAnimationStart(Animator animation) {
636a7cddd2c9649a8bab7c860374c5d95c0a4b7135cJon Miranda                if (FeatureFlags.LAUNCHER3_NEW_FOLDER_ANIMATION) {
637e4a252a16b106ac74ae443d7567501cbd4f43813Jon Miranda                    mFolderIcon.setBackgroundVisible(false);
638f79e54b8acb988203e20505039572dd4a5072793Jon Miranda                    mFolderIcon.drawLeaveBehindIfExists();
639e4a252a16b106ac74ae443d7567501cbd4f43813Jon Miranda                } else {
640a7cddd2c9649a8bab7c860374c5d95c0a4b7135cJon Miranda                    mFolderIcon.setVisibility(INVISIBLE);
641a7cddd2c9649a8bab7c860374c5d95c0a4b7135cJon Miranda                }
642a7cddd2c9649a8bab7c860374c5d95c0a4b7135cJon Miranda
64352851aa3fd2dd9957e190d779f904f6abaf53fedSunny Goyal                Utilities.sendCustomAccessibilityEvent(
64452851aa3fd2dd9957e190d779f904f6abaf53fedSunny Goyal                        Folder.this,
64552851aa3fd2dd9957e190d779f904f6abaf53fedSunny Goyal                        AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED,
646bc753359f8b08e0813016eebc8392b83a4d2bd6eSunny Goyal                        mContent.getAccessibilityDescription());
647df2cc41acbfacd576f99483a4af1cda32ebd3d09Adam Cohen            }
648df2cc41acbfacd576f99483a4af1cda32ebd3d09Adam Cohen            @Override
649df2cc41acbfacd576f99483a4af1cda32ebd3d09Adam Cohen            public void onAnimationEnd(Animator animation) {
6502801cafe62653131fdc9da402e5c44e5ffd0bf47Adam Cohen                mState = STATE_OPEN;
651c4fe9ea11e82fe16d60525323464fd50cf391378Adam Cohen
652b740f59c7d572d54402e3a74fb2a23c0f1b1af48Sunny Goyal                onCompleteRunnable.run();
653bc753359f8b08e0813016eebc8392b83a4d2bd6eSunny Goyal                mContent.setFocusOnFirstChild();
654df2cc41acbfacd576f99483a4af1cda32ebd3d09Adam Cohen            }
655df2cc41acbfacd576f99483a4af1cda32ebd3d09Adam Cohen        });
656b7e15adc7b67cb1d70ca72014b200dd6cbbf166aSunny Goyal
657b7e15adc7b67cb1d70ca72014b200dd6cbbf166aSunny Goyal        // Footer animation
658b7e15adc7b67cb1d70ca72014b200dd6cbbf166aSunny Goyal        if (mContent.getPageCount() > 1 && !mInfo.hasOption(FolderInfo.FLAG_MULTI_PAGE_ANIMATION)) {
659b7e15adc7b67cb1d70ca72014b200dd6cbbf166aSunny Goyal            int footerWidth = mContent.getDesiredWidth()
660b7e15adc7b67cb1d70ca72014b200dd6cbbf166aSunny Goyal                    - mFooter.getPaddingLeft() - mFooter.getPaddingRight();
661b7e15adc7b67cb1d70ca72014b200dd6cbbf166aSunny Goyal
662b7e15adc7b67cb1d70ca72014b200dd6cbbf166aSunny Goyal            float textWidth =  mFolderName.getPaint().measureText(mFolderName.getText().toString());
663a07c2f5c80908e8a612f4681314a7068b92baa9fSunny Goyal            float translation = (footerWidth - textWidth) / 2;
664a07c2f5c80908e8a612f4681314a7068b92baa9fSunny Goyal            mFolderName.setTranslationX(mContent.mIsRtl ? -translation : translation);
665c64cfdd8fa18de45fc1646c8ef2449f39ef83022Sunny Goyal            mPageIndicator.prepareEntryAnimation();
666b7e15adc7b67cb1d70ca72014b200dd6cbbf166aSunny Goyal
667b7e15adc7b67cb1d70ca72014b200dd6cbbf166aSunny Goyal            // Do not update the flag if we are in drag mode. The flag will be updated, when we
668b7e15adc7b67cb1d70ca72014b200dd6cbbf166aSunny Goyal            // actually drop the icon.
669b7e15adc7b67cb1d70ca72014b200dd6cbbf166aSunny Goyal            final boolean updateAnimationFlag = !mDragInProgress;
670740ac7f00e0b847b8e392800f7948d93493e11d6Sunny Goyal            anim.addListener(new AnimatorListenerAdapter() {
671b7e15adc7b67cb1d70ca72014b200dd6cbbf166aSunny Goyal
6725d2fc32e6da66f877dfba4fe513fbabdcdae5f99Sunny Goyal                @SuppressLint("InlinedApi")
673b7e15adc7b67cb1d70ca72014b200dd6cbbf166aSunny Goyal                @Override
674b7e15adc7b67cb1d70ca72014b200dd6cbbf166aSunny Goyal                public void onAnimationEnd(Animator animation) {
675a07c2f5c80908e8a612f4681314a7068b92baa9fSunny Goyal                    mFolderName.animate().setDuration(FOLDER_NAME_ANIMATION_DURATION)
676a07c2f5c80908e8a612f4681314a7068b92baa9fSunny Goyal                        .translationX(0)
677a52ecb0390c85afb385371bb844bb496c59ddf87Sunny Goyal                        .setInterpolator(AnimationUtils.loadInterpolator(
678a52ecb0390c85afb385371bb844bb496c59ddf87Sunny Goyal                                mLauncher, android.R.interpolator.fast_out_slow_in));
679c64cfdd8fa18de45fc1646c8ef2449f39ef83022Sunny Goyal                    mPageIndicator.playEntryAnimation();
680b7e15adc7b67cb1d70ca72014b200dd6cbbf166aSunny Goyal
681b7e15adc7b67cb1d70ca72014b200dd6cbbf166aSunny Goyal                    if (updateAnimationFlag) {
68243bf11d9c95f76c2dfeb625b23cb458df81252b3Sunny Goyal                        mInfo.setOption(FolderInfo.FLAG_MULTI_PAGE_ANIMATION, true,
68343bf11d9c95f76c2dfeb625b23cb458df81252b3Sunny Goyal                                mLauncher.getModelWriter());
684b7e15adc7b67cb1d70ca72014b200dd6cbbf166aSunny Goyal                    }
685b7e15adc7b67cb1d70ca72014b200dd6cbbf166aSunny Goyal                }
686b7e15adc7b67cb1d70ca72014b200dd6cbbf166aSunny Goyal            });
687b7e15adc7b67cb1d70ca72014b200dd6cbbf166aSunny Goyal        } else {
688b7e15adc7b67cb1d70ca72014b200dd6cbbf166aSunny Goyal            mFolderName.setTranslationX(0);
689b7e15adc7b67cb1d70ca72014b200dd6cbbf166aSunny Goyal        }
690b7e15adc7b67cb1d70ca72014b200dd6cbbf166aSunny Goyal
691c64cfdd8fa18de45fc1646c8ef2449f39ef83022Sunny Goyal        mPageIndicator.stopAllAnimations();
692a7cddd2c9649a8bab7c860374c5d95c0a4b7135cJon Miranda        startAnimation(anim);
69355bd9725d5c0373b89f7b9bbd9547550ea3bbc63Jorim Jaggi
69455bd9725d5c0373b89f7b9bbd9547550ea3bbc63Jorim Jaggi        // Make sure the folder picks up the last drag move even if the finger doesn't move.
69555bd9725d5c0373b89f7b9bbd9547550ea3bbc63Jorim Jaggi        if (mDragController.isDragging()) {
69655bd9725d5c0373b89f7b9bbd9547550ea3bbc63Jorim Jaggi            mDragController.forceTouchMove();
69708442b84a25a22f3f21aad89ed5a6adccaa2166dSunny Goyal        }
69808442b84a25a22f3f21aad89ed5a6adccaa2166dSunny Goyal
69908442b84a25a22f3f21aad89ed5a6adccaa2166dSunny Goyal        mContent.verifyVisibleHighResIcons(mContent.getNextPage());
700740ac7f00e0b847b8e392800f7948d93493e11d6Sunny Goyal
701740ac7f00e0b847b8e392800f7948d93493e11d6Sunny Goyal        // Notify the accessibility manager that this folder "window" has appeared and occluded
702740ac7f00e0b847b8e392800f7948d93493e11d6Sunny Goyal        // the workspace items
703740ac7f00e0b847b8e392800f7948d93493e11d6Sunny Goyal        sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
704740ac7f00e0b847b8e392800f7948d93493e11d6Sunny Goyal        dragLayer.sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED);
70555bd9725d5c0373b89f7b9bbd9547550ea3bbc63Jorim Jaggi    }
70655bd9725d5c0373b89f7b9bbd9547550ea3bbc63Jorim Jaggi
707e393d3af369c452f3b5f5d9c0a23a83aa3aa736bSunny Goyal    public void beginExternalDrag() {
7088701cd58cf3b32c9d2b9269e3956b32f4726a81aJonathan Miranda        mEmptyCellRank = mContent.allocateRankForNewItem();
70955bd9725d5c0373b89f7b9bbd9547550ea3bbc63Jorim Jaggi        mIsExternalDrag = true;
71055bd9725d5c0373b89f7b9bbd9547550ea3bbc63Jorim Jaggi        mDragInProgress = true;
711b863415c17aaaf6012647df5ed14803f89f94bcbSunny Goyal
712f406615354a92367481a60966b6b873cae37fe4cSunny Goyal        // Since this folder opened by another controller, it might not get onDrop or
713f406615354a92367481a60966b6b873cae37fe4cSunny Goyal        // onDropComplete. Perform cleanup once drag-n-drop ends.
714f406615354a92367481a60966b6b873cae37fe4cSunny Goyal        mDragController.addDragListener(this);
715f406615354a92367481a60966b6b873cae37fe4cSunny Goyal    }
716f406615354a92367481a60966b6b873cae37fe4cSunny Goyal
717740ac7f00e0b847b8e392800f7948d93493e11d6Sunny Goyal    @Override
718740ac7f00e0b847b8e392800f7948d93493e11d6Sunny Goyal    protected boolean isOfType(int type) {
719740ac7f00e0b847b8e392800f7948d93493e11d6Sunny Goyal        return (type & TYPE_FOLDER) != 0;
720740ac7f00e0b847b8e392800f7948d93493e11d6Sunny Goyal    }
721740ac7f00e0b847b8e392800f7948d93493e11d6Sunny Goyal
722740ac7f00e0b847b8e392800f7948d93493e11d6Sunny Goyal    @Override
723740ac7f00e0b847b8e392800f7948d93493e11d6Sunny Goyal    protected void handleClose(boolean animate) {
724740ac7f00e0b847b8e392800f7948d93493e11d6Sunny Goyal        mIsOpen = false;
725740ac7f00e0b847b8e392800f7948d93493e11d6Sunny Goyal
726740ac7f00e0b847b8e392800f7948d93493e11d6Sunny Goyal        if (isEditingName()) {
727740ac7f00e0b847b8e392800f7948d93493e11d6Sunny Goyal            mFolderName.dispatchBackKey();
728740ac7f00e0b847b8e392800f7948d93493e11d6Sunny Goyal        }
729740ac7f00e0b847b8e392800f7948d93493e11d6Sunny Goyal
730f79e54b8acb988203e20505039572dd4a5072793Jon Miranda        if (mFolderIcon != null) {
731f79e54b8acb988203e20505039572dd4a5072793Jon Miranda            if (FeatureFlags.LAUNCHER3_NEW_FOLDER_ANIMATION) {
732f79e54b8acb988203e20505039572dd4a5072793Jon Miranda                mFolderIcon.clearLeaveBehindIfExists();
733f79e54b8acb988203e20505039572dd4a5072793Jon Miranda            } else {
734f79e54b8acb988203e20505039572dd4a5072793Jon Miranda                mFolderIcon.shrinkAndFadeIn(animate);
735f79e54b8acb988203e20505039572dd4a5072793Jon Miranda            }
736740ac7f00e0b847b8e392800f7948d93493e11d6Sunny Goyal        }
737740ac7f00e0b847b8e392800f7948d93493e11d6Sunny Goyal
7388e776a62c5cc60737f5cdbbeacfec5f958112fa7Adam Cohen        if (!(getParent() instanceof DragLayer)) return;
739f9136dac42483a9d42f62f4a66339bf3be191336Sunny Goyal        DragLayer parent = (DragLayer) getParent();
740740ac7f00e0b847b8e392800f7948d93493e11d6Sunny Goyal
741740ac7f00e0b847b8e392800f7948d93493e11d6Sunny Goyal        if (animate) {
742740ac7f00e0b847b8e392800f7948d93493e11d6Sunny Goyal            animateClosed();
743740ac7f00e0b847b8e392800f7948d93493e11d6Sunny Goyal        } else {
744740ac7f00e0b847b8e392800f7948d93493e11d6Sunny Goyal            closeComplete(false);
745740ac7f00e0b847b8e392800f7948d93493e11d6Sunny Goyal        }
746740ac7f00e0b847b8e392800f7948d93493e11d6Sunny Goyal
747740ac7f00e0b847b8e392800f7948d93493e11d6Sunny Goyal        // Notify the accessibility manager that this folder "window" has disappeared and no
748740ac7f00e0b847b8e392800f7948d93493e11d6Sunny Goyal        // longer occludes the workspace items
749f9136dac42483a9d42f62f4a66339bf3be191336Sunny Goyal        parent.sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
750740ac7f00e0b847b8e392800f7948d93493e11d6Sunny Goyal    }
751740ac7f00e0b847b8e392800f7948d93493e11d6Sunny Goyal
752a7cddd2c9649a8bab7c860374c5d95c0a4b7135cJon Miranda    private AnimatorSet getClosingAnimator() {
753fc0ce83745ea40d03b312eade8a88a07ed990030Jon Miranda        AnimatorSet animatorSet = LauncherAnimUtils.createAnimatorSet();
754fc0ce83745ea40d03b312eade8a88a07ed990030Jon Miranda        animatorSet.play(LauncherAnimUtils.ofViewAlphaAndScale(this, 0, 0.9f, 0.9f));
755fc0ce83745ea40d03b312eade8a88a07ed990030Jon Miranda
756fc0ce83745ea40d03b312eade8a88a07ed990030Jon Miranda        AnimationLayerSet layerSet = new AnimationLayerSet();
757fc0ce83745ea40d03b312eade8a88a07ed990030Jon Miranda        layerSet.addView(this);
758fc0ce83745ea40d03b312eade8a88a07ed990030Jon Miranda        animatorSet.addListener(layerSet);
759471701162642d4cbfc5cebdd448e9a7af4c7c51fJon Miranda        animatorSet.setDuration(mExpandDuration);
760fc0ce83745ea40d03b312eade8a88a07ed990030Jon Miranda        return animatorSet;
761fc0ce83745ea40d03b312eade8a88a07ed990030Jon Miranda    }
762fc0ce83745ea40d03b312eade8a88a07ed990030Jon Miranda
763740ac7f00e0b847b8e392800f7948d93493e11d6Sunny Goyal    private void animateClosed() {
764471701162642d4cbfc5cebdd448e9a7af4c7c51fJon Miranda        AnimatorSet a = FeatureFlags.LAUNCHER3_NEW_FOLDER_ANIMATION
765a7cddd2c9649a8bab7c860374c5d95c0a4b7135cJon Miranda                ? new FolderAnimationManager(this, false /* isOpening */).getAnimator()
766a7cddd2c9649a8bab7c860374c5d95c0a4b7135cJon Miranda                : getClosingAnimator();
767fc0ce83745ea40d03b312eade8a88a07ed990030Jon Miranda        a.addListener(new AnimatorListenerAdapter() {
768df2cc41acbfacd576f99483a4af1cda32ebd3d09Adam Cohen            @Override
769df2cc41acbfacd576f99483a4af1cda32ebd3d09Adam Cohen            public void onAnimationEnd(Animator animation) {
770740ac7f00e0b847b8e392800f7948d93493e11d6Sunny Goyal                closeComplete(true);
771df2cc41acbfacd576f99483a4af1cda32ebd3d09Adam Cohen            }
772df2cc41acbfacd576f99483a4af1cda32ebd3d09Adam Cohen            @Override
773df2cc41acbfacd576f99483a4af1cda32ebd3d09Adam Cohen            public void onAnimationStart(Animator animation) {
77452851aa3fd2dd9957e190d779f904f6abaf53fedSunny Goyal                Utilities.sendCustomAccessibilityEvent(
77552851aa3fd2dd9957e190d779f904f6abaf53fedSunny Goyal                        Folder.this,
77652851aa3fd2dd9957e190d779f904f6abaf53fedSunny Goyal                        AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED,
7778b805b17158886035b38261eb611d8641701ae43Michael Jurka                        getContext().getString(R.string.folder_closed));
778df2cc41acbfacd576f99483a4af1cda32ebd3d09Adam Cohen            }
779df2cc41acbfacd576f99483a4af1cda32ebd3d09Adam Cohen        });
780a7cddd2c9649a8bab7c860374c5d95c0a4b7135cJon Miranda        startAnimation(a);
781df2cc41acbfacd576f99483a4af1cda32ebd3d09Adam Cohen    }
782df2cc41acbfacd576f99483a4af1cda32ebd3d09Adam Cohen
783740ac7f00e0b847b8e392800f7948d93493e11d6Sunny Goyal    private void closeComplete(boolean wasAnimated) {
784935fca185741e51ab634e5df63c6369d2d55dba5Sunny Goyal        // TODO: Clear all active animations.
785935fca185741e51ab634e5df63c6369d2d55dba5Sunny Goyal        DragLayer parent = (DragLayer) getParent();
786935fca185741e51ab634e5df63c6369d2d55dba5Sunny Goyal        if (parent != null) {
787935fca185741e51ab634e5df63c6369d2d55dba5Sunny Goyal            parent.removeView(this);
788935fca185741e51ab634e5df63c6369d2d55dba5Sunny Goyal        }
789935fca185741e51ab634e5df63c6369d2d55dba5Sunny Goyal        mDragController.removeDropTarget(this);
790935fca185741e51ab634e5df63c6369d2d55dba5Sunny Goyal        clearFocus();
791ac3a64dbf4a6bcde4966d4bf581b8a35d7cddf9dJon Miranda        if (mFolderIcon != null) {
792ac3a64dbf4a6bcde4966d4bf581b8a35d7cddf9dJon Miranda            mFolderIcon.setVisibility(View.VISIBLE);
793f9e8626fd0a51ddd91668bd1b658438194738743Jon Miranda            if (FeatureFlags.LAUNCHER3_NEW_FOLDER_ANIMATION) {
794f9e8626fd0a51ddd91668bd1b658438194738743Jon Miranda                mFolderIcon.setBackgroundVisible(true);
79519db4255a6c3316d4de3f0219219ddf86391bbc4Jon Miranda                mFolderIcon.mFolderName.setTextVisibility(true);
796f9e8626fd0a51ddd91668bd1b658438194738743Jon Miranda            }
797ac3a64dbf4a6bcde4966d4bf581b8a35d7cddf9dJon Miranda            if (wasAnimated) {
79827bdbcae7fc43612c6016f625038e93bb3bd1e7bJon Miranda                if (FeatureFlags.LAUNCHER3_NEW_FOLDER_ANIMATION) {
79927bdbcae7fc43612c6016f625038e93bb3bd1e7bJon Miranda                    mFolderIcon.mBackground.fadeInBackgroundShadow();
80027bdbcae7fc43612c6016f625038e93bb3bd1e7bJon Miranda                    mFolderIcon.mBackground.animateBackgroundStroke();
80127bdbcae7fc43612c6016f625038e93bb3bd1e7bJon Miranda                    mFolderIcon.onFolderClose(mContent.getCurrentPage());
80227bdbcae7fc43612c6016f625038e93bb3bd1e7bJon Miranda                }
803814deadc2c0f48e9a6153928d51399d4892d3086Tony                if (mFolderIcon.hasBadge()) {
804814deadc2c0f48e9a6153928d51399d4892d3086Tony                    mFolderIcon.createBadgeScaleAnimator(0f, 1f).start();
805814deadc2c0f48e9a6153928d51399d4892d3086Tony                }
806ac3a64dbf4a6bcde4966d4bf581b8a35d7cddf9dJon Miranda                mFolderIcon.requestFocus();
807ac3a64dbf4a6bcde4966d4bf581b8a35d7cddf9dJon Miranda            }
80830bdd6eb6994b89c99f3adaebd1de131d68d0545Tony Wickham        }
809935fca185741e51ab634e5df63c6369d2d55dba5Sunny Goyal
810935fca185741e51ab634e5df63c6369d2d55dba5Sunny Goyal        if (mRearrangeOnClose) {
811935fca185741e51ab634e5df63c6369d2d55dba5Sunny Goyal            rearrangeChildren();
812935fca185741e51ab634e5df63c6369d2d55dba5Sunny Goyal            mRearrangeOnClose = false;
813935fca185741e51ab634e5df63c6369d2d55dba5Sunny Goyal        }
814935fca185741e51ab634e5df63c6369d2d55dba5Sunny Goyal        if (getItemCount() <= 1) {
815935fca185741e51ab634e5df63c6369d2d55dba5Sunny Goyal            if (!mDragInProgress && !mSuppressFolderDeletion) {
816935fca185741e51ab634e5df63c6369d2d55dba5Sunny Goyal                replaceFolderWithFinalItem();
817935fca185741e51ab634e5df63c6369d2d55dba5Sunny Goyal            } else if (mDragInProgress) {
818935fca185741e51ab634e5df63c6369d2d55dba5Sunny Goyal                mDeleteFolderOnDropCompleted = true;
819935fca185741e51ab634e5df63c6369d2d55dba5Sunny Goyal            }
820935fca185741e51ab634e5df63c6369d2d55dba5Sunny Goyal        }
821935fca185741e51ab634e5df63c6369d2d55dba5Sunny Goyal        mSuppressFolderDeletion = false;
822935fca185741e51ab634e5df63c6369d2d55dba5Sunny Goyal        clearDragInfo();
823935fca185741e51ab634e5df63c6369d2d55dba5Sunny Goyal        mState = STATE_SMALL;
82427bdbcae7fc43612c6016f625038e93bb3bd1e7bJon Miranda        mContent.setCurrentPage(0);
825935fca185741e51ab634e5df63c6369d2d55dba5Sunny Goyal    }
826935fca185741e51ab634e5df63c6369d2d55dba5Sunny Goyal
827cb3382b1bfe1a534b1b44f5c4def9b2db605ac90Adam Cohen    public boolean acceptDrop(DragObject d) {
828aa8ef119f18864f4ab41c12f9c2ad6d7f643a0a9Sunny Goyal        final ItemInfo item = d.dragInfo;
829df2cc41acbfacd576f99483a4af1cda32ebd3d09Adam Cohen        final int itemType = item.itemType;
8302801cafe62653131fdc9da402e5c44e5ffd0bf47Adam Cohen        return ((itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION ||
831bfbf7f9f4a0b300613f0ff27a4eb592d88c08325Tony Wickham                itemType == LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT ||
832bfbf7f9f4a0b300613f0ff27a4eb592d88c08325Tony Wickham                itemType == LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT) &&
8332801cafe62653131fdc9da402e5c44e5ffd0bf47Adam Cohen                    !isFull());
834df2cc41acbfacd576f99483a4af1cda32ebd3d09Adam Cohen    }
835df2cc41acbfacd576f99483a4af1cda32ebd3d09Adam Cohen
836cb3382b1bfe1a534b1b44f5c4def9b2db605ac90Adam Cohen    public void onDragEnter(DragObject d) {
837c3a609f950a713d995f1968574d8ed7b4449f415Sunny Goyal        mPrevTargetRank = -1;
838bfbfd26c627a18f8e1e3e6d0e53e78feab360203Adam Cohen        mOnExitAlarm.cancelAlarm();
839b863415c17aaaf6012647df5ed14803f89f94bcbSunny Goyal        // Get the area offset such that the folder only closes if half the drag icon width
840b863415c17aaaf6012647df5ed14803f89f94bcbSunny Goyal        // is outside the folder area
841b863415c17aaaf6012647df5ed14803f89f94bcbSunny Goyal        mScrollAreaOffset = d.dragView.getDragRegionWidth() / 2 - d.xOffset;
842df2cc41acbfacd576f99483a4af1cda32ebd3d09Adam Cohen    }
843df2cc41acbfacd576f99483a4af1cda32ebd3d09Adam Cohen
844bfbfd26c627a18f8e1e3e6d0e53e78feab360203Adam Cohen    OnAlarmListener mReorderAlarmListener = new OnAlarmListener() {
845bfbfd26c627a18f8e1e3e6d0e53e78feab360203Adam Cohen        public void onAlarm(Alarm alarm) {
846c3a609f950a713d995f1968574d8ed7b4449f415Sunny Goyal            mContent.realTimeReorder(mEmptyCellRank, mTargetRank);
847c3a609f950a713d995f1968574d8ed7b4449f415Sunny Goyal            mEmptyCellRank = mTargetRank;
848bfbfd26c627a18f8e1e3e6d0e53e78feab360203Adam Cohen        }
849bfbfd26c627a18f8e1e3e6d0e53e78feab360203Adam Cohen    };
850bfbfd26c627a18f8e1e3e6d0e53e78feab360203Adam Cohen
8512374abfda3e53f84e005df8923170308e4df8c03Adam Cohen    public boolean isLayoutRtl() {
8522374abfda3e53f84e005df8923170308e4df8c03Adam Cohen        return (getLayoutDirection() == LAYOUT_DIRECTION_RTL);
8532374abfda3e53f84e005df8923170308e4df8c03Adam Cohen    }
8542374abfda3e53f84e005df8923170308e4df8c03Adam Cohen
8554846193300245c8c0a1f9bde3175f273df044309Sunny Goyal    @Override
856bfbfd26c627a18f8e1e3e6d0e53e78feab360203Adam Cohen    public void onDragOver(DragObject d) {
8574846193300245c8c0a1f9bde3175f273df044309Sunny Goyal        onDragOver(d, REORDER_DELAY);
8584846193300245c8c0a1f9bde3175f273df044309Sunny Goyal    }
8594846193300245c8c0a1f9bde3175f273df044309Sunny Goyal
8604846193300245c8c0a1f9bde3175f273df044309Sunny Goyal    private int getTargetRank(DragObject d, float[] recycle) {
8614846193300245c8c0a1f9bde3175f273df044309Sunny Goyal        recycle = d.getVisualCenter(recycle);
8624846193300245c8c0a1f9bde3175f273df044309Sunny Goyal        return mContent.findNearestArea(
8634846193300245c8c0a1f9bde3175f273df044309Sunny Goyal                (int) recycle[0] - getPaddingLeft(), (int) recycle[1] - getPaddingTop());
8644846193300245c8c0a1f9bde3175f273df044309Sunny Goyal    }
8654846193300245c8c0a1f9bde3175f273df044309Sunny Goyal
866091440a9cb9d4f42406631004aa484cbb79214caAdam Cohen    @Thunk void onDragOver(DragObject d, int reorderDelay) {
867b863415c17aaaf6012647df5ed14803f89f94bcbSunny Goyal        if (mScrollPauseAlarm.alarmPending()) {
8684846193300245c8c0a1f9bde3175f273df044309Sunny Goyal            return;
8694846193300245c8c0a1f9bde3175f273df044309Sunny Goyal        }
8704846193300245c8c0a1f9bde3175f273df044309Sunny Goyal        final float[] r = new float[2];
8714846193300245c8c0a1f9bde3175f273df044309Sunny Goyal        mTargetRank = getTargetRank(d, r);
872f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen
873c3a609f950a713d995f1968574d8ed7b4449f415Sunny Goyal        if (mTargetRank != mPrevTargetRank) {
8744cda5b770db7f375a4766fe6c0d657b9d5303374Alan Viverette            mReorderAlarm.cancelAlarm();
875c46bfef5dfd8c6867641bdc32c27b03e2f183726Sunny Goyal            mReorderAlarm.setOnAlarmListener(mReorderAlarmListener);
876c46bfef5dfd8c6867641bdc32c27b03e2f183726Sunny Goyal            mReorderAlarm.setAlarm(REORDER_DELAY);
877c3a609f950a713d995f1968574d8ed7b4449f415Sunny Goyal            mPrevTargetRank = mTargetRank;
878e78e3d734b577c1ab6dc0738a83600374908ea52Sunny Goyal
879e78e3d734b577c1ab6dc0738a83600374908ea52Sunny Goyal            if (d.stateAnnouncer != null) {
880e78e3d734b577c1ab6dc0738a83600374908ea52Sunny Goyal                d.stateAnnouncer.announce(getContext().getString(R.string.move_to_position,
881e78e3d734b577c1ab6dc0738a83600374908ea52Sunny Goyal                        mTargetRank + 1));
882e78e3d734b577c1ab6dc0738a83600374908ea52Sunny Goyal            }
883bfbfd26c627a18f8e1e3e6d0e53e78feab360203Adam Cohen        }
8844846193300245c8c0a1f9bde3175f273df044309Sunny Goyal
8854846193300245c8c0a1f9bde3175f273df044309Sunny Goyal        float x = r[0];
886b863415c17aaaf6012647df5ed14803f89f94bcbSunny Goyal        int currentPage = mContent.getNextPage();
8874846193300245c8c0a1f9bde3175f273df044309Sunny Goyal
888b863415c17aaaf6012647df5ed14803f89f94bcbSunny Goyal        float cellOverlap = mContent.getCurrentCellLayout().getCellWidth()
8893b0883fcda88dd192549055a387fb41e1c2c17adSunny Goyal                * ICON_OVERSCROLL_WIDTH_FACTOR;
8903b0883fcda88dd192549055a387fb41e1c2c17adSunny Goyal        boolean isOutsideLeftEdge = x < cellOverlap;
8913b0883fcda88dd192549055a387fb41e1c2c17adSunny Goyal        boolean isOutsideRightEdge = x > (getWidth() - cellOverlap);
8924846193300245c8c0a1f9bde3175f273df044309Sunny Goyal
8937066003b2032a49ae5e59dab9b706259bdeb7e6eSunny Goyal        if (currentPage > 0 && (mContent.mIsRtl ? isOutsideRightEdge : isOutsideLeftEdge)) {
894f5440cbd6c0525769d24b890e16313a728831e04Sunny Goyal            showScrollHint(SCROLL_LEFT, d);
895b863415c17aaaf6012647df5ed14803f89f94bcbSunny Goyal        } else if (currentPage < (mContent.getPageCount() - 1)
8967066003b2032a49ae5e59dab9b706259bdeb7e6eSunny Goyal                && (mContent.mIsRtl ? isOutsideLeftEdge : isOutsideRightEdge)) {
897f5440cbd6c0525769d24b890e16313a728831e04Sunny Goyal            showScrollHint(SCROLL_RIGHT, d);
8984846193300245c8c0a1f9bde3175f273df044309Sunny Goyal        } else {
8994846193300245c8c0a1f9bde3175f273df044309Sunny Goyal            mOnScrollHintAlarm.cancelAlarm();
900f5440cbd6c0525769d24b890e16313a728831e04Sunny Goyal            if (mScrollHintDir != SCROLL_NONE) {
901b863415c17aaaf6012647df5ed14803f89f94bcbSunny Goyal                mContent.clearScrollHint();
902f5440cbd6c0525769d24b890e16313a728831e04Sunny Goyal                mScrollHintDir = SCROLL_NONE;
9034846193300245c8c0a1f9bde3175f273df044309Sunny Goyal            }
9044846193300245c8c0a1f9bde3175f273df044309Sunny Goyal        }
905df2cc41acbfacd576f99483a4af1cda32ebd3d09Adam Cohen    }
906df2cc41acbfacd576f99483a4af1cda32ebd3d09Adam Cohen
9073b0883fcda88dd192549055a387fb41e1c2c17adSunny Goyal    private void showScrollHint(int direction, DragObject d) {
9083b0883fcda88dd192549055a387fb41e1c2c17adSunny Goyal        // Show scroll hint on the right
9093b0883fcda88dd192549055a387fb41e1c2c17adSunny Goyal        if (mScrollHintDir != direction) {
910b863415c17aaaf6012647df5ed14803f89f94bcbSunny Goyal            mContent.showScrollHint(direction);
9113b0883fcda88dd192549055a387fb41e1c2c17adSunny Goyal            mScrollHintDir = direction;
9123b0883fcda88dd192549055a387fb41e1c2c17adSunny Goyal        }
9133b0883fcda88dd192549055a387fb41e1c2c17adSunny Goyal
9143b0883fcda88dd192549055a387fb41e1c2c17adSunny Goyal        // Set alarm for when the hint is complete
9153b0883fcda88dd192549055a387fb41e1c2c17adSunny Goyal        if (!mOnScrollHintAlarm.alarmPending() || mCurrentScrollDir != direction) {
9163b0883fcda88dd192549055a387fb41e1c2c17adSunny Goyal            mCurrentScrollDir = direction;
9173b0883fcda88dd192549055a387fb41e1c2c17adSunny Goyal            mOnScrollHintAlarm.cancelAlarm();
9183b0883fcda88dd192549055a387fb41e1c2c17adSunny Goyal            mOnScrollHintAlarm.setOnAlarmListener(new OnScrollHintListener(d));
9193b0883fcda88dd192549055a387fb41e1c2c17adSunny Goyal            mOnScrollHintAlarm.setAlarm(SCROLL_HINT_DURATION);
9203b0883fcda88dd192549055a387fb41e1c2c17adSunny Goyal
9213b0883fcda88dd192549055a387fb41e1c2c17adSunny Goyal            mReorderAlarm.cancelAlarm();
9223b0883fcda88dd192549055a387fb41e1c2c17adSunny Goyal            mTargetRank = mEmptyCellRank;
9233b0883fcda88dd192549055a387fb41e1c2c17adSunny Goyal        }
9243b0883fcda88dd192549055a387fb41e1c2c17adSunny Goyal    }
9253b0883fcda88dd192549055a387fb41e1c2c17adSunny Goyal
926bfbfd26c627a18f8e1e3e6d0e53e78feab360203Adam Cohen    OnAlarmListener mOnExitAlarmListener = new OnAlarmListener() {
927bfbfd26c627a18f8e1e3e6d0e53e78feab360203Adam Cohen        public void onAlarm(Alarm alarm) {
9283e8f811f31a2275e778244427271f77f4116c08bAdam Cohen            completeDragExit();
929bfbfd26c627a18f8e1e3e6d0e53e78feab360203Adam Cohen        }
930bfbfd26c627a18f8e1e3e6d0e53e78feab360203Adam Cohen    };
9312801cafe62653131fdc9da402e5c44e5ffd0bf47Adam Cohen
93295bb800de9b0142bea0110b074eddbcf0e475736Adam Cohen    public void completeDragExit() {
933740ac7f00e0b847b8e392800f7948d93493e11d6Sunny Goyal        if (mIsOpen) {
934740ac7f00e0b847b8e392800f7948d93493e11d6Sunny Goyal            close(true);
935f406615354a92367481a60966b6b873cae37fe4cSunny Goyal            mRearrangeOnClose = true;
93631abc291deb7a8879fc2ef675814efdb6fa9d56fSunny Goyal        } else if (mState == STATE_ANIMATING) {
93731abc291deb7a8879fc2ef675814efdb6fa9d56fSunny Goyal            mRearrangeOnClose = true;
938f406615354a92367481a60966b6b873cae37fe4cSunny Goyal        } else {
939f406615354a92367481a60966b6b873cae37fe4cSunny Goyal            rearrangeChildren();
94031abc291deb7a8879fc2ef675814efdb6fa9d56fSunny Goyal            clearDragInfo();
941f406615354a92367481a60966b6b873cae37fe4cSunny Goyal        }
94231abc291deb7a8879fc2ef675814efdb6fa9d56fSunny Goyal    }
94331abc291deb7a8879fc2ef675814efdb6fa9d56fSunny Goyal
94431abc291deb7a8879fc2ef675814efdb6fa9d56fSunny Goyal    private void clearDragInfo() {
9453e8f811f31a2275e778244427271f77f4116c08bAdam Cohen        mCurrentDragView = null;
94655bd9725d5c0373b89f7b9bbd9547550ea3bbc63Jorim Jaggi        mIsExternalDrag = false;
9473e8f811f31a2275e778244427271f77f4116c08bAdam Cohen    }
9483e8f811f31a2275e778244427271f77f4116c08bAdam Cohen
949bfbfd26c627a18f8e1e3e6d0e53e78feab360203Adam Cohen    public void onDragExit(DragObject d) {
95053b8d071ce63b2b55fa8bdc71488c44308bfcf2fMindy DelliCarpini        // We only close the folder if this is a true drag exit, ie. not because
95153b8d071ce63b2b55fa8bdc71488c44308bfcf2fMindy DelliCarpini        // a drop has occurred above the folder.
952bfbfd26c627a18f8e1e3e6d0e53e78feab360203Adam Cohen        if (!d.dragComplete) {
953bfbfd26c627a18f8e1e3e6d0e53e78feab360203Adam Cohen            mOnExitAlarm.setOnAlarmListener(mOnExitAlarmListener);
954bfbfd26c627a18f8e1e3e6d0e53e78feab360203Adam Cohen            mOnExitAlarm.setAlarm(ON_EXIT_CLOSE_DELAY);
955bfbfd26c627a18f8e1e3e6d0e53e78feab360203Adam Cohen        }
956bfbfd26c627a18f8e1e3e6d0e53e78feab360203Adam Cohen        mReorderAlarm.cancelAlarm();
9574846193300245c8c0a1f9bde3175f273df044309Sunny Goyal
958b863415c17aaaf6012647df5ed14803f89f94bcbSunny Goyal        mOnScrollHintAlarm.cancelAlarm();
959b863415c17aaaf6012647df5ed14803f89f94bcbSunny Goyal        mScrollPauseAlarm.cancelAlarm();
960f5440cbd6c0525769d24b890e16313a728831e04Sunny Goyal        if (mScrollHintDir != SCROLL_NONE) {
961b863415c17aaaf6012647df5ed14803f89f94bcbSunny Goyal            mContent.clearScrollHint();
962f5440cbd6c0525769d24b890e16313a728831e04Sunny Goyal            mScrollHintDir = SCROLL_NONE;
9634846193300245c8c0a1f9bde3175f273df044309Sunny Goyal        }
9642801cafe62653131fdc9da402e5c44e5ffd0bf47Adam Cohen    }
9652801cafe62653131fdc9da402e5c44e5ffd0bf47Adam Cohen
966e9b651eef1b9f3647eba94f833bff3fc52f5956bSunny Goyal    /**
967e9b651eef1b9f3647eba94f833bff3fc52f5956bSunny Goyal     * When performing an accessibility drop, onDrop is sent immediately after onDragEnter. So we
968e9b651eef1b9f3647eba94f833bff3fc52f5956bSunny Goyal     * need to complete all transient states based on timers.
969e9b651eef1b9f3647eba94f833bff3fc52f5956bSunny Goyal     */
970e9b651eef1b9f3647eba94f833bff3fc52f5956bSunny Goyal    @Override
971e9b651eef1b9f3647eba94f833bff3fc52f5956bSunny Goyal    public void prepareAccessibilityDrop() {
972e9b651eef1b9f3647eba94f833bff3fc52f5956bSunny Goyal        if (mReorderAlarm.alarmPending()) {
973e9b651eef1b9f3647eba94f833bff3fc52f5956bSunny Goyal            mReorderAlarm.cancelAlarm();
974e9b651eef1b9f3647eba94f833bff3fc52f5956bSunny Goyal            mReorderAlarmListener.onAlarm(mReorderAlarm);
975e9b651eef1b9f3647eba94f833bff3fc52f5956bSunny Goyal        }
976e9b651eef1b9f3647eba94f833bff3fc52f5956bSunny Goyal    }
977e9b651eef1b9f3647eba94f833bff3fc52f5956bSunny Goyal
9781e2f465f46ded990ea30516cdb7f0fcf3280411fMichael Jurka    public void onDropCompleted(final View target, final DragObject d,
9791e2f465f46ded990ea30516cdb7f0fcf3280411fMichael Jurka            final boolean isFlingToDelete, final boolean success) {
9801e2f465f46ded990ea30516cdb7f0fcf3280411fMichael Jurka        if (mDeferDropAfterUninstall) {
981f3007589e346df224d357b389069adfeb266545eMichael Jurka            Log.d(TAG, "Deferred handling drop because waiting for uninstall.");
9821e2f465f46ded990ea30516cdb7f0fcf3280411fMichael Jurka            mDeferredAction = new Runnable() {
9831e2f465f46ded990ea30516cdb7f0fcf3280411fMichael Jurka                    public void run() {
9841e2f465f46ded990ea30516cdb7f0fcf3280411fMichael Jurka                        onDropCompleted(target, d, isFlingToDelete, success);
9851e2f465f46ded990ea30516cdb7f0fcf3280411fMichael Jurka                        mDeferredAction = null;
9861e2f465f46ded990ea30516cdb7f0fcf3280411fMichael Jurka                    }
9871e2f465f46ded990ea30516cdb7f0fcf3280411fMichael Jurka                };
9881e2f465f46ded990ea30516cdb7f0fcf3280411fMichael Jurka            return;
9891e2f465f46ded990ea30516cdb7f0fcf3280411fMichael Jurka        }
9901e2f465f46ded990ea30516cdb7f0fcf3280411fMichael Jurka
9911e2f465f46ded990ea30516cdb7f0fcf3280411fMichael Jurka        boolean beingCalledAfterUninstall = mDeferredAction != null;
9921e2f465f46ded990ea30516cdb7f0fcf3280411fMichael Jurka        boolean successfulDrop =
9931e2f465f46ded990ea30516cdb7f0fcf3280411fMichael Jurka                success && (!beingCalledAfterUninstall || mUninstallSuccessful);
9945f8afe6280eae34620067696173e71943e1a30a3Winson Chung
9951e2f465f46ded990ea30516cdb7f0fcf3280411fMichael Jurka        if (successfulDrop) {
99655bd9725d5c0373b89f7b9bbd9547550ea3bbc63Jorim Jaggi            if (mDeleteFolderOnDropCompleted && !mItemAddedBackToSelfViaIcon && target != this) {
99767bd9ccf856840856944815a382e4bbb0e71e949Adam Cohen                replaceFolderWithFinalItem();
99867bd9ccf856840856944815a382e4bbb0e71e949Adam Cohen            }
99910236d6ac7f5f787ca21736494ccb5eaf2bd7c27Tony Wickham        } else {
100067bd9ccf856840856944815a382e4bbb0e71e949Adam Cohen            // The drag failed, we need to return the item to the folder
1001b8c663c492d32963b6ee33750fc985f037c58f10Sunny Goyal            ShortcutInfo info = (ShortcutInfo) d.dragInfo;
1002b8c663c492d32963b6ee33750fc985f037c58f10Sunny Goyal            View icon = (mCurrentDragView != null && mCurrentDragView.getTag() == info)
1003b8c663c492d32963b6ee33750fc985f037c58f10Sunny Goyal                    ? mCurrentDragView : mContent.createNewView(info);
10048701cd58cf3b32c9d2b9269e3956b32f4726a81aJonathan Miranda            ArrayList<View> views = getItemsInReadingOrder();
1005b8c663c492d32963b6ee33750fc985f037c58f10Sunny Goyal            views.add(info.rank, icon);
1006b8c663c492d32963b6ee33750fc985f037c58f10Sunny Goyal            mContent.arrangeChildren(views, views.size());
1007b8c663c492d32963b6ee33750fc985f037c58f10Sunny Goyal            mItemsInvalidated = true;
1008b8c663c492d32963b6ee33750fc985f037c58f10Sunny Goyal
1009e393d3af369c452f3b5f5d9c0a23a83aa3aa736bSunny Goyal            try (SuppressInfoChanges s = new SuppressInfoChanges()) {
1010e393d3af369c452f3b5f5d9c0a23a83aa3aa736bSunny Goyal                mFolderIcon.onDrop(d);
1011e393d3af369c452f3b5f5d9c0a23a83aa3aa736bSunny Goyal            }
101267bd9ccf856840856944815a382e4bbb0e71e949Adam Cohen        }
101367bd9ccf856840856944815a382e4bbb0e71e949Adam Cohen
101467bd9ccf856840856944815a382e4bbb0e71e949Adam Cohen        if (target != this) {
101567bd9ccf856840856944815a382e4bbb0e71e949Adam Cohen            if (mOnExitAlarm.alarmPending()) {
101667bd9ccf856840856944815a382e4bbb0e71e949Adam Cohen                mOnExitAlarm.cancelAlarm();
10175455425c1b0355f5dd652cc8e7a2369c33dc9883Michael Jurka                if (!successfulDrop) {
10187a8b82b5926e6a520df28e7f591b9a94ec605581Adam Cohen                    mSuppressFolderDeletion = true;
10197a8b82b5926e6a520df28e7f591b9a94ec605581Adam Cohen                }
10205d85c44fd873c740dc191b28424c2ee367d730a2Sunny Goyal                mScrollPauseAlarm.cancelAlarm();
102167bd9ccf856840856944815a382e4bbb0e71e949Adam Cohen                completeDragExit();
102267bd9ccf856840856944815a382e4bbb0e71e949Adam Cohen            }
10239c58d8269b3bdfdbe500b52ccce878ff2edb196bAdam Cohen        }
10249c58d8269b3bdfdbe500b52ccce878ff2edb196bAdam Cohen
102567bd9ccf856840856944815a382e4bbb0e71e949Adam Cohen        mDeleteFolderOnDropCompleted = false;
102667bd9ccf856840856944815a382e4bbb0e71e949Adam Cohen        mDragInProgress = false;
102705e0f40e50574f50678a9e1461cb245cddc46abbAdam Cohen        mItemAddedBackToSelfViaIcon = false;
1028bfbfd26c627a18f8e1e3e6d0e53e78feab360203Adam Cohen        mCurrentDragView = null;
10294045eb736e76374ec8b53b4cea2eaea47c72db4aAdam Cohen
10304045eb736e76374ec8b53b4cea2eaea47c72db4aAdam Cohen        // Reordering may have occured, and we need to save the new item locations. We do this once
10314045eb736e76374ec8b53b4cea2eaea47c72db4aAdam Cohen        // at the end to prevent unnecessary database operations.
1032f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen        updateItemLocationsInDatabaseBatch();
1033b7e15adc7b67cb1d70ca72014b200dd6cbbf166aSunny Goyal
1034b7e15adc7b67cb1d70ca72014b200dd6cbbf166aSunny Goyal        // Use the item count to check for multi-page as the folder UI may not have
1035b7e15adc7b67cb1d70ca72014b200dd6cbbf166aSunny Goyal        // been refreshed yet.
1036b7e15adc7b67cb1d70ca72014b200dd6cbbf166aSunny Goyal        if (getItemCount() <= mContent.itemsPerPage()) {
1037b7e15adc7b67cb1d70ca72014b200dd6cbbf166aSunny Goyal            // Show the animation, next time something is added to the folder.
103843bf11d9c95f76c2dfeb625b23cb458df81252b3Sunny Goyal            mInfo.setOption(FolderInfo.FLAG_MULTI_PAGE_ANIMATION, false,
103943bf11d9c95f76c2dfeb625b23cb458df81252b3Sunny Goyal                    mLauncher.getModelWriter());
1040b7e15adc7b67cb1d70ca72014b200dd6cbbf166aSunny Goyal        }
1041b7e15adc7b67cb1d70ca72014b200dd6cbbf166aSunny Goyal
10422516428710f13d5abc425c896151ad013267cc3dTony Wickham        if (!isFlingToDelete) {
10432516428710f13d5abc425c896151ad013267cc3dTony Wickham            // Fling to delete already exits spring loaded mode after the animation finishes.
10442516428710f13d5abc425c896151ad013267cc3dTony Wickham            mLauncher.exitSpringLoadedDragModeDelayed(successfulDrop,
10452516428710f13d5abc425c896151ad013267cc3dTony Wickham                    Launcher.EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
10462516428710f13d5abc425c896151ad013267cc3dTony Wickham        }
10474045eb736e76374ec8b53b4cea2eaea47c72db4aAdam Cohen    }
10484045eb736e76374ec8b53b4cea2eaea47c72db4aAdam Cohen
1049fa401a10e7e9341daf6f3c5949bf9331902c26d0Sunny Goyal    @Override
10501e2f465f46ded990ea30516cdb7f0fcf3280411fMichael Jurka    public void deferCompleteDropAfterUninstallActivity() {
10511e2f465f46ded990ea30516cdb7f0fcf3280411fMichael Jurka        mDeferDropAfterUninstall = true;
10521e2f465f46ded990ea30516cdb7f0fcf3280411fMichael Jurka    }
10531e2f465f46ded990ea30516cdb7f0fcf3280411fMichael Jurka
1054fa401a10e7e9341daf6f3c5949bf9331902c26d0Sunny Goyal    @Override
1055d5bd67dfa9ee5fda2384a75231b7a68ceb8e9bd5Sunny Goyal    public void onDragObjectRemoved(boolean success) {
10561e2f465f46ded990ea30516cdb7f0fcf3280411fMichael Jurka        mDeferDropAfterUninstall = false;
10571e2f465f46ded990ea30516cdb7f0fcf3280411fMichael Jurka        mUninstallSuccessful = success;
10581e2f465f46ded990ea30516cdb7f0fcf3280411fMichael Jurka        if (mDeferredAction != null) {
10591e2f465f46ded990ea30516cdb7f0fcf3280411fMichael Jurka            mDeferredAction.run();
10601e2f465f46ded990ea30516cdb7f0fcf3280411fMichael Jurka        }
10611e2f465f46ded990ea30516cdb7f0fcf3280411fMichael Jurka    }
10621e2f465f46ded990ea30516cdb7f0fcf3280411fMichael Jurka
1063a48487a814c07a9f1f45eb3ffe3d873b3dc31b3bWinson Chung    @Override
1064eeb5bbc9409978cc2ae52d48380399fcde3d9f85Winson Chung    public float getIntrinsicIconScaleFactor() {
1065eeb5bbc9409978cc2ae52d48380399fcde3d9f85Winson Chung        return 1f;
1066eeb5bbc9409978cc2ae52d48380399fcde3d9f85Winson Chung    }
1067eeb5bbc9409978cc2ae52d48380399fcde3d9f85Winson Chung
1068eeb5bbc9409978cc2ae52d48380399fcde3d9f85Winson Chung    @Override
10691eeb3fc9d874d98dfb43883d17efb4202d83d88aMathew Inwood    public boolean supportsAppInfoDropTarget() {
1070f5440cbd6c0525769d24b890e16313a728831e04Sunny Goyal        return true;
10711eeb3fc9d874d98dfb43883d17efb4202d83d88aMathew Inwood    }
10721eeb3fc9d874d98dfb43883d17efb4202d83d88aMathew Inwood
10731eeb3fc9d874d98dfb43883d17efb4202d83d88aMathew Inwood    @Override
10741eeb3fc9d874d98dfb43883d17efb4202d83d88aMathew Inwood    public boolean supportsDeleteDropTarget() {
10751eeb3fc9d874d98dfb43883d17efb4202d83d88aMathew Inwood        return true;
10761eeb3fc9d874d98dfb43883d17efb4202d83d88aMathew Inwood    }
10771eeb3fc9d874d98dfb43883d17efb4202d83d88aMathew Inwood
1078f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen    private void updateItemLocationsInDatabaseBatch() {
10798701cd58cf3b32c9d2b9269e3956b32f4726a81aJonathan Miranda        ArrayList<View> list = getItemsInReadingOrder();
1080f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen        ArrayList<ItemInfo> items = new ArrayList<ItemInfo>();
1081f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen        for (int i = 0; i < list.size(); i++) {
1082f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen            View v = list.get(i);
1083f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen            ItemInfo info = (ItemInfo) v.getTag();
108408f7261d11a53ae4b330ad4fa897b8519de3d750Sunny Goyal            info.rank = i;
1085f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen            items.add(info);
1086f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen        }
1087f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen
108843bf11d9c95f76c2dfeb625b23cb458df81252b3Sunny Goyal        mLauncher.getModelWriter().moveItemsInDatabase(items, mInfo.id, 0);
1089f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen    }
1090f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen
109167bd9ccf856840856944815a382e4bbb0e71e949Adam Cohen    public void notifyDrop() {
109267bd9ccf856840856944815a382e4bbb0e71e949Adam Cohen        if (mDragInProgress) {
109305e0f40e50574f50678a9e1461cb245cddc46abbAdam Cohen            mItemAddedBackToSelfViaIcon = true;
109476078c4ef35b6e95fb6d8129a32cf52a36625e28Adam Cohen        }
1095df2cc41acbfacd576f99483a4af1cda32ebd3d09Adam Cohen    }
1096df2cc41acbfacd576f99483a4af1cda32ebd3d09Adam Cohen
1097df2cc41acbfacd576f99483a4af1cda32ebd3d09Adam Cohen    public boolean isDropEnabled() {
1098f0cbf0f8b850db450b12ff35c523d5006ee72e5bJon Miranda        if (FeatureFlags.LAUNCHER3_NEW_FOLDER_ANIMATION) {
1099f0cbf0f8b850db450b12ff35c523d5006ee72e5bJon Miranda            return mState != STATE_ANIMATING;
1100f0cbf0f8b850db450b12ff35c523d5006ee72e5bJon Miranda        }
1101df2cc41acbfacd576f99483a4af1cda32ebd3d09Adam Cohen        return true;
1102df2cc41acbfacd576f99483a4af1cda32ebd3d09Adam Cohen    }
1103df2cc41acbfacd576f99483a4af1cda32ebd3d09Adam Cohen
11042801cafe62653131fdc9da402e5c44e5ffd0bf47Adam Cohen    public boolean isFull() {
1105c3a609f950a713d995f1968574d8ed7b4449f415Sunny Goyal        return mContent.isFull();
11062801cafe62653131fdc9da402e5c44e5ffd0bf47Adam Cohen    }
11072801cafe62653131fdc9da402e5c44e5ffd0bf47Adam Cohen
11082801cafe62653131fdc9da402e5c44e5ffd0bf47Adam Cohen    private void centerAboutIcon() {
1109fadbe8ffbd90eb36c782a76a8b1a76a26dba288eWinson        DeviceProfile grid = mLauncher.getDeviceProfile();
11102801cafe62653131fdc9da402e5c44e5ffd0bf47Adam Cohen
1111fadbe8ffbd90eb36c782a76a8b1a76a26dba288eWinson        DragLayer.LayoutParams lp = (DragLayer.LayoutParams) getLayoutParams();
1112892c74d460ad98c6306420e1127c9aa3e505ba25Winson Chung        DragLayer parent = (DragLayer) mLauncher.findViewById(R.id.drag_layer);
1113440609453f14fa8ce4bec1d70032f0b7f9614d4eJon Miranda        int width = getFolderWidth();
1114f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen        int height = getFolderHeight();
11158e776a62c5cc60737f5cdbbeacfec5f958112fa7Adam Cohen
1116c3a609f950a713d995f1968574d8ed7b4449f415Sunny Goyal        float scale = parent.getDescendantRectRelativeToSelf(mFolderIcon, sTempRect);
1117fadbe8ffbd90eb36c782a76a8b1a76a26dba288eWinson        int centerX = sTempRect.centerX();
1118fadbe8ffbd90eb36c782a76a8b1a76a26dba288eWinson        int centerY = sTempRect.centerY();
11192801cafe62653131fdc9da402e5c44e5ffd0bf47Adam Cohen        int centeredLeft = centerX - width / 2;
11202801cafe62653131fdc9da402e5c44e5ffd0bf47Adam Cohen        int centeredTop = centerY - height / 2;
112141d8417678934449c2692a97c0ae7b0d8ac27326Sunny Goyal
112241d8417678934449c2692a97c0ae7b0d8ac27326Sunny Goyal        // We need to bound the folder to the currently visible workspace area
112341d8417678934449c2692a97c0ae7b0d8ac27326Sunny Goyal        mLauncher.getWorkspace().getPageAreaRelativeToDragLayer(sTempRect);
112441d8417678934449c2692a97c0ae7b0d8ac27326Sunny Goyal        int left = Math.min(Math.max(sTempRect.left, centeredLeft),
1125fadbe8ffbd90eb36c782a76a8b1a76a26dba288eWinson                sTempRect.right- width);
112641d8417678934449c2692a97c0ae7b0d8ac27326Sunny Goyal        int top = Math.min(Math.max(sTempRect.top, centeredTop),
1127fadbe8ffbd90eb36c782a76a8b1a76a26dba288eWinson                sTempRect.bottom - height);
112882fa92011b0f634d696309e13f63f1d544545e73Adam Cohen
11296c2975e7e3fc5dc7cb80f1a2b3e5ffd10e49c2beSunny Goyal        int distFromEdgeOfScreen = mLauncher.getWorkspace().getPaddingLeft() + getPaddingLeft();
113082fa92011b0f634d696309e13f63f1d544545e73Adam Cohen
113182fa92011b0f634d696309e13f63f1d544545e73Adam Cohen        if (grid.isPhone && (grid.availableWidthPx - width) < 4 * distFromEdgeOfScreen) {
113282fa92011b0f634d696309e13f63f1d544545e73Adam Cohen            // Center the folder if it is very close to being centered anyway, by virtue of
113382fa92011b0f634d696309e13f63f1d544545e73Adam Cohen            // filling the majority of the viewport. ie. remove it from the uncanny valley
113482fa92011b0f634d696309e13f63f1d544545e73Adam Cohen            // of centeredness.
1135af40f205d7f0c5f73a92ff5d9b8e9602dbde58c0Winson Chung            left = (grid.availableWidthPx - width) / 2;
113641d8417678934449c2692a97c0ae7b0d8ac27326Sunny Goyal        } else if (width >= sTempRect.width()) {
1137af40f205d7f0c5f73a92ff5d9b8e9602dbde58c0Winson Chung            // If the folder doesn't fit within the bounds, center it about the desired bounds
113841d8417678934449c2692a97c0ae7b0d8ac27326Sunny Goyal            left = sTempRect.left + (sTempRect.width() - width) / 2;
113935e7e64d4b98e42c760e5e91489b7014a6ba9073Adam Cohen        }
114041d8417678934449c2692a97c0ae7b0d8ac27326Sunny Goyal        if (height >= sTempRect.height()) {
1141fadbe8ffbd90eb36c782a76a8b1a76a26dba288eWinson            // Folder height is greater than page height, center on page
114241d8417678934449c2692a97c0ae7b0d8ac27326Sunny Goyal            top = sTempRect.top + (sTempRect.height() - height) / 2;
1143fadbe8ffbd90eb36c782a76a8b1a76a26dba288eWinson        } else {
1144fadbe8ffbd90eb36c782a76a8b1a76a26dba288eWinson            // Folder height is less than page height, so bound it to the absolute open folder
1145fadbe8ffbd90eb36c782a76a8b1a76a26dba288eWinson            // bounds if necessary
1146fadbe8ffbd90eb36c782a76a8b1a76a26dba288eWinson            Rect folderBounds = grid.getAbsoluteOpenFolderBounds();
1147fadbe8ffbd90eb36c782a76a8b1a76a26dba288eWinson            left = Math.max(folderBounds.left, Math.min(left, folderBounds.right - width));
1148fadbe8ffbd90eb36c782a76a8b1a76a26dba288eWinson            top = Math.max(folderBounds.top, Math.min(top, folderBounds.bottom - height));
11490e4857c302da6adbd45231d7a8bae0289fa33250Adam Cohen        }
11502801cafe62653131fdc9da402e5c44e5ffd0bf47Adam Cohen
11512801cafe62653131fdc9da402e5c44e5ffd0bf47Adam Cohen        int folderPivotX = width / 2 + (centeredLeft - left);
11522801cafe62653131fdc9da402e5c44e5ffd0bf47Adam Cohen        int folderPivotY = height / 2 + (centeredTop - top);
11532801cafe62653131fdc9da402e5c44e5ffd0bf47Adam Cohen        setPivotX(folderPivotX);
11542801cafe62653131fdc9da402e5c44e5ffd0bf47Adam Cohen        setPivotY(folderPivotY);
115548dfd26195603bf863be40f1d40922b8077fe5efJon Miranda
1156268c4751b547e1470a94030af73a4e7bb8ea221eAdam Cohen        mFolderIconPivotX = (int) (mFolderIcon.getMeasuredWidth() *
11572801cafe62653131fdc9da402e5c44e5ffd0bf47Adam Cohen                (1.0f * folderPivotX / width));
1158268c4751b547e1470a94030af73a4e7bb8ea221eAdam Cohen        mFolderIconPivotY = (int) (mFolderIcon.getMeasuredHeight() *
11592801cafe62653131fdc9da402e5c44e5ffd0bf47Adam Cohen                (1.0f * folderPivotY / height));
11603bf84d3250bf45f2a5b041245e9bd327e03e22c0Adam Cohen
1161662b598c86a1772d95303ef9c921d4bbe342387bAdam Cohen        lp.width = width;
1162662b598c86a1772d95303ef9c921d4bbe342387bAdam Cohen        lp.height = height;
1163662b598c86a1772d95303ef9c921d4bbe342387bAdam Cohen        lp.x = left;
1164662b598c86a1772d95303ef9c921d4bbe342387bAdam Cohen        lp.y = top;
11652801cafe62653131fdc9da402e5c44e5ffd0bf47Adam Cohen    }
11662801cafe62653131fdc9da402e5c44e5ffd0bf47Adam Cohen
1167f9c184a619e4e4b82cf9e0bf318ca6d8deaaaee7Adam Cohen    public float getPivotXForIconAnimation() {
1168268c4751b547e1470a94030af73a4e7bb8ea221eAdam Cohen        return mFolderIconPivotX;
1169268c4751b547e1470a94030af73a4e7bb8ea221eAdam Cohen    }
1170f9c184a619e4e4b82cf9e0bf318ca6d8deaaaee7Adam Cohen    public float getPivotYForIconAnimation() {
1171268c4751b547e1470a94030af73a4e7bb8ea221eAdam Cohen        return mFolderIconPivotY;
1172268c4751b547e1470a94030af73a4e7bb8ea221eAdam Cohen    }
1173268c4751b547e1470a94030af73a4e7bb8ea221eAdam Cohen
1174892c74d460ad98c6306420e1127c9aa3e505ba25Winson Chung    private int getContentAreaHeight() {
11752e6da1539bc7286336b3c24d96ab76434939ce4dAdam Cohen        DeviceProfile grid = mLauncher.getDeviceProfile();
11766c2975e7e3fc5dc7cb80f1a2b3e5ffd10e49c2beSunny Goyal        int maxContentAreaHeight = grid.availableHeightPx
11776c2975e7e3fc5dc7cb80f1a2b3e5ffd10e49c2beSunny Goyal                - grid.getTotalWorkspacePadding().y - mFooterHeight;
11781960ea4a7102bd0fa03cad1ea1e3a91f8e1bd9cdAdam Cohen        int height = Math.min(maxContentAreaHeight,
1179892c74d460ad98c6306420e1127c9aa3e505ba25Winson Chung                mContent.getDesiredHeight());
11801960ea4a7102bd0fa03cad1ea1e3a91f8e1bd9cdAdam Cohen        return Math.max(height, MIN_CONTENT_DIMEN);
11811960ea4a7102bd0fa03cad1ea1e3a91f8e1bd9cdAdam Cohen    }
11821960ea4a7102bd0fa03cad1ea1e3a91f8e1bd9cdAdam Cohen
11831960ea4a7102bd0fa03cad1ea1e3a91f8e1bd9cdAdam Cohen    private int getContentAreaWidth() {
11841960ea4a7102bd0fa03cad1ea1e3a91f8e1bd9cdAdam Cohen        return Math.max(mContent.getDesiredWidth(), MIN_CONTENT_DIMEN);
1185892c74d460ad98c6306420e1127c9aa3e505ba25Winson Chung    }
1186892c74d460ad98c6306420e1127c9aa3e505ba25Winson Chung
1187440609453f14fa8ce4bec1d70032f0b7f9614d4eJon Miranda    private int getFolderWidth() {
1188440609453f14fa8ce4bec1d70032f0b7f9614d4eJon Miranda        return getPaddingLeft() + getPaddingRight() + mContent.getDesiredWidth();
1189440609453f14fa8ce4bec1d70032f0b7f9614d4eJon Miranda    }
1190440609453f14fa8ce4bec1d70032f0b7f9614d4eJon Miranda
1191f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen    private int getFolderHeight() {
1192c3a609f950a713d995f1968574d8ed7b4449f415Sunny Goyal        return getFolderHeight(getContentAreaHeight());
1193c3a609f950a713d995f1968574d8ed7b4449f415Sunny Goyal    }
1194c3a609f950a713d995f1968574d8ed7b4449f415Sunny Goyal
1195c3a609f950a713d995f1968574d8ed7b4449f415Sunny Goyal    private int getFolderHeight(int contentAreaHeight) {
1196290800b5b7d575fd709f244f54a5fa5b63b58876Sunny Goyal        return getPaddingTop() + getPaddingBottom() + contentAreaHeight + mFooterHeight;
1197f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen    }
1198f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen
1199f0f4eda31841f41d892bf18847c1acdc45d2cd64Adam Cohen    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
1200c3a609f950a713d995f1968574d8ed7b4449f415Sunny Goyal        int contentWidth = getContentAreaWidth();
1201c3a609f950a713d995f1968574d8ed7b4449f415Sunny Goyal        int contentHeight = getContentAreaHeight();
12022801cafe62653131fdc9da402e5c44e5ffd0bf47Adam Cohen
1203c3a609f950a713d995f1968574d8ed7b4449f415Sunny Goyal        int contentAreaWidthSpec = MeasureSpec.makeMeasureSpec(contentWidth, MeasureSpec.EXACTLY);
1204c3a609f950a713d995f1968574d8ed7b4449f415Sunny Goyal        int contentAreaHeightSpec = MeasureSpec.makeMeasureSpec(contentHeight, MeasureSpec.EXACTLY);
1205c3a609f950a713d995f1968574d8ed7b4449f415Sunny Goyal
1206c3a609f950a713d995f1968574d8ed7b4449f415Sunny Goyal        mContent.setFixedSize(contentWidth, contentHeight);
12073333b0ced8e6743c41909f6f6b916f1f9ec5a004Sunny Goyal        mContent.measure(contentAreaWidthSpec, contentAreaHeightSpec);
1208a07c2f5c80908e8a612f4681314a7068b92baa9fSunny Goyal
1209a07c2f5c80908e8a612f4681314a7068b92baa9fSunny Goyal        if (mContent.getChildCount() > 0) {
1210a07c2f5c80908e8a612f4681314a7068b92baa9fSunny Goyal            int cellIconGap = (mContent.getPageAt(0).getCellWidth()
1211a07c2f5c80908e8a612f4681314a7068b92baa9fSunny Goyal                    - mLauncher.getDeviceProfile().iconSizePx) / 2;
1212a07c2f5c80908e8a612f4681314a7068b92baa9fSunny Goyal            mFooter.setPadding(mContent.getPaddingLeft() + cellIconGap,
1213a07c2f5c80908e8a612f4681314a7068b92baa9fSunny Goyal                    mFooter.getPaddingTop(),
1214a07c2f5c80908e8a612f4681314a7068b92baa9fSunny Goyal                    mContent.getPaddingRight() + cellIconGap,
1215a07c2f5c80908e8a612f4681314a7068b92baa9fSunny Goyal                    mFooter.getPaddingBottom());
1216a07c2f5c80908e8a612f4681314a7068b92baa9fSunny Goyal        }
1217290800b5b7d575fd709f244f54a5fa5b63b58876Sunny Goyal        mFooter.measure(contentAreaWidthSpec,
1218290800b5b7d575fd709f244f54a5fa5b63b58876Sunny Goyal                MeasureSpec.makeMeasureSpec(mFooterHeight, MeasureSpec.EXACTLY));
1219c3a609f950a713d995f1968574d8ed7b4449f415Sunny Goyal
1220c3a609f950a713d995f1968574d8ed7b4449f415Sunny Goyal        int folderWidth = getPaddingLeft() + getPaddingRight() + contentWidth;
1221c3a609f950a713d995f1968574d8ed7b4449f415Sunny Goyal        int folderHeight = getFolderHeight(contentHeight);
1222c3a609f950a713d995f1968574d8ed7b4449f415Sunny Goyal        setMeasuredDimension(folderWidth, folderHeight);
1223c3a609f950a713d995f1968574d8ed7b4449f415Sunny Goyal    }
1224c3a609f950a713d995f1968574d8ed7b4449f415Sunny Goyal
1225c3a609f950a713d995f1968574d8ed7b4449f415Sunny Goyal    /**
1226c3a609f950a713d995f1968574d8ed7b4449f415Sunny Goyal     * Rearranges the children based on their rank.
1227c3a609f950a713d995f1968574d8ed7b4449f415Sunny Goyal     */
1228c3a609f950a713d995f1968574d8ed7b4449f415Sunny Goyal    public void rearrangeChildren() {
1229c3a609f950a713d995f1968574d8ed7b4449f415Sunny Goyal        rearrangeChildren(-1);
1230c3a609f950a713d995f1968574d8ed7b4449f415Sunny Goyal    }
1231c3a609f950a713d995f1968574d8ed7b4449f415Sunny Goyal
1232c3a609f950a713d995f1968574d8ed7b4449f415Sunny Goyal    /**
1233c3a609f950a713d995f1968574d8ed7b4449f415Sunny Goyal     * Rearranges the children based on their rank.
1234c3a609f950a713d995f1968574d8ed7b4449f415Sunny Goyal     * @param itemCount if greater than the total children count, empty spaces are left at the end,
1235c3a609f950a713d995f1968574d8ed7b4449f415Sunny Goyal     * otherwise it is ignored.
1236c3a609f950a713d995f1968574d8ed7b4449f415Sunny Goyal     */
1237c3a609f950a713d995f1968574d8ed7b4449f415Sunny Goyal    public void rearrangeChildren(int itemCount) {
12388701cd58cf3b32c9d2b9269e3956b32f4726a81aJonathan Miranda        ArrayList<View> views = getItemsInReadingOrder();
1239c3a609f950a713d995f1968574d8ed7b4449f415Sunny Goyal        mContent.arrangeChildren(views, Math.max(itemCount, views.size()));
12407c6932140dcbd2db88d7a809ba72ee04abb5bf26Adam Cohen        mItemsInvalidated = true;
12412801cafe62653131fdc9da402e5c44e5ffd0bf47Adam Cohen    }
12422801cafe62653131fdc9da402e5c44e5ffd0bf47Adam Cohen
1243a9cf38f533d1e86269868f1e6a806ccffd4a78fcAdam Cohen    public int getItemCount() {
1244c3a609f950a713d995f1968574d8ed7b4449f415Sunny Goyal        return mContent.getItemCount();
1245a9cf38f533d1e86269868f1e6a806ccffd4a78fcAdam Cohen    }
1246a9cf38f533d1e86269868f1e6a806ccffd4a78fcAdam Cohen
1247091440a9cb9d4f42406631004aa484cbb79214caAdam Cohen    @Thunk void replaceFolderWithFinalItem() {
1248fb91f303f5e672f9b4c4f9f8a03486b224d60cd6Adam Cohen        // Add the last remaining child to the workspace in place of the folder
1249fb91f303f5e672f9b4c4f9f8a03486b224d60cd6Adam Cohen        Runnable onCompleteRunnable = new Runnable() {
1250fb91f303f5e672f9b4c4f9f8a03486b224d60cd6Adam Cohen            @Override
1251fb91f303f5e672f9b4c4f9f8a03486b224d60cd6Adam Cohen            public void run() {
1252ca656e3c69266234486c6669fc63244330cb0549Tony Wickham                int itemCount = mInfo.contents.size();
12536febe1f3613a3b994fa4a2bfad3a14f80f3b0126Winson                if (itemCount <= 1) {
12546febe1f3613a3b994fa4a2bfad3a14f80f3b0126Winson                    View newIcon = null;
12556febe1f3613a3b994fa4a2bfad3a14f80f3b0126Winson
12566febe1f3613a3b994fa4a2bfad3a14f80f3b0126Winson                    if (itemCount == 1) {
12576febe1f3613a3b994fa4a2bfad3a14f80f3b0126Winson                        // Move the item from the folder to the workspace, in the position of the
12586febe1f3613a3b994fa4a2bfad3a14f80f3b0126Winson                        // folder
12596febe1f3613a3b994fa4a2bfad3a14f80f3b0126Winson                        CellLayout cellLayout = mLauncher.getCellLayout(mInfo.container,
12606febe1f3613a3b994fa4a2bfad3a14f80f3b0126Winson                                mInfo.screenId);
12616febe1f3613a3b994fa4a2bfad3a14f80f3b0126Winson                        ShortcutInfo finalItem = mInfo.contents.remove(0);
12626febe1f3613a3b994fa4a2bfad3a14f80f3b0126Winson                        newIcon = mLauncher.createShortcut(cellLayout, finalItem);
126343bf11d9c95f76c2dfeb625b23cb458df81252b3Sunny Goyal                        mLauncher.getModelWriter().addOrMoveItemInDatabase(finalItem,
126443bf11d9c95f76c2dfeb625b23cb458df81252b3Sunny Goyal                                mInfo.container, mInfo.screenId, mInfo.cellX, mInfo.cellY);
12656febe1f3613a3b994fa4a2bfad3a14f80f3b0126Winson                    }
1266fb91f303f5e672f9b4c4f9f8a03486b224d60cd6Adam Cohen
12676febe1f3613a3b994fa4a2bfad3a14f80f3b0126Winson                    // Remove the folder
12682949fb5b16a07259e221c5c04470e90c8afb4ea8Winson                    mLauncher.removeItem(mFolderIcon, mInfo, true /* deleteFromDb */);
1269487f7dd3059621527eb439d7d51d34e00293f9b1Adam Cohen                    if (mFolderIcon instanceof DropTarget) {
1270487f7dd3059621527eb439d7d51d34e00293f9b1Adam Cohen                        mDragController.removeDropTarget((DropTarget) mFolderIcon);
1271487f7dd3059621527eb439d7d51d34e00293f9b1Adam Cohen                    }
1272fa56b3f2f1271d6045594eed294b22841c79c783Winson
12736febe1f3613a3b994fa4a2bfad3a14f80f3b0126Winson                    if (newIcon != null) {
12746febe1f3613a3b994fa4a2bfad3a14f80f3b0126Winson                        // We add the child after removing the folder to prevent both from existing
12756febe1f3613a3b994fa4a2bfad3a14f80f3b0126Winson                        // at the same time in the CellLayout.  We need to add the new item with
12766febe1f3613a3b994fa4a2bfad3a14f80f3b0126Winson                        // addInScreenFromBind() to ensure that hotseat items are placed correctly.
1277d5462aa31b53eb690a3f57c7c6c0cc4021271de6Sunny Goyal                        mLauncher.getWorkspace().addInScreenFromBind(newIcon, mInfo);
1278fa56b3f2f1271d6045594eed294b22841c79c783Winson
1279b9c29d455e301d1b31b911a37eb3bec7e0e16865Sunny Goyal                        // Focus the newly created child
1280b9c29d455e301d1b31b911a37eb3bec7e0e16865Sunny Goyal                        newIcon.requestFocus();
1281b9c29d455e301d1b31b911a37eb3bec7e0e16865Sunny Goyal                    }
1282c5e63f3dc7f19d0da8380e425894450302700ab9Adam Cohen                }
1283fb91f303f5e672f9b4c4f9f8a03486b224d60cd6Adam Cohen            }
1284fb91f303f5e672f9b4c4f9f8a03486b224d60cd6Adam Cohen        };
1285bc753359f8b08e0813016eebc8392b83a4d2bd6eSunny Goyal        View finalChild = mContent.getLastItem();
1286fb91f303f5e672f9b4c4f9f8a03486b224d60cd6Adam Cohen        if (finalChild != null) {
12876c5d10261eceaf1b57bce82e4e31d272bad2c349Jon Miranda            mFolderIcon.performDestroyAnimation(onCompleteRunnable);
128833231f587e1a6f716af68e6198df774d2847a8e5Winson Chung        } else {
128933231f587e1a6f716af68e6198df774d2847a8e5Winson Chung            onCompleteRunnable.run();
1290afb01ee74243cede19088e694ca82cea5983c603Adam Cohen        }
1291fb91f303f5e672f9b4c4f9f8a03486b224d60cd6Adam Cohen        mDestroyed = true;
1292fb91f303f5e672f9b4c4f9f8a03486b224d60cd6Adam Cohen    }
1293afb01ee74243cede19088e694ca82cea5983c603Adam Cohen
1294f9c184a619e4e4b82cf9e0bf318ca6d8deaaaee7Adam Cohen    public boolean isDestroyed() {
1295fb91f303f5e672f9b4c4f9f8a03486b224d60cd6Adam Cohen        return mDestroyed;
12962801cafe62653131fdc9da402e5c44e5ffd0bf47Adam Cohen    }
12972801cafe62653131fdc9da402e5c44e5ffd0bf47Adam Cohen
1298feec2171f84f465feab96631c9d067d032aa8f88Tony Wickham    // This method keeps track of the first and last item in the folder for the purposes
1299ac56cff1860b71d3f164aedd268703936e08fdc0Adam Cohen    // of keyboard focus
1300290800b5b7d575fd709f244f54a5fa5b63b58876Sunny Goyal    public void updateTextViewFocus() {
1301feec2171f84f465feab96631c9d067d032aa8f88Tony Wickham        final View firstChild = mContent.getFirstItem();
1302feec2171f84f465feab96631c9d067d032aa8f88Tony Wickham        final View lastChild = mContent.getLastItem();
1303feec2171f84f465feab96631c9d067d032aa8f88Tony Wickham        if (firstChild != null && lastChild != null) {
1304ac56cff1860b71d3f164aedd268703936e08fdc0Adam Cohen            mFolderName.setNextFocusDownId(lastChild.getId());
1305ac56cff1860b71d3f164aedd268703936e08fdc0Adam Cohen            mFolderName.setNextFocusRightId(lastChild.getId());
1306ac56cff1860b71d3f164aedd268703936e08fdc0Adam Cohen            mFolderName.setNextFocusLeftId(lastChild.getId());
1307ac56cff1860b71d3f164aedd268703936e08fdc0Adam Cohen            mFolderName.setNextFocusUpId(lastChild.getId());
1308feec2171f84f465feab96631c9d067d032aa8f88Tony Wickham            // Hitting TAB from the folder name wraps around to the first item on the current
1309feec2171f84f465feab96631c9d067d032aa8f88Tony Wickham            // folder page, and hitting SHIFT+TAB from that item wraps back to the folder name.
1310feec2171f84f465feab96631c9d067d032aa8f88Tony Wickham            mFolderName.setNextFocusForwardId(firstChild.getId());
1311feec2171f84f465feab96631c9d067d032aa8f88Tony Wickham            // When clicking off the folder when editing the name, this Folder gains focus. When
1312feec2171f84f465feab96631c9d067d032aa8f88Tony Wickham            // pressing an arrow key from that state, give the focus to the first item.
1313feec2171f84f465feab96631c9d067d032aa8f88Tony Wickham            this.setNextFocusDownId(firstChild.getId());
1314feec2171f84f465feab96631c9d067d032aa8f88Tony Wickham            this.setNextFocusRightId(firstChild.getId());
1315feec2171f84f465feab96631c9d067d032aa8f88Tony Wickham            this.setNextFocusLeftId(firstChild.getId());
1316feec2171f84f465feab96631c9d067d032aa8f88Tony Wickham            this.setNextFocusUpId(firstChild.getId());
1317feec2171f84f465feab96631c9d067d032aa8f88Tony Wickham            // When pressing shift+tab in the above state, give the focus to the last item.
1318feec2171f84f465feab96631c9d067d032aa8f88Tony Wickham            setOnKeyListener(new OnKeyListener() {
1319feec2171f84f465feab96631c9d067d032aa8f88Tony Wickham                @Override
1320feec2171f84f465feab96631c9d067d032aa8f88Tony Wickham                public boolean onKey(View v, int keyCode, KeyEvent event) {
1321feec2171f84f465feab96631c9d067d032aa8f88Tony Wickham                    boolean isShiftPlusTab = keyCode == KeyEvent.KEYCODE_TAB &&
1322feec2171f84f465feab96631c9d067d032aa8f88Tony Wickham                            event.hasModifiers(KeyEvent.META_SHIFT_ON);
1323feec2171f84f465feab96631c9d067d032aa8f88Tony Wickham                    if (isShiftPlusTab && Folder.this.isFocused()) {
1324feec2171f84f465feab96631c9d067d032aa8f88Tony Wickham                        return lastChild.requestFocus();
1325feec2171f84f465feab96631c9d067d032aa8f88Tony Wickham                    }
1326feec2171f84f465feab96631c9d067d032aa8f88Tony Wickham                    return false;
1327feec2171f84f465feab96631c9d067d032aa8f88Tony Wickham                }
1328feec2171f84f465feab96631c9d067d032aa8f88Tony Wickham            });
1329ac56cff1860b71d3f164aedd268703936e08fdc0Adam Cohen        }
1330ac56cff1860b71d3f164aedd268703936e08fdc0Adam Cohen    }
1331ac56cff1860b71d3f164aedd268703936e08fdc0Adam Cohen
1332bfbfd26c627a18f8e1e3e6d0e53e78feab360203Adam Cohen    public void onDrop(DragObject d) {
133355bd9725d5c0373b89f7b9bbd9547550ea3bbc63Jorim Jaggi        Runnable cleanUpRunnable = null;
133455bd9725d5c0373b89f7b9bbd9547550ea3bbc63Jorim Jaggi
1335689ff16ea27fb7c22c247aaf4f42ffe42fede253Adam Cohen        // If we are coming from All Apps space, we defer removing the extra empty screen
1336689ff16ea27fb7c22c247aaf4f42ffe42fede253Adam Cohen        // until the folder closes
133755bd9725d5c0373b89f7b9bbd9547550ea3bbc63Jorim Jaggi        if (d.dragSource != mLauncher.getWorkspace() && !(d.dragSource instanceof Folder)) {
133855bd9725d5c0373b89f7b9bbd9547550ea3bbc63Jorim Jaggi            cleanUpRunnable = new Runnable() {
133955bd9725d5c0373b89f7b9bbd9547550ea3bbc63Jorim Jaggi                @Override
134055bd9725d5c0373b89f7b9bbd9547550ea3bbc63Jorim Jaggi                public void run() {
1341689ff16ea27fb7c22c247aaf4f42ffe42fede253Adam Cohen                    mLauncher.exitSpringLoadedDragModeDelayed(true,
1342689ff16ea27fb7c22c247aaf4f42ffe42fede253Adam Cohen                            Launcher.EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT,
1343689ff16ea27fb7c22c247aaf4f42ffe42fede253Adam Cohen                            null);
134455bd9725d5c0373b89f7b9bbd9547550ea3bbc63Jorim Jaggi                }
134555bd9725d5c0373b89f7b9bbd9547550ea3bbc63Jorim Jaggi            };
1346bfbfd26c627a18f8e1e3e6d0e53e78feab360203Adam Cohen        }
134755bd9725d5c0373b89f7b9bbd9547550ea3bbc63Jorim Jaggi
1348b863415c17aaaf6012647df5ed14803f89f94bcbSunny Goyal        // If the icon was dropped while the page was being scrolled, we need to compute
1349b863415c17aaaf6012647df5ed14803f89f94bcbSunny Goyal        // the target location again such that the icon is placed of the final page.
1350b863415c17aaaf6012647df5ed14803f89f94bcbSunny Goyal        if (!mContent.rankOnCurrentPage(mEmptyCellRank)) {
1351b863415c17aaaf6012647df5ed14803f89f94bcbSunny Goyal            // Reorder again.
1352b863415c17aaaf6012647df5ed14803f89f94bcbSunny Goyal            mTargetRank = getTargetRank(d, null);
13534846193300245c8c0a1f9bde3175f273df044309Sunny Goyal
1354b863415c17aaaf6012647df5ed14803f89f94bcbSunny Goyal            // Rearrange items immediately.
1355b863415c17aaaf6012647df5ed14803f89f94bcbSunny Goyal            mReorderAlarmListener.onAlarm(mReorderAlarm);
13564846193300245c8c0a1f9bde3175f273df044309Sunny Goyal
1357b863415c17aaaf6012647df5ed14803f89f94bcbSunny Goyal            mOnScrollHintAlarm.cancelAlarm();
1358b863415c17aaaf6012647df5ed14803f89f94bcbSunny Goyal            mScrollPauseAlarm.cancelAlarm();
13594846193300245c8c0a1f9bde3175f273df044309Sunny Goyal        }
1360b863415c17aaaf6012647df5ed14803f89f94bcbSunny Goyal        mContent.completePendingPageChanges();
13614846193300245c8c0a1f9bde3175f273df044309Sunny Goyal
1362b24fa300250bd9b5d6498be0326c97190ecd4ae1Jon Miranda        PendingAddShortcutInfo pasi = d.dragInfo instanceof PendingAddShortcutInfo
1363b24fa300250bd9b5d6498be0326c97190ecd4ae1Jon Miranda                ? (PendingAddShortcutInfo) d.dragInfo : null;
1364b24fa300250bd9b5d6498be0326c97190ecd4ae1Jon Miranda        ShortcutInfo pasiSi = pasi != null ? pasi.activityInfo.createShortcutInfo() : null;
1365b24fa300250bd9b5d6498be0326c97190ecd4ae1Jon Miranda        if (pasi != null && pasiSi == null) {
1366b24fa300250bd9b5d6498be0326c97190ecd4ae1Jon Miranda            // There is no ShortcutInfo, so we have to go through a configuration activity.
1367c476d6e192494b14b91e76cbce5c9cec01588059Jon Miranda            pasi.container = mInfo.id;
1368c476d6e192494b14b91e76cbce5c9cec01588059Jon Miranda            pasi.rank = mEmptyCellRank;
1369c476d6e192494b14b91e76cbce5c9cec01588059Jon Miranda
1370c476d6e192494b14b91e76cbce5c9cec01588059Jon Miranda            mLauncher.addPendingItem(pasi, pasi.container, pasi.screenId, null, pasi.spanX,
1371c476d6e192494b14b91e76cbce5c9cec01588059Jon Miranda                    pasi.spanY);
1372c476d6e192494b14b91e76cbce5c9cec01588059Jon Miranda            d.deferDragViewCleanupPostAnimation = false;
1373c476d6e192494b14b91e76cbce5c9cec01588059Jon Miranda            mRearrangeOnClose = true;
1374e393d3af369c452f3b5f5d9c0a23a83aa3aa736bSunny Goyal        } else {
1375c476d6e192494b14b91e76cbce5c9cec01588059Jon Miranda            final ShortcutInfo si;
1376b24fa300250bd9b5d6498be0326c97190ecd4ae1Jon Miranda            if (pasiSi != null) {
1377b24fa300250bd9b5d6498be0326c97190ecd4ae1Jon Miranda                si = pasiSi;
1378b24fa300250bd9b5d6498be0326c97190ecd4ae1Jon Miranda            } else if (d.dragInfo instanceof AppInfo) {
1379c476d6e192494b14b91e76cbce5c9cec01588059Jon Miranda                // Came from all apps -- make a copy.
1380c476d6e192494b14b91e76cbce5c9cec01588059Jon Miranda                si = ((AppInfo) d.dragInfo).makeShortcut();
1381c476d6e192494b14b91e76cbce5c9cec01588059Jon Miranda            } else {
1382c476d6e192494b14b91e76cbce5c9cec01588059Jon Miranda                // ShortcutInfo
1383c476d6e192494b14b91e76cbce5c9cec01588059Jon Miranda                si = (ShortcutInfo) d.dragInfo;
138495abbb330ce9bbaf23594245f0f8a795c8118038Sunny Goyal            }
138555bd9725d5c0373b89f7b9bbd9547550ea3bbc63Jorim Jaggi
1386c476d6e192494b14b91e76cbce5c9cec01588059Jon Miranda            View currentDragView;
1387c476d6e192494b14b91e76cbce5c9cec01588059Jon Miranda            if (mIsExternalDrag) {
13888701cd58cf3b32c9d2b9269e3956b32f4726a81aJonathan Miranda                currentDragView = mContent.createAndAddViewForRank(si, mEmptyCellRank);
13898701cd58cf3b32c9d2b9269e3956b32f4726a81aJonathan Miranda
1390c476d6e192494b14b91e76cbce5c9cec01588059Jon Miranda                // Actually move the item in the database if it was an external drag. Call this
1391c476d6e192494b14b91e76cbce5c9cec01588059Jon Miranda                // before creating the view, so that ShortcutInfo is updated appropriately.
13928701cd58cf3b32c9d2b9269e3956b32f4726a81aJonathan Miranda                mLauncher.getModelWriter().addOrMoveItemInDatabase(
13938701cd58cf3b32c9d2b9269e3956b32f4726a81aJonathan Miranda                        si, mInfo.id, 0, si.cellX, si.cellY);
1394c476d6e192494b14b91e76cbce5c9cec01588059Jon Miranda
13958701cd58cf3b32c9d2b9269e3956b32f4726a81aJonathan Miranda                // We only need to update the locations if it doesn't get handled in
13968701cd58cf3b32c9d2b9269e3956b32f4726a81aJonathan Miranda                // #onDropCompleted.
13978701cd58cf3b32c9d2b9269e3956b32f4726a81aJonathan Miranda                if (d.dragSource != this) {
13988701cd58cf3b32c9d2b9269e3956b32f4726a81aJonathan Miranda                    updateItemLocationsInDatabaseBatch();
13998701cd58cf3b32c9d2b9269e3956b32f4726a81aJonathan Miranda                }
14008701cd58cf3b32c9d2b9269e3956b32f4726a81aJonathan Miranda                mIsExternalDrag = false;
14018701cd58cf3b32c9d2b9269e3956b32f4726a81aJonathan Miranda            } else {
14028701cd58cf3b32c9d2b9269e3956b32f4726a81aJonathan Miranda                currentDragView = mCurrentDragView;
14038701cd58cf3b32c9d2b9269e3956b32f4726a81aJonathan Miranda                mContent.addViewForRank(currentDragView, si, mEmptyCellRank);
14048701cd58cf3b32c9d2b9269e3956b32f4726a81aJonathan Miranda            }
140569c340a05e846b4b1b2d457164fd313e249df353Jon Miranda
1406c476d6e192494b14b91e76cbce5c9cec01588059Jon Miranda            if (d.dragView.hasDrawn()) {
1407c476d6e192494b14b91e76cbce5c9cec01588059Jon Miranda                // Temporarily reset the scale such that the animation target gets calculated
1408c476d6e192494b14b91e76cbce5c9cec01588059Jon Miranda                // correctly.
1409c476d6e192494b14b91e76cbce5c9cec01588059Jon Miranda                float scaleX = getScaleX();
1410c476d6e192494b14b91e76cbce5c9cec01588059Jon Miranda                float scaleY = getScaleY();
1411c476d6e192494b14b91e76cbce5c9cec01588059Jon Miranda                setScaleX(1.0f);
1412c476d6e192494b14b91e76cbce5c9cec01588059Jon Miranda                setScaleY(1.0f);
1413c476d6e192494b14b91e76cbce5c9cec01588059Jon Miranda                mLauncher.getDragLayer().animateViewIntoPosition(d.dragView, currentDragView,
1414c476d6e192494b14b91e76cbce5c9cec01588059Jon Miranda                        cleanUpRunnable, null);
1415c476d6e192494b14b91e76cbce5c9cec01588059Jon Miranda                setScaleX(scaleX);
1416c476d6e192494b14b91e76cbce5c9cec01588059Jon Miranda                setScaleY(scaleY);
1417c476d6e192494b14b91e76cbce5c9cec01588059Jon Miranda            } else {
1418c476d6e192494b14b91e76cbce5c9cec01588059Jon Miranda                d.deferDragViewCleanupPostAnimation = false;
1419c476d6e192494b14b91e76cbce5c9cec01588059Jon Miranda                currentDragView.setVisibility(VISIBLE);
1420c476d6e192494b14b91e76cbce5c9cec01588059Jon Miranda            }
1421c476d6e192494b14b91e76cbce5c9cec01588059Jon Miranda
14228701cd58cf3b32c9d2b9269e3956b32f4726a81aJonathan Miranda            mItemsInvalidated = true;
14238701cd58cf3b32c9d2b9269e3956b32f4726a81aJonathan Miranda            rearrangeChildren();
14248701cd58cf3b32c9d2b9269e3956b32f4726a81aJonathan Miranda
1425c476d6e192494b14b91e76cbce5c9cec01588059Jon Miranda            // Temporarily suppress the listener, as we did all the work already here.
1426c476d6e192494b14b91e76cbce5c9cec01588059Jon Miranda            try (SuppressInfoChanges s = new SuppressInfoChanges()) {
1427c476d6e192494b14b91e76cbce5c9cec01588059Jon Miranda                mInfo.add(si, false);
1428c476d6e192494b14b91e76cbce5c9cec01588059Jon Miranda            }
1429e393d3af369c452f3b5f5d9c0a23a83aa3aa736bSunny Goyal        }
1430e393d3af369c452f3b5f5d9c0a23a83aa3aa736bSunny Goyal
14314b02017fa5bdb276347d0b9e8a76a9fdd5bb0eb9Sunny Goyal        // Clear the drag info, as it is no longer being dragged.
1432f406615354a92367481a60966b6b873cae37fe4cSunny Goyal        mDragInProgress = false;
1433b7e15adc7b67cb1d70ca72014b200dd6cbbf166aSunny Goyal
1434b7e15adc7b67cb1d70ca72014b200dd6cbbf166aSunny Goyal        if (mContent.getPageCount() > 1) {
1435b7e15adc7b67cb1d70ca72014b200dd6cbbf166aSunny Goyal            // The animation has already been shown while opening the folder.
143643bf11d9c95f76c2dfeb625b23cb458df81252b3Sunny Goyal            mInfo.setOption(FolderInfo.FLAG_MULTI_PAGE_ANIMATION, true, mLauncher.getModelWriter());
1437b7e15adc7b67cb1d70ca72014b200dd6cbbf166aSunny Goyal        }
14383f8864069be20d14debacddee610062f3feae5a1Sunny Goyal
14393f8864069be20d14debacddee610062f3feae5a1Sunny Goyal        if (d.stateAnnouncer != null) {
14403f8864069be20d14debacddee610062f3feae5a1Sunny Goyal            d.stateAnnouncer.completeAction(R.string.item_moved);
14413f8864069be20d14debacddee610062f3feae5a1Sunny Goyal        }
1442bfbfd26c627a18f8e1e3e6d0e53e78feab360203Adam Cohen    }
1443bfbfd26c627a18f8e1e3e6d0e53e78feab360203Adam Cohen
14447a8b82b5926e6a520df28e7f591b9a94ec605581Adam Cohen    // This is used so the item doesn't immediately appear in the folder when added. In one case
14457a8b82b5926e6a520df28e7f591b9a94ec605581Adam Cohen    // we need to create the illusion that the item isn't added back to the folder yet, to
14467a8b82b5926e6a520df28e7f591b9a94ec605581Adam Cohen    // to correspond to the animation of the icon back into the folder. This is
14477a8b82b5926e6a520df28e7f591b9a94ec605581Adam Cohen    public void hideItem(ShortcutInfo info) {
14487a8b82b5926e6a520df28e7f591b9a94ec605581Adam Cohen        View v = getViewForInfo(info);
14497a8b82b5926e6a520df28e7f591b9a94ec605581Adam Cohen        v.setVisibility(INVISIBLE);
14507a8b82b5926e6a520df28e7f591b9a94ec605581Adam Cohen    }
14517a8b82b5926e6a520df28e7f591b9a94ec605581Adam Cohen    public void showItem(ShortcutInfo info) {
14527a8b82b5926e6a520df28e7f591b9a94ec605581Adam Cohen        View v = getViewForInfo(info);
14537a8b82b5926e6a520df28e7f591b9a94ec605581Adam Cohen        v.setVisibility(VISIBLE);
14547a8b82b5926e6a520df28e7f591b9a94ec605581Adam Cohen    }
14557a8b82b5926e6a520df28e7f591b9a94ec605581Adam Cohen
1456b7e15adc7b67cb1d70ca72014b200dd6cbbf166aSunny Goyal    @Override
1457c476d6e192494b14b91e76cbce5c9cec01588059Jon Miranda    public void onAdd(ShortcutInfo item, int rank) {
1458c476d6e192494b14b91e76cbce5c9cec01588059Jon Miranda        View view = mContent.createAndAddViewForRank(item, rank);
14594e54a4cfa50838ea0a970fea2632851bcb16e2c0Jon Miranda        mLauncher.getModelWriter().addOrMoveItemInDatabase(item, mInfo.id, 0, item.cellX,
14604e54a4cfa50838ea0a970fea2632851bcb16e2c0Jon Miranda                item.cellY);
14614e54a4cfa50838ea0a970fea2632851bcb16e2c0Jon Miranda
14628701cd58cf3b32c9d2b9269e3956b32f4726a81aJonathan Miranda        ArrayList<View> items = new ArrayList<>(getItemsInReadingOrder());
1463c476d6e192494b14b91e76cbce5c9cec01588059Jon Miranda        items.add(rank, view);
1464c476d6e192494b14b91e76cbce5c9cec01588059Jon Miranda        mContent.arrangeChildren(items, items.size());
14652e688a8cc217b69cd6cd46a7a408ca3dbfa48cb9Sunny Goyal        mItemsInvalidated = true;
1466bfbfd26c627a18f8e1e3e6d0e53e78feab360203Adam Cohen    }
1467bfbfd26c627a18f8e1e3e6d0e53e78feab360203Adam Cohen
1468a9cf38f533d1e86269868f1e6a806ccffd4a78fcAdam Cohen    public void onRemove(ShortcutInfo item) {
14697c6932140dcbd2db88d7a809ba72ee04abb5bf26Adam Cohen        mItemsInvalidated = true;
1470df1e4e868a1970b756486dadd652651d40276d36Adam Cohen        View v = getViewForInfo(item);
1471290800b5b7d575fd709f244f54a5fa5b63b58876Sunny Goyal        mContent.removeItem(v);
14722801cafe62653131fdc9da402e5c44e5ffd0bf47Adam Cohen        if (mState == STATE_ANIMATING) {
14732801cafe62653131fdc9da402e5c44e5ffd0bf47Adam Cohen            mRearrangeOnClose = true;
14742801cafe62653131fdc9da402e5c44e5ffd0bf47Adam Cohen        } else {
1475c3a609f950a713d995f1968574d8ed7b4449f415Sunny Goyal            rearrangeChildren();
14762801cafe62653131fdc9da402e5c44e5ffd0bf47Adam Cohen        }
1477afb01ee74243cede19088e694ca82cea5983c603Adam Cohen        if (getItemCount() <= 1) {
1478740ac7f00e0b847b8e392800f7948d93493e11d6Sunny Goyal            if (mIsOpen) {
1479740ac7f00e0b847b8e392800f7948d93493e11d6Sunny Goyal                close(true);
1480b9c29d455e301d1b31b911a37eb3bec7e0e16865Sunny Goyal            } else {
1481b9c29d455e301d1b31b911a37eb3bec7e0e16865Sunny Goyal                replaceFolderWithFinalItem();
1482b9c29d455e301d1b31b911a37eb3bec7e0e16865Sunny Goyal            }
1483afb01ee74243cede19088e694ca82cea5983c603Adam Cohen        }
1484a9cf38f533d1e86269868f1e6a806ccffd4a78fcAdam Cohen    }
14857c6932140dcbd2db88d7a809ba72ee04abb5bf26Adam Cohen
1486bc753359f8b08e0813016eebc8392b83a4d2bd6eSunny Goyal    private View getViewForInfo(final ShortcutInfo item) {
1487bc753359f8b08e0813016eebc8392b83a4d2bd6eSunny Goyal        return mContent.iterateOverItems(new ItemOperator() {
1488bc753359f8b08e0813016eebc8392b83a4d2bd6eSunny Goyal
1489bc753359f8b08e0813016eebc8392b83a4d2bd6eSunny Goyal            @Override
1490c52ba710053e4c6927937dd1a26d1abe06b6fa99Sunny Goyal            public boolean evaluate(ItemInfo info, View view) {
1491bc753359f8b08e0813016eebc8392b83a4d2bd6eSunny Goyal                return info == item;
1492df1e4e868a1970b756486dadd652651d40276d36Adam Cohen            }
1493bc753359f8b08e0813016eebc8392b83a4d2bd6eSunny Goyal        });
1494df1e4e868a1970b756486dadd652651d40276d36Adam Cohen    }
1495df1e4e868a1970b756486dadd652651d40276d36Adam Cohen
1496c52ba710053e4c6927937dd1a26d1abe06b6fa99Sunny Goyal    @Override
1497c52ba710053e4c6927937dd1a26d1abe06b6fa99Sunny Goyal    public void onItemsChanged(boolean animate) {
1498ac56cff1860b71d3f164aedd268703936e08fdc0Adam Cohen        updateTextViewFocus();
149976078c4ef35b6e95fb6d8129a32cf52a36625e28Adam Cohen    }
1500ac56cff1860b71d3f164aedd268703936e08fdc0Adam Cohen
1501440609453f14fa8ce4bec1d70032f0b7f9614d4eJon Miranda    @Override
1502608d0dba8717326319800a78cfa85f3ad08b9549Jon Miranda    public void prepareAutoUpdate() {
1503440609453f14fa8ce4bec1d70032f0b7f9614d4eJon Miranda        close(false);
1504440609453f14fa8ce4bec1d70032f0b7f9614d4eJon Miranda    }
1505440609453f14fa8ce4bec1d70032f0b7f9614d4eJon Miranda
150690cb3e51af988d526912aed7a532745d729a844cSunny Goyal    public void onTitleChanged(CharSequence title) {
150790cb3e51af988d526912aed7a532745d729a844cSunny Goyal    }
150890cb3e51af988d526912aed7a532745d729a844cSunny Goyal
15098701cd58cf3b32c9d2b9269e3956b32f4726a81aJonathan Miranda    public ArrayList<View> getItemsInReadingOrder() {
15107c6932140dcbd2db88d7a809ba72ee04abb5bf26Adam Cohen        if (mItemsInvalidated) {
15118701cd58cf3b32c9d2b9269e3956b32f4726a81aJonathan Miranda            mItemsInReadingOrder.clear();
15128701cd58cf3b32c9d2b9269e3956b32f4726a81aJonathan Miranda            mContent.iterateOverItems(new ItemOperator() {
1513bc753359f8b08e0813016eebc8392b83a4d2bd6eSunny Goyal
15148701cd58cf3b32c9d2b9269e3956b32f4726a81aJonathan Miranda                @Override
15158701cd58cf3b32c9d2b9269e3956b32f4726a81aJonathan Miranda                public boolean evaluate(ItemInfo info, View view) {
15168701cd58cf3b32c9d2b9269e3956b32f4726a81aJonathan Miranda                    mItemsInReadingOrder.add(view);
15178701cd58cf3b32c9d2b9269e3956b32f4726a81aJonathan Miranda                    return false;
15188701cd58cf3b32c9d2b9269e3956b32f4726a81aJonathan Miranda                }
15198701cd58cf3b32c9d2b9269e3956b32f4726a81aJonathan Miranda            });
15207c6932140dcbd2db88d7a809ba72ee04abb5bf26Adam Cohen            mItemsInvalidated = false;
15217c6932140dcbd2db88d7a809ba72ee04abb5bf26Adam Cohen        }
15228701cd58cf3b32c9d2b9269e3956b32f4726a81aJonathan Miranda        return mItemsInReadingOrder;
15237c6932140dcbd2db88d7a809ba72ee04abb5bf26Adam Cohen    }
15248dfcba4af7a7ece09e8c7d96053e54f3a383e905Adam Cohen
152527bdbcae7fc43612c6016f625038e93bb3bd1e7bJon Miranda    public List<BubbleTextView> getItemsOnPage(int page) {
15268701cd58cf3b32c9d2b9269e3956b32f4726a81aJonathan Miranda        ArrayList<View> allItems = getItemsInReadingOrder();
1527471701162642d4cbfc5cebdd448e9a7af4c7c51fJon Miranda        int lastPage = mContent.getPageCount() - 1;
1528471701162642d4cbfc5cebdd448e9a7af4c7c51fJon Miranda        int totalItemsInFolder = allItems.size();
1529471701162642d4cbfc5cebdd448e9a7af4c7c51fJon Miranda        int itemsPerPage = mContent.itemsPerPage();
153027bdbcae7fc43612c6016f625038e93bb3bd1e7bJon Miranda        int numItemsOnCurrentPage = page == lastPage
153127bdbcae7fc43612c6016f625038e93bb3bd1e7bJon Miranda                ? totalItemsInFolder - (itemsPerPage * page)
1532471701162642d4cbfc5cebdd448e9a7af4c7c51fJon Miranda                : itemsPerPage;
1533471701162642d4cbfc5cebdd448e9a7af4c7c51fJon Miranda
153427bdbcae7fc43612c6016f625038e93bb3bd1e7bJon Miranda        int startIndex = page * itemsPerPage;
153527bdbcae7fc43612c6016f625038e93bb3bd1e7bJon Miranda        int endIndex = Math.min(startIndex + numItemsOnCurrentPage, allItems.size());
1536471701162642d4cbfc5cebdd448e9a7af4c7c51fJon Miranda
1537471701162642d4cbfc5cebdd448e9a7af4c7c51fJon Miranda        List<BubbleTextView> itemsOnCurrentPage = new ArrayList<>(numItemsOnCurrentPage);
1538471701162642d4cbfc5cebdd448e9a7af4c7c51fJon Miranda        for (int i = startIndex; i < endIndex; ++i) {
1539471701162642d4cbfc5cebdd448e9a7af4c7c51fJon Miranda            itemsOnCurrentPage.add((BubbleTextView) allItems.get(i));
1540471701162642d4cbfc5cebdd448e9a7af4c7c51fJon Miranda        }
1541471701162642d4cbfc5cebdd448e9a7af4c7c51fJon Miranda        return itemsOnCurrentPage;
1542471701162642d4cbfc5cebdd448e9a7af4c7c51fJon Miranda    }
1543471701162642d4cbfc5cebdd448e9a7af4c7c51fJon Miranda
1544ea0818d475df26d498ce08105bc2bc5582fce5a9Adam Cohen    public void onFocusChange(View v, boolean hasFocus) {
1545b740f59c7d572d54402e3a74fb2a23c0f1b1af48Sunny Goyal        if (v == mFolderName) {
1546b740f59c7d572d54402e3a74fb2a23c0f1b1af48Sunny Goyal            if (hasFocus) {
1547b740f59c7d572d54402e3a74fb2a23c0f1b1af48Sunny Goyal                startEditingFolderName();
1548b740f59c7d572d54402e3a74fb2a23c0f1b1af48Sunny Goyal            } else {
1549740ac7f00e0b847b8e392800f7948d93493e11d6Sunny Goyal                mFolderName.dispatchBackKey();
1550b740f59c7d572d54402e3a74fb2a23c0f1b1af48Sunny Goyal            }
1551ea0818d475df26d498ce08105bc2bc5582fce5a9Adam Cohen        }
1552ea0818d475df26d498ce08105bc2bc5582fce5a9Adam Cohen    }
15537d30a37007bac318db1c9af47a9af12d348042a5Adam Cohen
15547d30a37007bac318db1c9af47a9af12d348042a5Adam Cohen    @Override
15557d30a37007bac318db1c9af47a9af12d348042a5Adam Cohen    public void getHitRectRelativeToDragLayer(Rect outRect) {
15567d30a37007bac318db1c9af47a9af12d348042a5Adam Cohen        getHitRect(outRect);
15574846193300245c8c0a1f9bde3175f273df044309Sunny Goyal        outRect.left -= mScrollAreaOffset;
15584846193300245c8c0a1f9bde3175f273df044309Sunny Goyal        outRect.right += mScrollAreaOffset;
15594846193300245c8c0a1f9bde3175f273df044309Sunny Goyal    }
15604846193300245c8c0a1f9bde3175f273df044309Sunny Goyal
15618f1eff7b6cc8621888ee46605c32e601f80a890bWinson Chung    @Override
1562df7ef68bbf36f8bbf015ace74fce0e00142b2e23Hyunyoung Song    public void fillInLogContainerData(View v, ItemInfo info, Target target, Target targetParent) {
1563ddec1c739ef37c3a042982b8943fe42e04b65f4cHyunyoung Song        target.gridX = info.cellX;
1564ddec1c739ef37c3a042982b8943fe42e04b65f4cHyunyoung Song        target.gridY = info.cellY;
1565ddec1c739ef37c3a042982b8943fe42e04b65f4cHyunyoung Song        target.pageIndex = mContent.getCurrentPage();
15666c46a6d324310bd2fc0ea223c9782ba6394512e7Sunny Goyal        targetParent.containerType = ContainerType.FOLDER;
15678f1eff7b6cc8621888ee46605c32e601f80a890bWinson Chung    }
15688f1eff7b6cc8621888ee46605c32e601f80a890bWinson Chung
15694846193300245c8c0a1f9bde3175f273df044309Sunny Goyal    private class OnScrollHintListener implements OnAlarmListener {
15704846193300245c8c0a1f9bde3175f273df044309Sunny Goyal
15714846193300245c8c0a1f9bde3175f273df044309Sunny Goyal        private final DragObject mDragObject;
15724846193300245c8c0a1f9bde3175f273df044309Sunny Goyal
15734846193300245c8c0a1f9bde3175f273df044309Sunny Goyal        OnScrollHintListener(DragObject object) {
15744846193300245c8c0a1f9bde3175f273df044309Sunny Goyal            mDragObject = object;
15754846193300245c8c0a1f9bde3175f273df044309Sunny Goyal        }
15764846193300245c8c0a1f9bde3175f273df044309Sunny Goyal
15774846193300245c8c0a1f9bde3175f273df044309Sunny Goyal        /**
15784846193300245c8c0a1f9bde3175f273df044309Sunny Goyal         * Scroll hint has been shown long enough. Now scroll to appropriate page.
15794846193300245c8c0a1f9bde3175f273df044309Sunny Goyal         */
15804846193300245c8c0a1f9bde3175f273df044309Sunny Goyal        @Override
15814846193300245c8c0a1f9bde3175f273df044309Sunny Goyal        public void onAlarm(Alarm alarm) {
1582f5440cbd6c0525769d24b890e16313a728831e04Sunny Goyal            if (mCurrentScrollDir == SCROLL_LEFT) {
1583b863415c17aaaf6012647df5ed14803f89f94bcbSunny Goyal                mContent.scrollLeft();
1584f5440cbd6c0525769d24b890e16313a728831e04Sunny Goyal                mScrollHintDir = SCROLL_NONE;
1585f5440cbd6c0525769d24b890e16313a728831e04Sunny Goyal            } else if (mCurrentScrollDir == SCROLL_RIGHT) {
1586b863415c17aaaf6012647df5ed14803f89f94bcbSunny Goyal                mContent.scrollRight();
1587f5440cbd6c0525769d24b890e16313a728831e04Sunny Goyal                mScrollHintDir = SCROLL_NONE;
15884846193300245c8c0a1f9bde3175f273df044309Sunny Goyal            } else {
15894846193300245c8c0a1f9bde3175f273df044309Sunny Goyal                // This should not happen
15904846193300245c8c0a1f9bde3175f273df044309Sunny Goyal                return;
15914846193300245c8c0a1f9bde3175f273df044309Sunny Goyal            }
1592f5440cbd6c0525769d24b890e16313a728831e04Sunny Goyal            mCurrentScrollDir = SCROLL_NONE;
15934846193300245c8c0a1f9bde3175f273df044309Sunny Goyal
15944846193300245c8c0a1f9bde3175f273df044309Sunny Goyal            // Pause drag event until the scrolling is finished
15954846193300245c8c0a1f9bde3175f273df044309Sunny Goyal            mScrollPauseAlarm.setOnAlarmListener(new OnScrollFinishedListener(mDragObject));
1596f5440cbd6c0525769d24b890e16313a728831e04Sunny Goyal            mScrollPauseAlarm.setAlarm(RESCROLL_DELAY);
15974846193300245c8c0a1f9bde3175f273df044309Sunny Goyal        }
15984846193300245c8c0a1f9bde3175f273df044309Sunny Goyal    }
15994846193300245c8c0a1f9bde3175f273df044309Sunny Goyal
16004846193300245c8c0a1f9bde3175f273df044309Sunny Goyal    private class OnScrollFinishedListener implements OnAlarmListener {
16014846193300245c8c0a1f9bde3175f273df044309Sunny Goyal
16024846193300245c8c0a1f9bde3175f273df044309Sunny Goyal        private final DragObject mDragObject;
16034846193300245c8c0a1f9bde3175f273df044309Sunny Goyal
16044846193300245c8c0a1f9bde3175f273df044309Sunny Goyal        OnScrollFinishedListener(DragObject object) {
16054846193300245c8c0a1f9bde3175f273df044309Sunny Goyal            mDragObject = object;
16064846193300245c8c0a1f9bde3175f273df044309Sunny Goyal        }
16074846193300245c8c0a1f9bde3175f273df044309Sunny Goyal
16084846193300245c8c0a1f9bde3175f273df044309Sunny Goyal        /**
16094846193300245c8c0a1f9bde3175f273df044309Sunny Goyal         * Page scroll is complete.
16104846193300245c8c0a1f9bde3175f273df044309Sunny Goyal         */
16114846193300245c8c0a1f9bde3175f273df044309Sunny Goyal        @Override
16124846193300245c8c0a1f9bde3175f273df044309Sunny Goyal        public void onAlarm(Alarm alarm) {
16134846193300245c8c0a1f9bde3175f273df044309Sunny Goyal            // Reorder immediately on page change.
16144846193300245c8c0a1f9bde3175f273df044309Sunny Goyal            onDragOver(mDragObject, 1);
16154846193300245c8c0a1f9bde3175f273df044309Sunny Goyal        }
16167d30a37007bac318db1c9af47a9af12d348042a5Adam Cohen    }
16171dd0f8bf51d4e2befe0caece0e361a96ae697f41Sunny Goyal
16181dd0f8bf51d4e2befe0caece0e361a96ae697f41Sunny Goyal    // Compares item position based on rank and position giving priority to the rank.
1619317698bd012c1930b83bb4e2e47ac9e363fa6c6aSunny Goyal    public static final Comparator<ItemInfo> ITEM_POS_COMPARATOR = new Comparator<ItemInfo>() {
16201dd0f8bf51d4e2befe0caece0e361a96ae697f41Sunny Goyal
16211dd0f8bf51d4e2befe0caece0e361a96ae697f41Sunny Goyal        @Override
16221dd0f8bf51d4e2befe0caece0e361a96ae697f41Sunny Goyal        public int compare(ItemInfo lhs, ItemInfo rhs) {
16231dd0f8bf51d4e2befe0caece0e361a96ae697f41Sunny Goyal            if (lhs.rank != rhs.rank) {
16241dd0f8bf51d4e2befe0caece0e361a96ae697f41Sunny Goyal                return lhs.rank - rhs.rank;
16251dd0f8bf51d4e2befe0caece0e361a96ae697f41Sunny Goyal            } else if (lhs.cellY != rhs.cellY) {
16261dd0f8bf51d4e2befe0caece0e361a96ae697f41Sunny Goyal                return lhs.cellY - rhs.cellY;
16271dd0f8bf51d4e2befe0caece0e361a96ae697f41Sunny Goyal            } else {
16281dd0f8bf51d4e2befe0caece0e361a96ae697f41Sunny Goyal                return lhs.cellX - rhs.cellX;
16291dd0f8bf51d4e2befe0caece0e361a96ae697f41Sunny Goyal            }
16301dd0f8bf51d4e2befe0caece0e361a96ae697f41Sunny Goyal        }
16311dd0f8bf51d4e2befe0caece0e361a96ae697f41Sunny Goyal    };
1632e393d3af369c452f3b5f5d9c0a23a83aa3aa736bSunny Goyal
1633e393d3af369c452f3b5f5d9c0a23a83aa3aa736bSunny Goyal    /**
1634e393d3af369c452f3b5f5d9c0a23a83aa3aa736bSunny Goyal     * Temporary resource held while we don't want to handle info changes
1635e393d3af369c452f3b5f5d9c0a23a83aa3aa736bSunny Goyal     */
1636e393d3af369c452f3b5f5d9c0a23a83aa3aa736bSunny Goyal    private class SuppressInfoChanges implements AutoCloseable {
1637e393d3af369c452f3b5f5d9c0a23a83aa3aa736bSunny Goyal
1638e393d3af369c452f3b5f5d9c0a23a83aa3aa736bSunny Goyal        SuppressInfoChanges() {
1639e393d3af369c452f3b5f5d9c0a23a83aa3aa736bSunny Goyal            mInfo.removeListener(Folder.this);
1640e393d3af369c452f3b5f5d9c0a23a83aa3aa736bSunny Goyal        }
1641e393d3af369c452f3b5f5d9c0a23a83aa3aa736bSunny Goyal
1642e393d3af369c452f3b5f5d9c0a23a83aa3aa736bSunny Goyal        @Override
1643e393d3af369c452f3b5f5d9c0a23a83aa3aa736bSunny Goyal        public void close() {
1644e393d3af369c452f3b5f5d9c0a23a83aa3aa736bSunny Goyal            mInfo.addListener(Folder.this);
1645e393d3af369c452f3b5f5d9c0a23a83aa3aa736bSunny Goyal            updateTextViewFocus();
1646e393d3af369c452f3b5f5d9c0a23a83aa3aa736bSunny Goyal        }
1647e393d3af369c452f3b5f5d9c0a23a83aa3aa736bSunny Goyal    }
1648740ac7f00e0b847b8e392800f7948d93493e11d6Sunny Goyal
1649740ac7f00e0b847b8e392800f7948d93493e11d6Sunny Goyal    /**
1650740ac7f00e0b847b8e392800f7948d93493e11d6Sunny Goyal     * Returns a folder which is already open or null
1651740ac7f00e0b847b8e392800f7948d93493e11d6Sunny Goyal     */
1652740ac7f00e0b847b8e392800f7948d93493e11d6Sunny Goyal    public static Folder getOpen(Launcher launcher) {
1653740ac7f00e0b847b8e392800f7948d93493e11d6Sunny Goyal        return getOpenView(launcher, TYPE_FOLDER);
1654740ac7f00e0b847b8e392800f7948d93493e11d6Sunny Goyal    }
1655c6cf493b07fc7234c2a0a164318fd9aeb02e48f3Jon Miranda
1656c6cf493b07fc7234c2a0a164318fd9aeb02e48f3Jon Miranda    @Override
1657c6cf493b07fc7234c2a0a164318fd9aeb02e48f3Jon Miranda    public int getLogContainerType() {
1658c6cf493b07fc7234c2a0a164318fd9aeb02e48f3Jon Miranda        return ContainerType.FOLDER;
1659c6cf493b07fc7234c2a0a164318fd9aeb02e48f3Jon Miranda    }
166031dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project}
1661