The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2008 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Daniel Sandler | 325dc23 | 2013-06-05 22:57:57 -0400 | [diff] [blame] | 17 | package com.android.launcher3; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 18 | |
Adam Cohen | df2cc41 | 2011-04-27 16:56:57 -0700 | [diff] [blame] | 19 | import android.animation.Animator; |
| 20 | import android.animation.AnimatorListenerAdapter; |
Adam Cohen | c4fe9ea | 2014-08-18 18:54:10 -0700 | [diff] [blame] | 21 | import android.animation.AnimatorSet; |
Adam Cohen | df2cc41 | 2011-04-27 16:56:57 -0700 | [diff] [blame] | 22 | import android.animation.ObjectAnimator; |
| 23 | import android.animation.PropertyValuesHolder; |
Sunny Goyal | c46bfef | 2015-01-05 12:40:08 -0800 | [diff] [blame] | 24 | import android.annotation.TargetApi; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 25 | import android.content.Context; |
Adam Cohen | 76fc085 | 2011-06-17 13:26:23 -0700 | [diff] [blame] | 26 | import android.content.res.Resources; |
Winson Chung | b745afb | 2015-03-02 11:51:23 -0800 | [diff] [blame] | 27 | import android.graphics.Point; |
Winson Chung | 043f2af | 2012-03-01 16:09:54 -0800 | [diff] [blame] | 28 | import android.graphics.PointF; |
Romain Guy | fb5411e | 2010-02-24 10:04:17 -0800 | [diff] [blame] | 29 | import android.graphics.Rect; |
Sunny Goyal | c46bfef | 2015-01-05 12:40:08 -0800 | [diff] [blame] | 30 | import android.os.Build; |
Adam Cohen | 7a14d0b | 2011-06-24 11:36:35 -0700 | [diff] [blame] | 31 | import android.text.InputType; |
Adam Cohen | e601a43 | 2011-07-26 21:51:30 -0700 | [diff] [blame] | 32 | import android.text.Selection; |
| 33 | import android.text.Spannable; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 34 | import android.util.AttributeSet; |
Adam Cohen | 3bf84d3 | 2012-05-07 20:17:14 -0700 | [diff] [blame] | 35 | import android.util.Log; |
Adam Cohen | 76fc085 | 2011-06-17 13:26:23 -0700 | [diff] [blame] | 36 | import android.view.ActionMode; |
| 37 | import android.view.KeyEvent; |
Adam Cohen | df2cc41 | 2011-04-27 16:56:57 -0700 | [diff] [blame] | 38 | import android.view.LayoutInflater; |
Adam Cohen | 76fc085 | 2011-06-17 13:26:23 -0700 | [diff] [blame] | 39 | import android.view.Menu; |
| 40 | import android.view.MenuItem; |
Adam Cohen | 0c872ba | 2011-05-05 10:34:16 -0700 | [diff] [blame] | 41 | import android.view.MotionEvent; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 42 | import android.view.View; |
Sunny Goyal | c491835 | 2015-03-10 18:15:48 -0700 | [diff] [blame] | 43 | import android.view.ViewGroup; |
Adam Cohen | 3371da0 | 2011-10-25 21:38:29 -0700 | [diff] [blame] | 44 | import android.view.accessibility.AccessibilityEvent; |
| 45 | import android.view.accessibility.AccessibilityManager; |
Adam Cohen | c4fe9ea | 2014-08-18 18:54:10 -0700 | [diff] [blame] | 46 | import android.view.animation.AccelerateInterpolator; |
Adam Cohen | 76fc085 | 2011-06-17 13:26:23 -0700 | [diff] [blame] | 47 | import android.view.inputmethod.EditorInfo; |
| 48 | import android.view.inputmethod.InputMethodManager; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 49 | import android.widget.LinearLayout; |
Adam Cohen | df2cc41 | 2011-04-27 16:56:57 -0700 | [diff] [blame] | 50 | import android.widget.TextView; |
Sunny Goyal | 4846193 | 2015-03-09 17:41:09 -0700 | [diff] [blame] | 51 | |
Daniel Sandler | 325dc23 | 2013-06-05 22:57:57 -0400 | [diff] [blame] | 52 | import com.android.launcher3.FolderInfo.FolderListener; |
Sunny Goyal | bc75335 | 2015-03-05 09:40:44 -0800 | [diff] [blame] | 53 | import com.android.launcher3.Workspace.ItemOperator; |
Adam Cohen | 091440a | 2015-03-18 14:16:05 -0700 | [diff] [blame] | 54 | import com.android.launcher3.util.Thunk; |
Romain Guy | edcce09 | 2010-03-04 13:03:17 -0800 | [diff] [blame] | 55 | |
Adam Cohen | c0dcf59 | 2011-06-01 15:30:43 -0700 | [diff] [blame] | 56 | import java.util.ArrayList; |
Adam Cohen | 3bf84d3 | 2012-05-07 20:17:14 -0700 | [diff] [blame] | 57 | import java.util.Collections; |
Adam Cohen | c0dcf59 | 2011-06-01 15:30:43 -0700 | [diff] [blame] | 58 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 59 | /** |
| 60 | * Represents a set of icons chosen by the user or generated by the system. |
| 61 | */ |
Adam Cohen | 8dfcba4 | 2011-07-07 16:38:18 -0700 | [diff] [blame] | 62 | public class Folder extends LinearLayout implements DragSource, View.OnClickListener, |
Adam Cohen | ea0818d | 2011-09-30 20:06:58 -0700 | [diff] [blame] | 63 | View.OnLongClickListener, DropTarget, FolderListener, TextView.OnEditorActionListener, |
| 64 | View.OnFocusChangeListener { |
Adam Cohen | df2cc41 | 2011-04-27 16:56:57 -0700 | [diff] [blame] | 65 | private static final String TAG = "Launcher.Folder"; |
| 66 | |
Sunny Goyal | c3a609f | 2015-02-26 17:43:50 -0800 | [diff] [blame] | 67 | /** |
| 68 | * We avoid measuring {@link #mContentWrapper} with a 0 width or height, as this |
| 69 | * results in CellLayout being measured as UNSPECIFIED, which it does not support. |
| 70 | */ |
| 71 | private static final int MIN_CONTENT_DIMEN = 5; |
Sunny Goyal | 290800b | 2015-03-05 11:33:33 -0800 | [diff] [blame] | 72 | private static final boolean ALLOW_FOLDER_SCROLL = true; |
Adam Cohen | 4eac29a | 2011-07-11 17:53:37 -0700 | [diff] [blame] | 73 | |
Adam Cohen | df2cc41 | 2011-04-27 16:56:57 -0700 | [diff] [blame] | 74 | static final int STATE_NONE = -1; |
| 75 | static final int STATE_SMALL = 0; |
| 76 | static final int STATE_ANIMATING = 1; |
| 77 | static final int STATE_OPEN = 2; |
| 78 | |
Sunny Goyal | 4846193 | 2015-03-09 17:41:09 -0700 | [diff] [blame] | 79 | /** |
Sunny Goyal | 4846193 | 2015-03-09 17:41:09 -0700 | [diff] [blame] | 80 | * Time for which the scroll hint is shown before automatically changing page. |
| 81 | */ |
| 82 | public static final int SCROLL_HINT_DURATION = DragController.SCROLL_DELAY; |
| 83 | |
| 84 | /** |
Sunny Goyal | 5d85c44 | 2015-03-10 13:14:47 -0700 | [diff] [blame] | 85 | * Time in milliseconds for which an icon sticks to the target position |
| 86 | * in case of a sorted folder. |
| 87 | */ |
| 88 | private static final int SORTED_STICKY_REORDER_DELAY = 1500; |
| 89 | |
| 90 | /** |
Sunny Goyal | 4846193 | 2015-03-09 17:41:09 -0700 | [diff] [blame] | 91 | * Fraction of icon width which behave as scroll region. |
| 92 | */ |
| 93 | private static final float ICON_OVERSCROLL_WIDTH_FACTOR = 0.45f; |
| 94 | |
Adam Cohen | f0f4eda | 2013-06-06 21:27:03 -0700 | [diff] [blame] | 95 | private static final int REORDER_DELAY = 250; |
Adam Cohen | 5d518fa | 2013-12-05 14:16:23 -0800 | [diff] [blame] | 96 | private static final int ON_EXIT_CLOSE_DELAY = 400; |
Sunny Goyal | c3a609f | 2015-02-26 17:43:50 -0800 | [diff] [blame] | 97 | private static final Rect sTempRect = new Rect(); |
| 98 | |
| 99 | private static String sDefaultFolderName; |
| 100 | private static String sHintText; |
| 101 | |
| 102 | private final Alarm mReorderAlarm = new Alarm(); |
| 103 | private final Alarm mOnExitAlarm = new Alarm(); |
| 104 | |
Adam Cohen | 091440a | 2015-03-18 14:16:05 -0700 | [diff] [blame] | 105 | @Thunk final ArrayList<View> mItemsInReadingOrder = new ArrayList<View>(); |
Sunny Goyal | c3a609f | 2015-02-26 17:43:50 -0800 | [diff] [blame] | 106 | |
| 107 | private final int mExpandDuration; |
| 108 | private final int mMaterialExpandDuration; |
| 109 | private final int mMaterialExpandStagger; |
| 110 | |
| 111 | private final InputMethodManager mInputMethodManager; |
| 112 | |
| 113 | protected final Launcher mLauncher; |
| 114 | protected DragController mDragController; |
| 115 | protected FolderInfo mInfo; |
| 116 | |
Adam Cohen | 091440a | 2015-03-18 14:16:05 -0700 | [diff] [blame] | 117 | @Thunk FolderIcon mFolderIcon; |
Sunny Goyal | c3a609f | 2015-02-26 17:43:50 -0800 | [diff] [blame] | 118 | |
Adam Cohen | 091440a | 2015-03-18 14:16:05 -0700 | [diff] [blame] | 119 | @Thunk FolderContent mContent; |
| 120 | @Thunk View mContentWrapper; |
Sunny Goyal | c3a609f | 2015-02-26 17:43:50 -0800 | [diff] [blame] | 121 | FolderEditText mFolderName; |
| 122 | |
Sunny Goyal | 290800b | 2015-03-05 11:33:33 -0800 | [diff] [blame] | 123 | private View mFooter; |
| 124 | private int mFooterHeight; |
Sunny Goyal | c3a609f | 2015-02-26 17:43:50 -0800 | [diff] [blame] | 125 | |
| 126 | // Cell ranks used for drag and drop |
Adam Cohen | 091440a | 2015-03-18 14:16:05 -0700 | [diff] [blame] | 127 | @Thunk int mTargetRank, mPrevTargetRank, mEmptyCellRank; |
Sunny Goyal | c3a609f | 2015-02-26 17:43:50 -0800 | [diff] [blame] | 128 | |
Adam Cohen | 091440a | 2015-03-18 14:16:05 -0700 | [diff] [blame] | 129 | @Thunk int mState = STATE_NONE; |
Sunny Goyal | c3a609f | 2015-02-26 17:43:50 -0800 | [diff] [blame] | 130 | private boolean mRearrangeOnClose = false; |
Adam Cohen | 7c69321 | 2011-05-18 15:26:57 -0700 | [diff] [blame] | 131 | boolean mItemsInvalidated = false; |
Adam Cohen | bfbfd26 | 2011-06-13 16:55:12 -0700 | [diff] [blame] | 132 | private ShortcutInfo mCurrentDragInfo; |
| 133 | private View mCurrentDragView; |
Jorim Jaggi | 55bd972 | 2014-01-16 15:30:42 -0800 | [diff] [blame] | 134 | private boolean mIsExternalDrag; |
Adam Cohen | bfbfd26 | 2011-06-13 16:55:12 -0700 | [diff] [blame] | 135 | boolean mSuppressOnAdd = false; |
Adam Cohen | 67bd9cc | 2011-07-29 14:07:04 -0700 | [diff] [blame] | 136 | private boolean mDragInProgress = false; |
| 137 | private boolean mDeleteFolderOnDropCompleted = false; |
| 138 | private boolean mSuppressFolderDeletion = false; |
Adam Cohen | 05e0f40 | 2011-08-01 12:12:49 -0700 | [diff] [blame] | 139 | private boolean mItemAddedBackToSelfViaIcon = false; |
Adam Cohen | 091440a | 2015-03-18 14:16:05 -0700 | [diff] [blame] | 140 | @Thunk float mFolderIconPivotX; |
| 141 | @Thunk float mFolderIconPivotY; |
Adam Cohen | 76fc085 | 2011-06-17 13:26:23 -0700 | [diff] [blame] | 142 | private boolean mIsEditingName = false; |
Adam Cohen | 1960ea4 | 2013-11-12 11:33:14 +0000 | [diff] [blame] | 143 | |
Adam Cohen | fb91f30 | 2012-06-11 15:45:18 -0700 | [diff] [blame] | 144 | private boolean mDestroyed; |
| 145 | |
Adam Cohen | 091440a | 2015-03-18 14:16:05 -0700 | [diff] [blame] | 146 | @Thunk Runnable mDeferredAction; |
Michael Jurka | 1e2f465 | 2013-07-08 18:03:46 -0700 | [diff] [blame] | 147 | private boolean mDeferDropAfterUninstall; |
| 148 | private boolean mUninstallSuccessful; |
| 149 | |
Sunny Goyal | 4846193 | 2015-03-09 17:41:09 -0700 | [diff] [blame] | 150 | // Folder scrolling |
| 151 | private int mScrollAreaOffset; |
| 152 | private Alarm mOnScrollHintAlarm; |
Adam Cohen | 091440a | 2015-03-18 14:16:05 -0700 | [diff] [blame] | 153 | @Thunk Alarm mScrollPauseAlarm; |
Sunny Goyal | 4846193 | 2015-03-09 17:41:09 -0700 | [diff] [blame] | 154 | |
| 155 | // TODO: Use {@link #mContent} once {@link #ALLOW_FOLDER_SCROLL} is removed. |
Adam Cohen | 091440a | 2015-03-18 14:16:05 -0700 | [diff] [blame] | 156 | @Thunk FolderPagedView mPagedView; |
Sunny Goyal | 4846193 | 2015-03-09 17:41:09 -0700 | [diff] [blame] | 157 | |
Adam Cohen | 091440a | 2015-03-18 14:16:05 -0700 | [diff] [blame] | 158 | @Thunk int mScrollHintDir = DragController.SCROLL_NONE; |
| 159 | @Thunk int mCurrentScrollDir = DragController.SCROLL_NONE; |
Sunny Goyal | 4846193 | 2015-03-09 17:41:09 -0700 | [diff] [blame] | 160 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 161 | /** |
| 162 | * Used to inflate the Workspace from XML. |
| 163 | * |
| 164 | * @param context The application's context. |
Sunny Goyal | c3a609f | 2015-02-26 17:43:50 -0800 | [diff] [blame] | 165 | * @param attrs The attributes set containing the Workspace's customization values. |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 166 | */ |
| 167 | public Folder(Context context, AttributeSet attrs) { |
| 168 | super(context, attrs); |
| 169 | setAlwaysDrawnWithCacheEnabled(false); |
Adam Cohen | 76fc085 | 2011-06-17 13:26:23 -0700 | [diff] [blame] | 170 | mInputMethodManager = (InputMethodManager) |
Michael Jurka | 8b805b1 | 2012-04-18 14:23:14 -0700 | [diff] [blame] | 171 | getContext().getSystemService(Context.INPUT_METHOD_SERVICE); |
Adam Cohen | 76fc085 | 2011-06-17 13:26:23 -0700 | [diff] [blame] | 172 | |
Sunny Goyal | c3a609f | 2015-02-26 17:43:50 -0800 | [diff] [blame] | 173 | Resources res = getResources(); |
Adam Cohen | c4fe9ea | 2014-08-18 18:54:10 -0700 | [diff] [blame] | 174 | mExpandDuration = res.getInteger(R.integer.config_folderExpandDuration); |
| 175 | mMaterialExpandDuration = res.getInteger(R.integer.config_materialFolderExpandDuration); |
| 176 | mMaterialExpandStagger = res.getInteger(R.integer.config_materialFolderExpandStagger); |
Adam Cohen | 4ef610f | 2011-06-21 22:37:36 -0700 | [diff] [blame] | 177 | |
| 178 | if (sDefaultFolderName == null) { |
| 179 | sDefaultFolderName = res.getString(R.string.folder_name); |
| 180 | } |
Adam Cohen | a65beee | 2011-06-27 21:32:23 -0700 | [diff] [blame] | 181 | if (sHintText == null) { |
| 182 | sHintText = res.getString(R.string.folder_hint_text); |
| 183 | } |
Adam Cohen | 4eac29a | 2011-07-11 17:53:37 -0700 | [diff] [blame] | 184 | mLauncher = (Launcher) context; |
Adam Cohen | ac56cff | 2011-09-28 20:45:37 -0700 | [diff] [blame] | 185 | // We need this view to be focusable in touch mode so that when text editing of the folder |
| 186 | // name is complete, we have something to focus on, thus hiding the cursor and giving |
Sunny Goyal | c3a609f | 2015-02-26 17:43:50 -0800 | [diff] [blame] | 187 | // reliable behavior when clicking the text field (since it will always gain focus on click). |
Adam Cohen | ac56cff | 2011-09-28 20:45:37 -0700 | [diff] [blame] | 188 | setFocusableInTouchMode(true); |
Sunny Goyal | 4846193 | 2015-03-09 17:41:09 -0700 | [diff] [blame] | 189 | |
| 190 | if (ALLOW_FOLDER_SCROLL) { |
| 191 | mOnScrollHintAlarm = new Alarm(); |
| 192 | mScrollPauseAlarm = new Alarm(); |
| 193 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 194 | } |
| 195 | |
| 196 | @Override |
| 197 | protected void onFinishInflate() { |
| 198 | super.onFinishInflate(); |
Sunny Goyal | c3a609f | 2015-02-26 17:43:50 -0800 | [diff] [blame] | 199 | mContentWrapper = findViewById(R.id.folder_content_wrapper); |
Sunny Goyal | bc75335 | 2015-03-05 09:40:44 -0800 | [diff] [blame] | 200 | mContent = (FolderContent) findViewById(R.id.folder_content); |
Sunny Goyal | c3a609f | 2015-02-26 17:43:50 -0800 | [diff] [blame] | 201 | mContent.setFolder(this); |
Sunny Goyal | dcbcc86 | 2014-08-12 15:58:36 -0700 | [diff] [blame] | 202 | |
Adam Cohen | ac56cff | 2011-09-28 20:45:37 -0700 | [diff] [blame] | 203 | mFolderName = (FolderEditText) findViewById(R.id.folder_name); |
| 204 | mFolderName.setFolder(this); |
Adam Cohen | ea0818d | 2011-09-30 20:06:58 -0700 | [diff] [blame] | 205 | mFolderName.setOnFocusChangeListener(this); |
Adam Cohen | 76fc085 | 2011-06-17 13:26:23 -0700 | [diff] [blame] | 206 | |
Adam Cohen | 76fc085 | 2011-06-17 13:26:23 -0700 | [diff] [blame] | 207 | // We disable action mode for now since it messes up the view on phones |
| 208 | mFolderName.setCustomSelectionActionModeCallback(mActionModeCallback); |
Adam Cohen | 76fc085 | 2011-06-17 13:26:23 -0700 | [diff] [blame] | 209 | mFolderName.setOnEditorActionListener(this); |
Adam Cohen | 4ef610f | 2011-06-21 22:37:36 -0700 | [diff] [blame] | 210 | mFolderName.setSelectAllOnFocus(true); |
Adam Cohen | 7a14d0b | 2011-06-24 11:36:35 -0700 | [diff] [blame] | 211 | mFolderName.setInputType(mFolderName.getInputType() | |
| 212 | InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS | InputType.TYPE_TEXT_FLAG_CAP_WORDS); |
Sunny Goyal | c3a609f | 2015-02-26 17:43:50 -0800 | [diff] [blame] | 213 | |
Sunny Goyal | 290800b | 2015-03-05 11:33:33 -0800 | [diff] [blame] | 214 | mFooter = ALLOW_FOLDER_SCROLL ? findViewById(R.id.folder_footer) : mFolderName; |
| 215 | // We find out how tall footer wants to be (it is set to wrap_content), so that |
Sunny Goyal | c3a609f | 2015-02-26 17:43:50 -0800 | [diff] [blame] | 216 | // we can allocate the appropriate amount of space for it. |
| 217 | int measureSpec = MeasureSpec.UNSPECIFIED; |
Sunny Goyal | 290800b | 2015-03-05 11:33:33 -0800 | [diff] [blame] | 218 | mFooter.measure(measureSpec, measureSpec); |
| 219 | mFooterHeight = mFooter.getMeasuredHeight(); |
Sunny Goyal | 4846193 | 2015-03-09 17:41:09 -0700 | [diff] [blame] | 220 | |
| 221 | if (ALLOW_FOLDER_SCROLL) { |
| 222 | mPagedView = (FolderPagedView) mContent; |
| 223 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 224 | } |
Adam Cohen | 2801caf | 2011-05-13 20:57:39 -0700 | [diff] [blame] | 225 | |
Adam Cohen | 76fc085 | 2011-06-17 13:26:23 -0700 | [diff] [blame] | 226 | private ActionMode.Callback mActionModeCallback = new ActionMode.Callback() { |
| 227 | public boolean onActionItemClicked(ActionMode mode, MenuItem item) { |
| 228 | return false; |
| 229 | } |
| 230 | |
| 231 | public boolean onCreateActionMode(ActionMode mode, Menu menu) { |
| 232 | return false; |
| 233 | } |
| 234 | |
| 235 | public void onDestroyActionMode(ActionMode mode) { |
| 236 | } |
| 237 | |
| 238 | public boolean onPrepareActionMode(ActionMode mode, Menu menu) { |
| 239 | return false; |
| 240 | } |
| 241 | }; |
| 242 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 243 | public void onClick(View v) { |
Adam Cohen | df2cc41 | 2011-04-27 16:56:57 -0700 | [diff] [blame] | 244 | Object tag = v.getTag(); |
| 245 | if (tag instanceof ShortcutInfo) { |
Adam Cohen | b5fe60c | 2013-06-06 22:03:51 -0700 | [diff] [blame] | 246 | mLauncher.onClick(v); |
Adam Cohen | df2cc41 | 2011-04-27 16:56:57 -0700 | [diff] [blame] | 247 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 248 | } |
| 249 | |
| 250 | public boolean onLongClick(View v) { |
Winson Chung | 36a62fe | 2012-05-06 18:04:42 -0700 | [diff] [blame] | 251 | // Return if global dragging is not enabled |
| 252 | if (!mLauncher.isDraggingEnabled()) return true; |
| 253 | |
Adam Cohen | df2cc41 | 2011-04-27 16:56:57 -0700 | [diff] [blame] | 254 | Object tag = v.getTag(); |
| 255 | if (tag instanceof ShortcutInfo) { |
Adam Cohen | df2cc41 | 2011-04-27 16:56:57 -0700 | [diff] [blame] | 256 | ShortcutInfo item = (ShortcutInfo) tag; |
| 257 | if (!v.isInTouchMode()) { |
| 258 | return false; |
| 259 | } |
| 260 | |
Winson Chung | b745afb | 2015-03-02 11:51:23 -0800 | [diff] [blame] | 261 | mLauncher.getWorkspace().beginDragShared(v, new Point(), this, false); |
Adam Cohen | 76078c4 | 2011-06-09 15:06:52 -0700 | [diff] [blame] | 262 | |
| 263 | mCurrentDragInfo = item; |
Sunny Goyal | c3a609f | 2015-02-26 17:43:50 -0800 | [diff] [blame] | 264 | mEmptyCellRank = item.rank; |
Adam Cohen | bfbfd26 | 2011-06-13 16:55:12 -0700 | [diff] [blame] | 265 | mCurrentDragView = v; |
Adam Cohen | fc53cd2 | 2011-07-20 15:45:11 -0700 | [diff] [blame] | 266 | |
Sunny Goyal | 290800b | 2015-03-05 11:33:33 -0800 | [diff] [blame] | 267 | mContent.removeItem(mCurrentDragView); |
Adam Cohen | fc53cd2 | 2011-07-20 15:45:11 -0700 | [diff] [blame] | 268 | mInfo.remove(mCurrentDragInfo); |
Adam Cohen | 67bd9cc | 2011-07-29 14:07:04 -0700 | [diff] [blame] | 269 | mDragInProgress = true; |
Adam Cohen | 05e0f40 | 2011-08-01 12:12:49 -0700 | [diff] [blame] | 270 | mItemAddedBackToSelfViaIcon = false; |
Adam Cohen | df2cc41 | 2011-04-27 16:56:57 -0700 | [diff] [blame] | 271 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 272 | return true; |
| 273 | } |
| 274 | |
Adam Cohen | 76fc085 | 2011-06-17 13:26:23 -0700 | [diff] [blame] | 275 | public boolean isEditingName() { |
| 276 | return mIsEditingName; |
| 277 | } |
| 278 | |
| 279 | public void startEditingFolderName() { |
Adam Cohen | a65beee | 2011-06-27 21:32:23 -0700 | [diff] [blame] | 280 | mFolderName.setHint(""); |
Adam Cohen | 76fc085 | 2011-06-17 13:26:23 -0700 | [diff] [blame] | 281 | mIsEditingName = true; |
| 282 | } |
| 283 | |
| 284 | public void dismissEditingName() { |
| 285 | mInputMethodManager.hideSoftInputFromWindow(getWindowToken(), 0); |
| 286 | doneEditingFolderName(true); |
| 287 | } |
| 288 | |
| 289 | public void doneEditingFolderName(boolean commit) { |
Adam Cohen | a65beee | 2011-06-27 21:32:23 -0700 | [diff] [blame] | 290 | mFolderName.setHint(sHintText); |
Adam Cohen | 1df26a3 | 2011-08-12 16:15:23 -0700 | [diff] [blame] | 291 | // Convert to a string here to ensure that no other state associated with the text field |
| 292 | // gets saved. |
Adam Cohen | 3371da0 | 2011-10-25 21:38:29 -0700 | [diff] [blame] | 293 | String newTitle = mFolderName.getText().toString(); |
| 294 | mInfo.setTitle(newTitle); |
Adam Cohen | 76fc085 | 2011-06-17 13:26:23 -0700 | [diff] [blame] | 295 | LauncherModel.updateItemInDatabase(mLauncher, mInfo); |
Adam Cohen | ac56cff | 2011-09-28 20:45:37 -0700 | [diff] [blame] | 296 | |
Adam Cohen | 3371da0 | 2011-10-25 21:38:29 -0700 | [diff] [blame] | 297 | if (commit) { |
| 298 | sendCustomAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED, |
Michael Jurka | 8b805b1 | 2012-04-18 14:23:14 -0700 | [diff] [blame] | 299 | String.format(getContext().getString(R.string.folder_renamed), newTitle)); |
Adam Cohen | 3371da0 | 2011-10-25 21:38:29 -0700 | [diff] [blame] | 300 | } |
Adam Cohen | ac56cff | 2011-09-28 20:45:37 -0700 | [diff] [blame] | 301 | // In order to clear the focus from the text field, we set the focus on ourself. This |
| 302 | // ensures that every time the field is clicked, focus is gained, giving reliable behavior. |
| 303 | requestFocus(); |
| 304 | |
Adam Cohen | e601a43 | 2011-07-26 21:51:30 -0700 | [diff] [blame] | 305 | Selection.setSelection((Spannable) mFolderName.getText(), 0, 0); |
Adam Cohen | 76fc085 | 2011-06-17 13:26:23 -0700 | [diff] [blame] | 306 | mIsEditingName = false; |
| 307 | } |
| 308 | |
| 309 | public boolean onEditorAction(TextView v, int actionId, KeyEvent event) { |
| 310 | if (actionId == EditorInfo.IME_ACTION_DONE) { |
| 311 | dismissEditingName(); |
| 312 | return true; |
| 313 | } |
| 314 | return false; |
| 315 | } |
| 316 | |
| 317 | public View getEditTextRegion() { |
| 318 | return mFolderName; |
| 319 | } |
| 320 | |
Adam Cohen | 0c872ba | 2011-05-05 10:34:16 -0700 | [diff] [blame] | 321 | /** |
| 322 | * We need to handle touch events to prevent them from falling through to the workspace below. |
| 323 | */ |
| 324 | @Override |
| 325 | public boolean onTouchEvent(MotionEvent ev) { |
| 326 | return true; |
| 327 | } |
| 328 | |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 329 | public void setDragController(DragController dragController) { |
| 330 | mDragController = dragController; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 331 | } |
| 332 | |
Sunny Goyal | c3a609f | 2015-02-26 17:43:50 -0800 | [diff] [blame] | 333 | public void setFolderIcon(FolderIcon icon) { |
Adam Cohen | 2801caf | 2011-05-13 20:57:39 -0700 | [diff] [blame] | 334 | mFolderIcon = icon; |
| 335 | } |
| 336 | |
Adam Cohen | 3371da0 | 2011-10-25 21:38:29 -0700 | [diff] [blame] | 337 | @Override |
| 338 | public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) { |
| 339 | // When the folder gets focus, we don't want to announce the list of items. |
| 340 | return true; |
| 341 | } |
| 342 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 343 | /** |
| 344 | * @return the FolderInfo object associated with this folder |
| 345 | */ |
| 346 | FolderInfo getInfo() { |
| 347 | return mInfo; |
| 348 | } |
| 349 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 350 | void bind(FolderInfo info) { |
| 351 | mInfo = info; |
Adam Cohen | df2cc41 | 2011-04-27 16:56:57 -0700 | [diff] [blame] | 352 | ArrayList<ShortcutInfo> children = info.contents; |
Sunny Goyal | 08f7261 | 2015-01-05 13:41:43 -0800 | [diff] [blame] | 353 | Collections.sort(children, Utilities.RANK_COMPARATOR); |
Sunny Goyal | 08f7261 | 2015-01-05 13:41:43 -0800 | [diff] [blame] | 354 | |
Sunny Goyal | c3a609f | 2015-02-26 17:43:50 -0800 | [diff] [blame] | 355 | ArrayList<ShortcutInfo> overflow = mContent.bindItems(children); |
Adam Cohen | 0057bbc | 2011-08-12 18:30:51 -0700 | [diff] [blame] | 356 | |
Jason Monk | 4ff7388 | 2014-04-24 16:48:07 -0400 | [diff] [blame] | 357 | // If our folder has too many items we prune them from the list. This is an issue |
Adam Cohen | c508b2d | 2011-06-28 14:41:44 -0700 | [diff] [blame] | 358 | // when upgrading from the old Folders implementation which could contain an unlimited |
| 359 | // number of items. |
| 360 | for (ShortcutInfo item: overflow) { |
| 361 | mInfo.remove(item); |
| 362 | LauncherModel.deleteItemFromDatabase(mLauncher, item); |
| 363 | } |
| 364 | |
Sunny Goyal | c3a609f | 2015-02-26 17:43:50 -0800 | [diff] [blame] | 365 | DragLayer.LayoutParams lp = (DragLayer.LayoutParams) getLayoutParams(); |
| 366 | if (lp == null) { |
| 367 | lp = new DragLayer.LayoutParams(0, 0); |
| 368 | lp.customPosition = true; |
| 369 | setLayoutParams(lp); |
| 370 | } |
| 371 | centerAboutIcon(); |
| 372 | |
Adam Cohen | 4dbe6d9 | 2011-05-18 17:14:15 -0700 | [diff] [blame] | 373 | mItemsInvalidated = true; |
Adam Cohen | ac56cff | 2011-09-28 20:45:37 -0700 | [diff] [blame] | 374 | updateTextViewFocus(); |
Adam Cohen | a9cf38f | 2011-05-02 15:36:58 -0700 | [diff] [blame] | 375 | mInfo.addListener(this); |
Adam Cohen | 4ef610f | 2011-06-21 22:37:36 -0700 | [diff] [blame] | 376 | |
Adam Cohen | afb01ee | 2011-06-23 15:38:03 -0700 | [diff] [blame] | 377 | if (!sDefaultFolderName.contentEquals(mInfo.title)) { |
Adam Cohen | 4ef610f | 2011-06-21 22:37:36 -0700 | [diff] [blame] | 378 | mFolderName.setText(mInfo.title); |
| 379 | } else { |
| 380 | mFolderName.setText(""); |
| 381 | } |
Winson Chung | 33231f5 | 2013-12-09 16:57:45 -0800 | [diff] [blame] | 382 | |
| 383 | // In case any children didn't come across during loading, clean up the folder accordingly |
| 384 | mFolderIcon.post(new Runnable() { |
| 385 | public void run() { |
| 386 | if (getItemCount() <= 1) { |
| 387 | replaceFolderWithFinalItem(); |
| 388 | } |
| 389 | } |
| 390 | }); |
Adam Cohen | df2cc41 | 2011-04-27 16:56:57 -0700 | [diff] [blame] | 391 | } |
| 392 | |
| 393 | /** |
| 394 | * Creates a new UserFolder, inflated from R.layout.user_folder. |
| 395 | * |
| 396 | * @param context The application's context. |
| 397 | * |
| 398 | * @return A new UserFolder. |
| 399 | */ |
| 400 | static Folder fromXml(Context context) { |
Sunny Goyal | 290800b | 2015-03-05 11:33:33 -0800 | [diff] [blame] | 401 | return (Folder) LayoutInflater.from(context).inflate( |
| 402 | ALLOW_FOLDER_SCROLL ? R.layout.user_folder_scroll : R.layout.user_folder, null); |
Adam Cohen | df2cc41 | 2011-04-27 16:56:57 -0700 | [diff] [blame] | 403 | } |
| 404 | |
| 405 | /** |
| 406 | * This method is intended to make the UserFolder to be visually identical in size and position |
| 407 | * to its associated FolderIcon. This allows for a seamless transition into the expanded state. |
| 408 | */ |
| 409 | private void positionAndSizeAsIcon() { |
Adam Cohen | 3e8f811 | 2011-07-02 18:03:00 -0700 | [diff] [blame] | 410 | if (!(getParent() instanceof DragLayer)) return; |
Adam Cohen | 662b598 | 2011-12-13 17:45:21 -0800 | [diff] [blame] | 411 | setScaleX(0.8f); |
| 412 | setScaleY(0.8f); |
| 413 | setAlpha(0f); |
Adam Cohen | df2cc41 | 2011-04-27 16:56:57 -0700 | [diff] [blame] | 414 | mState = STATE_SMALL; |
| 415 | } |
| 416 | |
Adam Cohen | c4fe9ea | 2014-08-18 18:54:10 -0700 | [diff] [blame] | 417 | private void prepareReveal() { |
| 418 | setScaleX(1f); |
| 419 | setScaleY(1f); |
| 420 | setAlpha(1f); |
| 421 | mState = STATE_SMALL; |
| 422 | } |
| 423 | |
Adam Cohen | df2cc41 | 2011-04-27 16:56:57 -0700 | [diff] [blame] | 424 | public void animateOpen() { |
Adam Cohen | 8e776a6 | 2011-06-28 18:10:06 -0700 | [diff] [blame] | 425 | if (!(getParent() instanceof DragLayer)) return; |
Adam Cohen | 6441de0 | 2011-12-14 14:25:32 -0800 | [diff] [blame] | 426 | |
Sunny Goyal | 4846193 | 2015-03-09 17:41:09 -0700 | [diff] [blame] | 427 | if (ALLOW_FOLDER_SCROLL) { |
Sunny Goyal | 5d85c44 | 2015-03-10 13:14:47 -0700 | [diff] [blame] | 428 | mPagedView.completePendingPageChanges(); |
| 429 | if (!(mDragInProgress && mPagedView.mIsSorted)) { |
| 430 | // Open on the first page. |
| 431 | mPagedView.snapToPageImmediately(0); |
| 432 | } |
Sunny Goyal | 4846193 | 2015-03-09 17:41:09 -0700 | [diff] [blame] | 433 | } |
| 434 | |
Adam Cohen | c4fe9ea | 2014-08-18 18:54:10 -0700 | [diff] [blame] | 435 | Animator openFolderAnim = null; |
| 436 | final Runnable onCompleteRunnable; |
Kenny Guy | d794a3f | 2014-09-16 15:17:58 +0100 | [diff] [blame] | 437 | if (!Utilities.isLmpOrAbove()) { |
Adam Cohen | c4fe9ea | 2014-08-18 18:54:10 -0700 | [diff] [blame] | 438 | positionAndSizeAsIcon(); |
| 439 | centerAboutIcon(); |
| 440 | |
| 441 | PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1); |
| 442 | PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.0f); |
| 443 | PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.0f); |
| 444 | final ObjectAnimator oa = |
| 445 | LauncherAnimUtils.ofPropertyValuesHolder(this, alpha, scaleX, scaleY); |
| 446 | oa.setDuration(mExpandDuration); |
| 447 | openFolderAnim = oa; |
| 448 | |
| 449 | setLayerType(LAYER_TYPE_HARDWARE, null); |
| 450 | onCompleteRunnable = new Runnable() { |
| 451 | @Override |
| 452 | public void run() { |
| 453 | setLayerType(LAYER_TYPE_NONE, null); |
| 454 | } |
| 455 | }; |
| 456 | } else { |
| 457 | prepareReveal(); |
| 458 | centerAboutIcon(); |
| 459 | |
| 460 | int width = getPaddingLeft() + getPaddingRight() + mContent.getDesiredWidth(); |
| 461 | int height = getFolderHeight(); |
| 462 | |
| 463 | float transX = - 0.075f * (width / 2 - getPivotX()); |
| 464 | float transY = - 0.075f * (height / 2 - getPivotY()); |
| 465 | setTranslationX(transX); |
| 466 | setTranslationY(transY); |
| 467 | PropertyValuesHolder tx = PropertyValuesHolder.ofFloat("translationX", transX, 0); |
| 468 | PropertyValuesHolder ty = PropertyValuesHolder.ofFloat("translationY", transY, 0); |
| 469 | |
| 470 | int rx = (int) Math.max(Math.max(width - getPivotX(), 0), getPivotX()); |
| 471 | int ry = (int) Math.max(Math.max(height - getPivotY(), 0), getPivotY()); |
| 472 | float radius = (float) Math.sqrt(rx * rx + ry * ry); |
| 473 | AnimatorSet anim = LauncherAnimUtils.createAnimatorSet(); |
| 474 | Animator reveal = LauncherAnimUtils.createCircularReveal(this, (int) getPivotX(), |
| 475 | (int) getPivotY(), 0, radius); |
| 476 | reveal.setDuration(mMaterialExpandDuration); |
| 477 | reveal.setInterpolator(new LogDecelerateInterpolator(100, 0)); |
| 478 | |
Sunny Goyal | bc75335 | 2015-03-05 09:40:44 -0800 | [diff] [blame] | 479 | mContentWrapper.setAlpha(0f); |
| 480 | Animator iconsAlpha = LauncherAnimUtils.ofFloat(mContentWrapper, "alpha", 0f, 1f); |
Adam Cohen | c4fe9ea | 2014-08-18 18:54:10 -0700 | [diff] [blame] | 481 | iconsAlpha.setDuration(mMaterialExpandDuration); |
| 482 | iconsAlpha.setStartDelay(mMaterialExpandStagger); |
| 483 | iconsAlpha.setInterpolator(new AccelerateInterpolator(1.5f)); |
| 484 | |
Sunny Goyal | 290800b | 2015-03-05 11:33:33 -0800 | [diff] [blame] | 485 | mFooter.setAlpha(0f); |
| 486 | Animator textAlpha = LauncherAnimUtils.ofFloat(mFooter, "alpha", 0f, 1f); |
Adam Cohen | c4fe9ea | 2014-08-18 18:54:10 -0700 | [diff] [blame] | 487 | textAlpha.setDuration(mMaterialExpandDuration); |
| 488 | textAlpha.setStartDelay(mMaterialExpandStagger); |
| 489 | textAlpha.setInterpolator(new AccelerateInterpolator(1.5f)); |
| 490 | |
| 491 | Animator drift = LauncherAnimUtils.ofPropertyValuesHolder(this, tx, ty); |
| 492 | drift.setDuration(mMaterialExpandDuration); |
| 493 | drift.setStartDelay(mMaterialExpandStagger); |
| 494 | drift.setInterpolator(new LogDecelerateInterpolator(60, 0)); |
| 495 | |
| 496 | anim.play(drift); |
| 497 | anim.play(iconsAlpha); |
| 498 | anim.play(textAlpha); |
| 499 | anim.play(reveal); |
| 500 | |
| 501 | openFolderAnim = anim; |
| 502 | |
Sunny Goyal | bc75335 | 2015-03-05 09:40:44 -0800 | [diff] [blame] | 503 | mContentWrapper.setLayerType(LAYER_TYPE_HARDWARE, null); |
Adam Cohen | c4fe9ea | 2014-08-18 18:54:10 -0700 | [diff] [blame] | 504 | onCompleteRunnable = new Runnable() { |
| 505 | @Override |
| 506 | public void run() { |
Sunny Goyal | bc75335 | 2015-03-05 09:40:44 -0800 | [diff] [blame] | 507 | mContentWrapper.setLayerType(LAYER_TYPE_NONE, null); |
Adam Cohen | c4fe9ea | 2014-08-18 18:54:10 -0700 | [diff] [blame] | 508 | } |
| 509 | }; |
| 510 | } |
| 511 | openFolderAnim.addListener(new AnimatorListenerAdapter() { |
Adam Cohen | df2cc41 | 2011-04-27 16:56:57 -0700 | [diff] [blame] | 512 | @Override |
| 513 | public void onAnimationStart(Animator animation) { |
Adam Cohen | 3371da0 | 2011-10-25 21:38:29 -0700 | [diff] [blame] | 514 | sendCustomAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED, |
Sunny Goyal | bc75335 | 2015-03-05 09:40:44 -0800 | [diff] [blame] | 515 | mContent.getAccessibilityDescription()); |
Adam Cohen | df2cc41 | 2011-04-27 16:56:57 -0700 | [diff] [blame] | 516 | mState = STATE_ANIMATING; |
| 517 | } |
| 518 | @Override |
| 519 | public void onAnimationEnd(Animator animation) { |
Adam Cohen | 2801caf | 2011-05-13 20:57:39 -0700 | [diff] [blame] | 520 | mState = STATE_OPEN; |
Adam Cohen | c4fe9ea | 2014-08-18 18:54:10 -0700 | [diff] [blame] | 521 | |
| 522 | if (onCompleteRunnable != null) { |
| 523 | onCompleteRunnable.run(); |
| 524 | } |
Jorim Jaggi | 55bd972 | 2014-01-16 15:30:42 -0800 | [diff] [blame] | 525 | |
Sunny Goyal | bc75335 | 2015-03-05 09:40:44 -0800 | [diff] [blame] | 526 | mContent.setFocusOnFirstChild(); |
Adam Cohen | df2cc41 | 2011-04-27 16:56:57 -0700 | [diff] [blame] | 527 | } |
| 528 | }); |
Adam Cohen | c4fe9ea | 2014-08-18 18:54:10 -0700 | [diff] [blame] | 529 | openFolderAnim.start(); |
Jorim Jaggi | 55bd972 | 2014-01-16 15:30:42 -0800 | [diff] [blame] | 530 | |
| 531 | // Make sure the folder picks up the last drag move even if the finger doesn't move. |
| 532 | if (mDragController.isDragging()) { |
| 533 | mDragController.forceTouchMove(); |
| 534 | } |
Sunny Goyal | 34b6527 | 2015-03-11 16:56:52 -0700 | [diff] [blame] | 535 | |
| 536 | if (ALLOW_FOLDER_SCROLL) { |
| 537 | FolderPagedView pages = (FolderPagedView) mContent; |
| 538 | pages.verifyVisibleHighResIcons(pages.getNextPage()); |
| 539 | } |
Jorim Jaggi | 55bd972 | 2014-01-16 15:30:42 -0800 | [diff] [blame] | 540 | } |
| 541 | |
| 542 | public void beginExternalDrag(ShortcutInfo item) { |
Jorim Jaggi | 55bd972 | 2014-01-16 15:30:42 -0800 | [diff] [blame] | 543 | mCurrentDragInfo = item; |
Sunny Goyal | 5d85c44 | 2015-03-10 13:14:47 -0700 | [diff] [blame] | 544 | mEmptyCellRank = mContent.allocateRankForNewItem(item); |
Jorim Jaggi | 55bd972 | 2014-01-16 15:30:42 -0800 | [diff] [blame] | 545 | mIsExternalDrag = true; |
Jorim Jaggi | 55bd972 | 2014-01-16 15:30:42 -0800 | [diff] [blame] | 546 | mDragInProgress = true; |
Sunny Goyal | 5d85c44 | 2015-03-10 13:14:47 -0700 | [diff] [blame] | 547 | if (ALLOW_FOLDER_SCROLL && mPagedView.mIsSorted) { |
| 548 | mScrollPauseAlarm.setOnAlarmListener(null); |
| 549 | mScrollPauseAlarm.cancelAlarm(); |
| 550 | mScrollPauseAlarm.setAlarm(SORTED_STICKY_REORDER_DELAY); |
| 551 | } |
| 552 | |
Adam Cohen | df2cc41 | 2011-04-27 16:56:57 -0700 | [diff] [blame] | 553 | } |
| 554 | |
Adam Cohen | 091440a | 2015-03-18 14:16:05 -0700 | [diff] [blame] | 555 | @Thunk void sendCustomAccessibilityEvent(int type, String text) { |
Michael Jurka | 8b805b1 | 2012-04-18 14:23:14 -0700 | [diff] [blame] | 556 | AccessibilityManager accessibilityManager = (AccessibilityManager) |
| 557 | getContext().getSystemService(Context.ACCESSIBILITY_SERVICE); |
| 558 | if (accessibilityManager.isEnabled()) { |
Adam Cohen | 3371da0 | 2011-10-25 21:38:29 -0700 | [diff] [blame] | 559 | AccessibilityEvent event = AccessibilityEvent.obtain(type); |
| 560 | onInitializeAccessibilityEvent(event); |
| 561 | event.getText().add(text); |
Michael Jurka | 8b805b1 | 2012-04-18 14:23:14 -0700 | [diff] [blame] | 562 | accessibilityManager.sendAccessibilityEvent(event); |
Adam Cohen | 3371da0 | 2011-10-25 21:38:29 -0700 | [diff] [blame] | 563 | } |
| 564 | } |
| 565 | |
Adam Cohen | df2cc41 | 2011-04-27 16:56:57 -0700 | [diff] [blame] | 566 | public void animateClosed() { |
Adam Cohen | 8e776a6 | 2011-06-28 18:10:06 -0700 | [diff] [blame] | 567 | if (!(getParent() instanceof DragLayer)) return; |
Adam Cohen | 662b598 | 2011-12-13 17:45:21 -0800 | [diff] [blame] | 568 | PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0); |
| 569 | PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 0.9f); |
| 570 | PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 0.9f); |
Michael Jurka | 032e6ba | 2013-04-22 15:08:42 +0200 | [diff] [blame] | 571 | final ObjectAnimator oa = |
Michael Jurka | 2ecf995 | 2012-06-18 12:52:28 -0700 | [diff] [blame] | 572 | LauncherAnimUtils.ofPropertyValuesHolder(this, alpha, scaleX, scaleY); |
Adam Cohen | df2cc41 | 2011-04-27 16:56:57 -0700 | [diff] [blame] | 573 | |
Adam Cohen | 2801caf | 2011-05-13 20:57:39 -0700 | [diff] [blame] | 574 | oa.addListener(new AnimatorListenerAdapter() { |
Adam Cohen | df2cc41 | 2011-04-27 16:56:57 -0700 | [diff] [blame] | 575 | @Override |
| 576 | public void onAnimationEnd(Animator animation) { |
Adam Cohen | 2801caf | 2011-05-13 20:57:39 -0700 | [diff] [blame] | 577 | onCloseComplete(); |
Michael Jurka | 0121c3e | 2012-05-31 08:36:04 -0700 | [diff] [blame] | 578 | setLayerType(LAYER_TYPE_NONE, null); |
Adam Cohen | 2801caf | 2011-05-13 20:57:39 -0700 | [diff] [blame] | 579 | mState = STATE_SMALL; |
Adam Cohen | df2cc41 | 2011-04-27 16:56:57 -0700 | [diff] [blame] | 580 | } |
| 581 | @Override |
| 582 | public void onAnimationStart(Animator animation) { |
Adam Cohen | 3371da0 | 2011-10-25 21:38:29 -0700 | [diff] [blame] | 583 | sendCustomAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED, |
Michael Jurka | 8b805b1 | 2012-04-18 14:23:14 -0700 | [diff] [blame] | 584 | getContext().getString(R.string.folder_closed)); |
Adam Cohen | df2cc41 | 2011-04-27 16:56:57 -0700 | [diff] [blame] | 585 | mState = STATE_ANIMATING; |
| 586 | } |
| 587 | }); |
Adam Cohen | 2801caf | 2011-05-13 20:57:39 -0700 | [diff] [blame] | 588 | oa.setDuration(mExpandDuration); |
Michael Jurka | 0121c3e | 2012-05-31 08:36:04 -0700 | [diff] [blame] | 589 | setLayerType(LAYER_TYPE_HARDWARE, null); |
Michael Jurka | f1ad608 | 2013-03-13 12:55:46 +0100 | [diff] [blame] | 590 | oa.start(); |
Adam Cohen | df2cc41 | 2011-04-27 16:56:57 -0700 | [diff] [blame] | 591 | } |
| 592 | |
Adam Cohen | cb3382b | 2011-05-24 14:07:08 -0700 | [diff] [blame] | 593 | public boolean acceptDrop(DragObject d) { |
| 594 | final ItemInfo item = (ItemInfo) d.dragInfo; |
Adam Cohen | df2cc41 | 2011-04-27 16:56:57 -0700 | [diff] [blame] | 595 | final int itemType = item.itemType; |
Adam Cohen | 2801caf | 2011-05-13 20:57:39 -0700 | [diff] [blame] | 596 | return ((itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION || |
| 597 | itemType == LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT) && |
| 598 | !isFull()); |
Adam Cohen | df2cc41 | 2011-04-27 16:56:57 -0700 | [diff] [blame] | 599 | } |
| 600 | |
Adam Cohen | cb3382b | 2011-05-24 14:07:08 -0700 | [diff] [blame] | 601 | public void onDragEnter(DragObject d) { |
Sunny Goyal | c3a609f | 2015-02-26 17:43:50 -0800 | [diff] [blame] | 602 | mPrevTargetRank = -1; |
Adam Cohen | bfbfd26 | 2011-06-13 16:55:12 -0700 | [diff] [blame] | 603 | mOnExitAlarm.cancelAlarm(); |
Sunny Goyal | 4846193 | 2015-03-09 17:41:09 -0700 | [diff] [blame] | 604 | if (ALLOW_FOLDER_SCROLL) { |
| 605 | // Get the area offset such that the folder only closes if half the drag icon width |
| 606 | // is outside the folder area |
| 607 | mScrollAreaOffset = d.dragView.getDragRegionWidth() / 2 - d.xOffset; |
| 608 | } |
Adam Cohen | bfbfd26 | 2011-06-13 16:55:12 -0700 | [diff] [blame] | 609 | } |
| 610 | |
| 611 | OnAlarmListener mReorderAlarmListener = new OnAlarmListener() { |
| 612 | public void onAlarm(Alarm alarm) { |
Sunny Goyal | c3a609f | 2015-02-26 17:43:50 -0800 | [diff] [blame] | 613 | mContent.realTimeReorder(mEmptyCellRank, mTargetRank); |
| 614 | mEmptyCellRank = mTargetRank; |
Adam Cohen | bfbfd26 | 2011-06-13 16:55:12 -0700 | [diff] [blame] | 615 | } |
| 616 | }; |
| 617 | |
Sunny Goyal | c46bfef | 2015-01-05 12:40:08 -0800 | [diff] [blame] | 618 | @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1) |
Adam Cohen | 2374abf | 2013-04-16 14:56:57 -0700 | [diff] [blame] | 619 | public boolean isLayoutRtl() { |
| 620 | return (getLayoutDirection() == LAYOUT_DIRECTION_RTL); |
| 621 | } |
| 622 | |
Sunny Goyal | 4846193 | 2015-03-09 17:41:09 -0700 | [diff] [blame] | 623 | @Override |
Adam Cohen | cb3382b | 2011-05-24 14:07:08 -0700 | [diff] [blame] | 624 | public void onDragOver(DragObject d) { |
Sunny Goyal | 4846193 | 2015-03-09 17:41:09 -0700 | [diff] [blame] | 625 | onDragOver(d, REORDER_DELAY); |
| 626 | } |
Adam Cohen | f0f4eda | 2013-06-06 21:27:03 -0700 | [diff] [blame] | 627 | |
Sunny Goyal | 4846193 | 2015-03-09 17:41:09 -0700 | [diff] [blame] | 628 | private int getTargetRank(DragObject d, float[] recycle) { |
| 629 | recycle = d.getVisualCenter(recycle); |
| 630 | return mContent.findNearestArea( |
| 631 | (int) recycle[0] - getPaddingLeft(), (int) recycle[1] - getPaddingTop()); |
| 632 | } |
| 633 | |
Adam Cohen | 091440a | 2015-03-18 14:16:05 -0700 | [diff] [blame] | 634 | @Thunk void onDragOver(DragObject d, int reorderDelay) { |
Sunny Goyal | 4846193 | 2015-03-09 17:41:09 -0700 | [diff] [blame] | 635 | if (ALLOW_FOLDER_SCROLL && mScrollPauseAlarm.alarmPending()) { |
| 636 | return; |
| 637 | } |
| 638 | final float[] r = new float[2]; |
| 639 | mTargetRank = getTargetRank(d, r); |
| 640 | |
Sunny Goyal | c3a609f | 2015-02-26 17:43:50 -0800 | [diff] [blame] | 641 | if (mTargetRank != mPrevTargetRank) { |
Alan Viverette | 4cda5b7 | 2013-08-28 17:53:41 -0700 | [diff] [blame] | 642 | mReorderAlarm.cancelAlarm(); |
Sunny Goyal | c46bfef | 2015-01-05 12:40:08 -0800 | [diff] [blame] | 643 | mReorderAlarm.setOnAlarmListener(mReorderAlarmListener); |
| 644 | mReorderAlarm.setAlarm(REORDER_DELAY); |
Sunny Goyal | c3a609f | 2015-02-26 17:43:50 -0800 | [diff] [blame] | 645 | mPrevTargetRank = mTargetRank; |
Adam Cohen | bfbfd26 | 2011-06-13 16:55:12 -0700 | [diff] [blame] | 646 | } |
Sunny Goyal | 4846193 | 2015-03-09 17:41:09 -0700 | [diff] [blame] | 647 | |
| 648 | if (!ALLOW_FOLDER_SCROLL) { |
| 649 | return; |
| 650 | } |
| 651 | |
| 652 | float x = r[0]; |
| 653 | int currentPage = mPagedView.getNextPage(); |
Sunny Goyal | 4846193 | 2015-03-09 17:41:09 -0700 | [diff] [blame] | 654 | |
Sunny Goyal | 3b0883f | 2015-04-07 09:27:07 -0700 | [diff] [blame] | 655 | float cellOverlap = mPagedView.getCurrentCellLayout().getCellWidth() |
| 656 | * ICON_OVERSCROLL_WIDTH_FACTOR; |
| 657 | boolean isOutsideLeftEdge = x < cellOverlap; |
| 658 | boolean isOutsideRightEdge = x > (getWidth() - cellOverlap); |
Sunny Goyal | 4846193 | 2015-03-09 17:41:09 -0700 | [diff] [blame] | 659 | |
Sunny Goyal | 3b0883f | 2015-04-07 09:27:07 -0700 | [diff] [blame] | 660 | if (currentPage > 0 && (mPagedView.rtlLayout ? isOutsideRightEdge : isOutsideLeftEdge)) { |
| 661 | showScrollHint(DragController.SCROLL_LEFT, d); |
| 662 | } else if (currentPage < (mPagedView.getPageCount() - 1) |
| 663 | && (mPagedView.rtlLayout ? isOutsideLeftEdge : isOutsideRightEdge)) { |
| 664 | showScrollHint(DragController.SCROLL_RIGHT, d); |
Sunny Goyal | 4846193 | 2015-03-09 17:41:09 -0700 | [diff] [blame] | 665 | } else { |
| 666 | mOnScrollHintAlarm.cancelAlarm(); |
| 667 | if (mScrollHintDir != DragController.SCROLL_NONE) { |
| 668 | mPagedView.clearScrollHint(); |
| 669 | mScrollHintDir = DragController.SCROLL_NONE; |
| 670 | } |
| 671 | } |
Adam Cohen | df2cc41 | 2011-04-27 16:56:57 -0700 | [diff] [blame] | 672 | } |
| 673 | |
Sunny Goyal | 3b0883f | 2015-04-07 09:27:07 -0700 | [diff] [blame] | 674 | private void showScrollHint(int direction, DragObject d) { |
| 675 | // Show scroll hint on the right |
| 676 | if (mScrollHintDir != direction) { |
| 677 | mPagedView.showScrollHint(direction); |
| 678 | mScrollHintDir = direction; |
| 679 | } |
| 680 | |
| 681 | // Set alarm for when the hint is complete |
| 682 | if (!mOnScrollHintAlarm.alarmPending() || mCurrentScrollDir != direction) { |
| 683 | mCurrentScrollDir = direction; |
| 684 | mOnScrollHintAlarm.cancelAlarm(); |
| 685 | mOnScrollHintAlarm.setOnAlarmListener(new OnScrollHintListener(d)); |
| 686 | mOnScrollHintAlarm.setAlarm(SCROLL_HINT_DURATION); |
| 687 | |
| 688 | mReorderAlarm.cancelAlarm(); |
| 689 | mTargetRank = mEmptyCellRank; |
| 690 | } |
| 691 | } |
| 692 | |
Adam Cohen | bfbfd26 | 2011-06-13 16:55:12 -0700 | [diff] [blame] | 693 | OnAlarmListener mOnExitAlarmListener = new OnAlarmListener() { |
| 694 | public void onAlarm(Alarm alarm) { |
Adam Cohen | 3e8f811 | 2011-07-02 18:03:00 -0700 | [diff] [blame] | 695 | completeDragExit(); |
Adam Cohen | bfbfd26 | 2011-06-13 16:55:12 -0700 | [diff] [blame] | 696 | } |
| 697 | }; |
| 698 | |
Adam Cohen | 95bb800 | 2011-07-03 23:40:28 -0700 | [diff] [blame] | 699 | public void completeDragExit() { |
Adam Cohen | 3e8f811 | 2011-07-02 18:03:00 -0700 | [diff] [blame] | 700 | mLauncher.closeFolder(); |
| 701 | mCurrentDragInfo = null; |
| 702 | mCurrentDragView = null; |
| 703 | mSuppressOnAdd = false; |
| 704 | mRearrangeOnClose = true; |
Jorim Jaggi | 55bd972 | 2014-01-16 15:30:42 -0800 | [diff] [blame] | 705 | mIsExternalDrag = false; |
Adam Cohen | 3e8f811 | 2011-07-02 18:03:00 -0700 | [diff] [blame] | 706 | } |
| 707 | |
Adam Cohen | cb3382b | 2011-05-24 14:07:08 -0700 | [diff] [blame] | 708 | public void onDragExit(DragObject d) { |
Mindy DelliCarpini | 53b8d07 | 2013-07-03 08:23:13 -0700 | [diff] [blame] | 709 | // We only close the folder if this is a true drag exit, ie. not because |
| 710 | // a drop has occurred above the folder. |
Adam Cohen | bfbfd26 | 2011-06-13 16:55:12 -0700 | [diff] [blame] | 711 | if (!d.dragComplete) { |
| 712 | mOnExitAlarm.setOnAlarmListener(mOnExitAlarmListener); |
| 713 | mOnExitAlarm.setAlarm(ON_EXIT_CLOSE_DELAY); |
| 714 | } |
| 715 | mReorderAlarm.cancelAlarm(); |
Sunny Goyal | 4846193 | 2015-03-09 17:41:09 -0700 | [diff] [blame] | 716 | |
| 717 | if (ALLOW_FOLDER_SCROLL) { |
| 718 | mOnScrollHintAlarm.cancelAlarm(); |
| 719 | mScrollPauseAlarm.cancelAlarm(); |
| 720 | if (mScrollHintDir != DragController.SCROLL_NONE) { |
| 721 | mPagedView.clearScrollHint(); |
| 722 | mScrollHintDir = DragController.SCROLL_NONE; |
| 723 | } |
| 724 | } |
Adam Cohen | 2801caf | 2011-05-13 20:57:39 -0700 | [diff] [blame] | 725 | } |
| 726 | |
Michael Jurka | 1e2f465 | 2013-07-08 18:03:46 -0700 | [diff] [blame] | 727 | public void onDropCompleted(final View target, final DragObject d, |
| 728 | final boolean isFlingToDelete, final boolean success) { |
| 729 | if (mDeferDropAfterUninstall) { |
Michael Jurka | f300758 | 2013-08-21 14:33:57 +0200 | [diff] [blame] | 730 | Log.d(TAG, "Deferred handling drop because waiting for uninstall."); |
Michael Jurka | 1e2f465 | 2013-07-08 18:03:46 -0700 | [diff] [blame] | 731 | mDeferredAction = new Runnable() { |
| 732 | public void run() { |
| 733 | onDropCompleted(target, d, isFlingToDelete, success); |
| 734 | mDeferredAction = null; |
| 735 | } |
| 736 | }; |
| 737 | return; |
| 738 | } |
| 739 | |
| 740 | boolean beingCalledAfterUninstall = mDeferredAction != null; |
| 741 | boolean successfulDrop = |
| 742 | success && (!beingCalledAfterUninstall || mUninstallSuccessful); |
Winson Chung | 5f8afe6 | 2013-08-12 16:19:28 -0700 | [diff] [blame] | 743 | |
Michael Jurka | 1e2f465 | 2013-07-08 18:03:46 -0700 | [diff] [blame] | 744 | if (successfulDrop) { |
Jorim Jaggi | 55bd972 | 2014-01-16 15:30:42 -0800 | [diff] [blame] | 745 | if (mDeleteFolderOnDropCompleted && !mItemAddedBackToSelfViaIcon && target != this) { |
Adam Cohen | 67bd9cc | 2011-07-29 14:07:04 -0700 | [diff] [blame] | 746 | replaceFolderWithFinalItem(); |
| 747 | } |
| 748 | } else { |
Sunny Goyal | c3a609f | 2015-02-26 17:43:50 -0800 | [diff] [blame] | 749 | rearrangeChildren(); |
Adam Cohen | 67bd9cc | 2011-07-29 14:07:04 -0700 | [diff] [blame] | 750 | // The drag failed, we need to return the item to the folder |
| 751 | mFolderIcon.onDrop(d); |
Adam Cohen | 67bd9cc | 2011-07-29 14:07:04 -0700 | [diff] [blame] | 752 | } |
| 753 | |
| 754 | if (target != this) { |
| 755 | if (mOnExitAlarm.alarmPending()) { |
| 756 | mOnExitAlarm.cancelAlarm(); |
Michael Jurka | 5455425 | 2013-08-01 12:52:23 +0200 | [diff] [blame] | 757 | if (!successfulDrop) { |
Adam Cohen | 7a8b82b | 2013-05-29 18:41:50 -0700 | [diff] [blame] | 758 | mSuppressFolderDeletion = true; |
| 759 | } |
Sunny Goyal | 5d85c44 | 2015-03-10 13:14:47 -0700 | [diff] [blame] | 760 | mScrollPauseAlarm.cancelAlarm(); |
Adam Cohen | 67bd9cc | 2011-07-29 14:07:04 -0700 | [diff] [blame] | 761 | completeDragExit(); |
| 762 | } |
Adam Cohen | 9c58d82 | 2013-12-13 17:18:10 -0800 | [diff] [blame] | 763 | } |
| 764 | |
Adam Cohen | 67bd9cc | 2011-07-29 14:07:04 -0700 | [diff] [blame] | 765 | mDeleteFolderOnDropCompleted = false; |
| 766 | mDragInProgress = false; |
Adam Cohen | 05e0f40 | 2011-08-01 12:12:49 -0700 | [diff] [blame] | 767 | mItemAddedBackToSelfViaIcon = false; |
Adam Cohen | bfbfd26 | 2011-06-13 16:55:12 -0700 | [diff] [blame] | 768 | mCurrentDragInfo = null; |
| 769 | mCurrentDragView = null; |
| 770 | mSuppressOnAdd = false; |
Adam Cohen | 4045eb7 | 2011-10-06 11:44:26 -0700 | [diff] [blame] | 771 | |
| 772 | // Reordering may have occured, and we need to save the new item locations. We do this once |
| 773 | // at the end to prevent unnecessary database operations. |
Adam Cohen | f0f4eda | 2013-06-06 21:27:03 -0700 | [diff] [blame] | 774 | updateItemLocationsInDatabaseBatch(); |
Adam Cohen | 4045eb7 | 2011-10-06 11:44:26 -0700 | [diff] [blame] | 775 | } |
| 776 | |
Michael Jurka | 1e2f465 | 2013-07-08 18:03:46 -0700 | [diff] [blame] | 777 | public void deferCompleteDropAfterUninstallActivity() { |
| 778 | mDeferDropAfterUninstall = true; |
| 779 | } |
| 780 | |
| 781 | public void onUninstallActivityReturned(boolean success) { |
| 782 | mDeferDropAfterUninstall = false; |
| 783 | mUninstallSuccessful = success; |
| 784 | if (mDeferredAction != null) { |
| 785 | mDeferredAction.run(); |
| 786 | } |
| 787 | } |
| 788 | |
Winson Chung | a48487a | 2012-03-20 16:19:37 -0700 | [diff] [blame] | 789 | @Override |
Winson Chung | eeb5bbc | 2013-11-13 15:47:05 -0800 | [diff] [blame] | 790 | public float getIntrinsicIconScaleFactor() { |
| 791 | return 1f; |
| 792 | } |
| 793 | |
| 794 | @Override |
Winson Chung | 043f2af | 2012-03-01 16:09:54 -0800 | [diff] [blame] | 795 | public boolean supportsFlingToDelete() { |
| 796 | return true; |
| 797 | } |
| 798 | |
Mathew Inwood | 1eeb3fc | 2013-11-25 17:01:34 +0000 | [diff] [blame] | 799 | @Override |
| 800 | public boolean supportsAppInfoDropTarget() { |
| 801 | return false; |
| 802 | } |
| 803 | |
| 804 | @Override |
| 805 | public boolean supportsDeleteDropTarget() { |
| 806 | return true; |
| 807 | } |
| 808 | |
Winson Chung | a48487a | 2012-03-20 16:19:37 -0700 | [diff] [blame] | 809 | public void onFlingToDelete(DragObject d, int x, int y, PointF vec) { |
| 810 | // Do nothing |
| 811 | } |
| 812 | |
| 813 | @Override |
| 814 | public void onFlingToDeleteCompleted() { |
| 815 | // Do nothing |
| 816 | } |
| 817 | |
Adam Cohen | f0f4eda | 2013-06-06 21:27:03 -0700 | [diff] [blame] | 818 | private void updateItemLocationsInDatabaseBatch() { |
| 819 | ArrayList<View> list = getItemsInReadingOrder(); |
| 820 | ArrayList<ItemInfo> items = new ArrayList<ItemInfo>(); |
| 821 | for (int i = 0; i < list.size(); i++) { |
| 822 | View v = list.get(i); |
| 823 | ItemInfo info = (ItemInfo) v.getTag(); |
Sunny Goyal | 08f7261 | 2015-01-05 13:41:43 -0800 | [diff] [blame] | 824 | info.rank = i; |
Adam Cohen | f0f4eda | 2013-06-06 21:27:03 -0700 | [diff] [blame] | 825 | items.add(info); |
| 826 | } |
| 827 | |
| 828 | LauncherModel.moveItemsInDatabase(mLauncher, items, mInfo.id, 0); |
| 829 | } |
| 830 | |
Adam Cohen | e25af79 | 2013-06-06 23:08:25 -0700 | [diff] [blame] | 831 | public void addItemLocationsInDatabase() { |
| 832 | ArrayList<View> list = getItemsInReadingOrder(); |
| 833 | for (int i = 0; i < list.size(); i++) { |
| 834 | View v = list.get(i); |
| 835 | ItemInfo info = (ItemInfo) v.getTag(); |
| 836 | LauncherModel.addItemToDatabase(mLauncher, info, mInfo.id, 0, |
Sunny Goyal | 1d4a2df | 2015-03-30 11:11:46 -0700 | [diff] [blame] | 837 | info.cellX, info.cellY); |
Adam Cohen | e25af79 | 2013-06-06 23:08:25 -0700 | [diff] [blame] | 838 | } |
| 839 | } |
| 840 | |
Adam Cohen | 67bd9cc | 2011-07-29 14:07:04 -0700 | [diff] [blame] | 841 | public void notifyDrop() { |
| 842 | if (mDragInProgress) { |
Adam Cohen | 05e0f40 | 2011-08-01 12:12:49 -0700 | [diff] [blame] | 843 | mItemAddedBackToSelfViaIcon = true; |
Adam Cohen | 76078c4 | 2011-06-09 15:06:52 -0700 | [diff] [blame] | 844 | } |
Adam Cohen | df2cc41 | 2011-04-27 16:56:57 -0700 | [diff] [blame] | 845 | } |
| 846 | |
| 847 | public boolean isDropEnabled() { |
| 848 | return true; |
| 849 | } |
| 850 | |
Adam Cohen | 2801caf | 2011-05-13 20:57:39 -0700 | [diff] [blame] | 851 | public boolean isFull() { |
Sunny Goyal | c3a609f | 2015-02-26 17:43:50 -0800 | [diff] [blame] | 852 | return mContent.isFull(); |
Adam Cohen | 2801caf | 2011-05-13 20:57:39 -0700 | [diff] [blame] | 853 | } |
| 854 | |
| 855 | private void centerAboutIcon() { |
Adam Cohen | 8e776a6 | 2011-06-28 18:10:06 -0700 | [diff] [blame] | 856 | DragLayer.LayoutParams lp = (DragLayer.LayoutParams) getLayoutParams(); |
Adam Cohen | 2801caf | 2011-05-13 20:57:39 -0700 | [diff] [blame] | 857 | |
Winson Chung | 892c74d | 2013-08-22 16:15:50 -0700 | [diff] [blame] | 858 | DragLayer parent = (DragLayer) mLauncher.findViewById(R.id.drag_layer); |
Adam Cohen | 2801caf | 2011-05-13 20:57:39 -0700 | [diff] [blame] | 859 | int width = getPaddingLeft() + getPaddingRight() + mContent.getDesiredWidth(); |
Adam Cohen | f0f4eda | 2013-06-06 21:27:03 -0700 | [diff] [blame] | 860 | int height = getFolderHeight(); |
Adam Cohen | 2801caf | 2011-05-13 20:57:39 -0700 | [diff] [blame] | 861 | |
Sunny Goyal | c3a609f | 2015-02-26 17:43:50 -0800 | [diff] [blame] | 862 | float scale = parent.getDescendantRectRelativeToSelf(mFolderIcon, sTempRect); |
Adam Cohen | 8e776a6 | 2011-06-28 18:10:06 -0700 | [diff] [blame] | 863 | |
Winson Chung | af40f20 | 2013-09-18 18:26:31 -0700 | [diff] [blame] | 864 | LauncherAppState app = LauncherAppState.getInstance(); |
| 865 | DeviceProfile grid = app.getDynamicGrid().getDeviceProfile(); |
| 866 | |
Sunny Goyal | c3a609f | 2015-02-26 17:43:50 -0800 | [diff] [blame] | 867 | int centerX = (int) (sTempRect.left + sTempRect.width() * scale / 2); |
| 868 | int centerY = (int) (sTempRect.top + sTempRect.height() * scale / 2); |
Adam Cohen | 2801caf | 2011-05-13 20:57:39 -0700 | [diff] [blame] | 869 | int centeredLeft = centerX - width / 2; |
| 870 | int centeredTop = centerY - height / 2; |
Winson Chung | 3057b1c | 2013-10-10 17:35:35 -0700 | [diff] [blame] | 871 | int currentPage = mLauncher.getWorkspace().getNextPage(); |
Adam Cohen | 7cc1bc4 | 2011-12-13 21:28:43 -0800 | [diff] [blame] | 872 | // In case the workspace is scrolling, we need to use the final scroll to compute |
| 873 | // the folders bounds. |
| 874 | mLauncher.getWorkspace().setFinalScrollForPageChange(currentPage); |
Adam Cohen | 35e7e64 | 2011-07-17 14:47:18 -0700 | [diff] [blame] | 875 | // We first fetch the currently visible CellLayoutChildren |
Adam Cohen | 7cc1bc4 | 2011-12-13 21:28:43 -0800 | [diff] [blame] | 876 | CellLayout currentLayout = (CellLayout) mLauncher.getWorkspace().getChildAt(currentPage); |
Michael Jurka | a52570f | 2012-03-20 03:18:20 -0700 | [diff] [blame] | 877 | ShortcutAndWidgetContainer boundingLayout = currentLayout.getShortcutsAndWidgets(); |
Adam Cohen | 35e7e64 | 2011-07-17 14:47:18 -0700 | [diff] [blame] | 878 | Rect bounds = new Rect(); |
| 879 | parent.getDescendantRectRelativeToSelf(boundingLayout, bounds); |
Adam Cohen | 7cc1bc4 | 2011-12-13 21:28:43 -0800 | [diff] [blame] | 880 | // We reset the workspaces scroll |
| 881 | mLauncher.getWorkspace().resetFinalScrollForPageChange(currentPage); |
Adam Cohen | 2801caf | 2011-05-13 20:57:39 -0700 | [diff] [blame] | 882 | |
Adam Cohen | 35e7e64 | 2011-07-17 14:47:18 -0700 | [diff] [blame] | 883 | // We need to bound the folder to the currently visible CellLayoutChildren |
| 884 | int left = Math.min(Math.max(bounds.left, centeredLeft), |
| 885 | bounds.left + bounds.width() - width); |
| 886 | int top = Math.min(Math.max(bounds.top, centeredTop), |
| 887 | bounds.top + bounds.height() - height); |
Winson Chung | af40f20 | 2013-09-18 18:26:31 -0700 | [diff] [blame] | 888 | if (grid.isPhone() && (grid.availableWidthPx - width) < grid.iconSizePx) { |
| 889 | // Center the folder if it is full (on phones only) |
| 890 | left = (grid.availableWidthPx - width) / 2; |
| 891 | } else if (width >= bounds.width()) { |
| 892 | // If the folder doesn't fit within the bounds, center it about the desired bounds |
Adam Cohen | 35e7e64 | 2011-07-17 14:47:18 -0700 | [diff] [blame] | 893 | left = bounds.left + (bounds.width() - width) / 2; |
Adam Cohen | 0e4857c | 2011-06-30 17:05:14 -0700 | [diff] [blame] | 894 | } |
Adam Cohen | 35e7e64 | 2011-07-17 14:47:18 -0700 | [diff] [blame] | 895 | if (height >= bounds.height()) { |
| 896 | top = bounds.top + (bounds.height() - height) / 2; |
Adam Cohen | 0e4857c | 2011-06-30 17:05:14 -0700 | [diff] [blame] | 897 | } |
Adam Cohen | 2801caf | 2011-05-13 20:57:39 -0700 | [diff] [blame] | 898 | |
| 899 | int folderPivotX = width / 2 + (centeredLeft - left); |
| 900 | int folderPivotY = height / 2 + (centeredTop - top); |
| 901 | setPivotX(folderPivotX); |
| 902 | setPivotY(folderPivotY); |
Adam Cohen | 268c475 | 2012-06-06 17:47:33 -0700 | [diff] [blame] | 903 | mFolderIconPivotX = (int) (mFolderIcon.getMeasuredWidth() * |
Adam Cohen | 2801caf | 2011-05-13 20:57:39 -0700 | [diff] [blame] | 904 | (1.0f * folderPivotX / width)); |
Adam Cohen | 268c475 | 2012-06-06 17:47:33 -0700 | [diff] [blame] | 905 | mFolderIconPivotY = (int) (mFolderIcon.getMeasuredHeight() * |
Adam Cohen | 2801caf | 2011-05-13 20:57:39 -0700 | [diff] [blame] | 906 | (1.0f * folderPivotY / height)); |
Adam Cohen | 3bf84d3 | 2012-05-07 20:17:14 -0700 | [diff] [blame] | 907 | |
Adam Cohen | 662b598 | 2011-12-13 17:45:21 -0800 | [diff] [blame] | 908 | lp.width = width; |
| 909 | lp.height = height; |
| 910 | lp.x = left; |
| 911 | lp.y = top; |
Adam Cohen | 2801caf | 2011-05-13 20:57:39 -0700 | [diff] [blame] | 912 | } |
| 913 | |
Adam Cohen | 268c475 | 2012-06-06 17:47:33 -0700 | [diff] [blame] | 914 | float getPivotXForIconAnimation() { |
| 915 | return mFolderIconPivotX; |
| 916 | } |
| 917 | float getPivotYForIconAnimation() { |
| 918 | return mFolderIconPivotY; |
| 919 | } |
| 920 | |
Winson Chung | 892c74d | 2013-08-22 16:15:50 -0700 | [diff] [blame] | 921 | private int getContentAreaHeight() { |
| 922 | LauncherAppState app = LauncherAppState.getInstance(); |
| 923 | DeviceProfile grid = app.getDynamicGrid().getDeviceProfile(); |
| 924 | Rect workspacePadding = grid.getWorkspacePadding(grid.isLandscape ? |
| 925 | CellLayout.LANDSCAPE : CellLayout.PORTRAIT); |
| 926 | int maxContentAreaHeight = grid.availableHeightPx - |
Winson Chung | 892c74d | 2013-08-22 16:15:50 -0700 | [diff] [blame] | 927 | workspacePadding.top - workspacePadding.bottom - |
Sunny Goyal | 290800b | 2015-03-05 11:33:33 -0800 | [diff] [blame] | 928 | mFooterHeight; |
Adam Cohen | 1960ea4 | 2013-11-12 11:33:14 +0000 | [diff] [blame] | 929 | int height = Math.min(maxContentAreaHeight, |
Winson Chung | 892c74d | 2013-08-22 16:15:50 -0700 | [diff] [blame] | 930 | mContent.getDesiredHeight()); |
Adam Cohen | 1960ea4 | 2013-11-12 11:33:14 +0000 | [diff] [blame] | 931 | return Math.max(height, MIN_CONTENT_DIMEN); |
| 932 | } |
| 933 | |
| 934 | private int getContentAreaWidth() { |
| 935 | return Math.max(mContent.getDesiredWidth(), MIN_CONTENT_DIMEN); |
Winson Chung | 892c74d | 2013-08-22 16:15:50 -0700 | [diff] [blame] | 936 | } |
| 937 | |
Adam Cohen | f0f4eda | 2013-06-06 21:27:03 -0700 | [diff] [blame] | 938 | private int getFolderHeight() { |
Sunny Goyal | c3a609f | 2015-02-26 17:43:50 -0800 | [diff] [blame] | 939 | return getFolderHeight(getContentAreaHeight()); |
| 940 | } |
| 941 | |
| 942 | private int getFolderHeight(int contentAreaHeight) { |
Sunny Goyal | 290800b | 2015-03-05 11:33:33 -0800 | [diff] [blame] | 943 | return getPaddingTop() + getPaddingBottom() + contentAreaHeight + mFooterHeight; |
Adam Cohen | f0f4eda | 2013-06-06 21:27:03 -0700 | [diff] [blame] | 944 | } |
Adam Cohen | 234c4cd | 2011-07-17 21:03:04 -0700 | [diff] [blame] | 945 | |
Adam Cohen | f0f4eda | 2013-06-06 21:27:03 -0700 | [diff] [blame] | 946 | protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { |
Sunny Goyal | c3a609f | 2015-02-26 17:43:50 -0800 | [diff] [blame] | 947 | int contentWidth = getContentAreaWidth(); |
| 948 | int contentHeight = getContentAreaHeight(); |
Adam Cohen | 1960ea4 | 2013-11-12 11:33:14 +0000 | [diff] [blame] | 949 | |
Sunny Goyal | c3a609f | 2015-02-26 17:43:50 -0800 | [diff] [blame] | 950 | int contentAreaWidthSpec = MeasureSpec.makeMeasureSpec(contentWidth, MeasureSpec.EXACTLY); |
| 951 | int contentAreaHeightSpec = MeasureSpec.makeMeasureSpec(contentHeight, MeasureSpec.EXACTLY); |
| 952 | |
| 953 | mContent.setFixedSize(contentWidth, contentHeight); |
| 954 | mContentWrapper.measure(contentAreaWidthSpec, contentAreaHeightSpec); |
Sunny Goyal | 290800b | 2015-03-05 11:33:33 -0800 | [diff] [blame] | 955 | mFooter.measure(contentAreaWidthSpec, |
| 956 | MeasureSpec.makeMeasureSpec(mFooterHeight, MeasureSpec.EXACTLY)); |
Sunny Goyal | c3a609f | 2015-02-26 17:43:50 -0800 | [diff] [blame] | 957 | |
| 958 | int folderWidth = getPaddingLeft() + getPaddingRight() + contentWidth; |
| 959 | int folderHeight = getFolderHeight(contentHeight); |
| 960 | setMeasuredDimension(folderWidth, folderHeight); |
Adam Cohen | 234c4cd | 2011-07-17 21:03:04 -0700 | [diff] [blame] | 961 | } |
| 962 | |
Sunny Goyal | c3a609f | 2015-02-26 17:43:50 -0800 | [diff] [blame] | 963 | /** |
| 964 | * Rearranges the children based on their rank. |
| 965 | */ |
| 966 | public void rearrangeChildren() { |
| 967 | rearrangeChildren(-1); |
| 968 | } |
Adam Cohen | 2801caf | 2011-05-13 20:57:39 -0700 | [diff] [blame] | 969 | |
Sunny Goyal | c3a609f | 2015-02-26 17:43:50 -0800 | [diff] [blame] | 970 | /** |
| 971 | * Rearranges the children based on their rank. |
| 972 | * @param itemCount if greater than the total children count, empty spaces are left at the end, |
| 973 | * otherwise it is ignored. |
| 974 | */ |
| 975 | public void rearrangeChildren(int itemCount) { |
| 976 | ArrayList<View> views = getItemsInReadingOrder(); |
| 977 | mContent.arrangeChildren(views, Math.max(itemCount, views.size())); |
Adam Cohen | 7c69321 | 2011-05-18 15:26:57 -0700 | [diff] [blame] | 978 | mItemsInvalidated = true; |
Adam Cohen | 2801caf | 2011-05-13 20:57:39 -0700 | [diff] [blame] | 979 | } |
| 980 | |
Sunny Goyal | c491835 | 2015-03-10 18:15:48 -0700 | [diff] [blame] | 981 | // TODO remove this once GSA code fix is submitted |
| 982 | public ViewGroup getContent() { |
| 983 | return (ViewGroup) mContent; |
| 984 | } |
| 985 | |
Adam Cohen | a9cf38f | 2011-05-02 15:36:58 -0700 | [diff] [blame] | 986 | public int getItemCount() { |
Sunny Goyal | c3a609f | 2015-02-26 17:43:50 -0800 | [diff] [blame] | 987 | return mContent.getItemCount(); |
Adam Cohen | a9cf38f | 2011-05-02 15:36:58 -0700 | [diff] [blame] | 988 | } |
| 989 | |
Adam Cohen | 091440a | 2015-03-18 14:16:05 -0700 | [diff] [blame] | 990 | @Thunk void onCloseComplete() { |
Adam Cohen | 05e0f40 | 2011-08-01 12:12:49 -0700 | [diff] [blame] | 991 | DragLayer parent = (DragLayer) getParent(); |
Michael Jurka | 5649c28 | 2012-06-18 10:33:21 -0700 | [diff] [blame] | 992 | if (parent != null) { |
| 993 | parent.removeView(this); |
| 994 | } |
Adam Cohen | 4554ee1 | 2011-08-03 16:13:21 -0700 | [diff] [blame] | 995 | mDragController.removeDropTarget((DropTarget) this); |
Adam Cohen | 05e0f40 | 2011-08-01 12:12:49 -0700 | [diff] [blame] | 996 | clearFocus(); |
Adam Cohen | ac56cff | 2011-09-28 20:45:37 -0700 | [diff] [blame] | 997 | mFolderIcon.requestFocus(); |
Adam Cohen | 05e0f40 | 2011-08-01 12:12:49 -0700 | [diff] [blame] | 998 | |
Adam Cohen | 2801caf | 2011-05-13 20:57:39 -0700 | [diff] [blame] | 999 | if (mRearrangeOnClose) { |
Sunny Goyal | c3a609f | 2015-02-26 17:43:50 -0800 | [diff] [blame] | 1000 | rearrangeChildren(); |
Adam Cohen | 2801caf | 2011-05-13 20:57:39 -0700 | [diff] [blame] | 1001 | mRearrangeOnClose = false; |
| 1002 | } |
Adam Cohen | afb01ee | 2011-06-23 15:38:03 -0700 | [diff] [blame] | 1003 | if (getItemCount() <= 1) { |
Adam Cohen | 67bd9cc | 2011-07-29 14:07:04 -0700 | [diff] [blame] | 1004 | if (!mDragInProgress && !mSuppressFolderDeletion) { |
| 1005 | replaceFolderWithFinalItem(); |
| 1006 | } else if (mDragInProgress) { |
| 1007 | mDeleteFolderOnDropCompleted = true; |
| 1008 | } |
Adam Cohen | afb01ee | 2011-06-23 15:38:03 -0700 | [diff] [blame] | 1009 | } |
Adam Cohen | 67bd9cc | 2011-07-29 14:07:04 -0700 | [diff] [blame] | 1010 | mSuppressFolderDeletion = false; |
Adam Cohen | afb01ee | 2011-06-23 15:38:03 -0700 | [diff] [blame] | 1011 | } |
| 1012 | |
Adam Cohen | 091440a | 2015-03-18 14:16:05 -0700 | [diff] [blame] | 1013 | @Thunk void replaceFolderWithFinalItem() { |
Adam Cohen | afb01ee | 2011-06-23 15:38:03 -0700 | [diff] [blame] | 1014 | // Add the last remaining child to the workspace in place of the folder |
Adam Cohen | fb91f30 | 2012-06-11 15:45:18 -0700 | [diff] [blame] | 1015 | Runnable onCompleteRunnable = new Runnable() { |
| 1016 | @Override |
| 1017 | public void run() { |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 1018 | CellLayout cellLayout = mLauncher.getCellLayout(mInfo.container, mInfo.screenId); |
Adam Cohen | afb01ee | 2011-06-23 15:38:03 -0700 | [diff] [blame] | 1019 | |
Winson Chung | 33231f5 | 2013-12-09 16:57:45 -0800 | [diff] [blame] | 1020 | View child = null; |
Adam Cohen | fb91f30 | 2012-06-11 15:45:18 -0700 | [diff] [blame] | 1021 | // Move the item from the folder to the workspace, in the position of the folder |
| 1022 | if (getItemCount() == 1) { |
| 1023 | ShortcutInfo finalItem = mInfo.contents.get(0); |
Adam Cohen | c5e63f3 | 2012-07-12 16:16:57 -0700 | [diff] [blame] | 1024 | child = mLauncher.createShortcut(R.layout.application, cellLayout, |
Adam Cohen | fb91f30 | 2012-06-11 15:45:18 -0700 | [diff] [blame] | 1025 | finalItem); |
| 1026 | LauncherModel.addOrMoveItemInDatabase(mLauncher, finalItem, mInfo.container, |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 1027 | mInfo.screenId, mInfo.cellX, mInfo.cellY); |
Adam Cohen | fb91f30 | 2012-06-11 15:45:18 -0700 | [diff] [blame] | 1028 | } |
Adam Cohen | 487f7dd | 2012-06-28 18:12:10 -0700 | [diff] [blame] | 1029 | if (getItemCount() <= 1) { |
| 1030 | // Remove the folder |
| 1031 | LauncherModel.deleteItemFromDatabase(mLauncher, mInfo); |
Dan Sandler | 0eb687f | 2014-01-10 13:24:55 -0500 | [diff] [blame] | 1032 | if (cellLayout != null) { |
| 1033 | // b/12446428 -- sometimes the cell layout has already gone away? |
| 1034 | cellLayout.removeView(mFolderIcon); |
| 1035 | } |
Adam Cohen | 487f7dd | 2012-06-28 18:12:10 -0700 | [diff] [blame] | 1036 | if (mFolderIcon instanceof DropTarget) { |
| 1037 | mDragController.removeDropTarget((DropTarget) mFolderIcon); |
| 1038 | } |
| 1039 | mLauncher.removeFolder(mInfo); |
| 1040 | } |
Adam Cohen | c5e63f3 | 2012-07-12 16:16:57 -0700 | [diff] [blame] | 1041 | // We add the child after removing the folder to prevent both from existing at |
Winson Chung | 0e6a713 | 2013-08-23 12:55:10 -0700 | [diff] [blame] | 1042 | // the same time in the CellLayout. We need to add the new item with addInScreenFromBind() |
| 1043 | // to ensure that hotseat items are placed correctly. |
Adam Cohen | c5e63f3 | 2012-07-12 16:16:57 -0700 | [diff] [blame] | 1044 | if (child != null) { |
Winson Chung | 0e6a713 | 2013-08-23 12:55:10 -0700 | [diff] [blame] | 1045 | mLauncher.getWorkspace().addInScreenFromBind(child, mInfo.container, mInfo.screenId, |
Adam Cohen | c5e63f3 | 2012-07-12 16:16:57 -0700 | [diff] [blame] | 1046 | mInfo.cellX, mInfo.cellY, mInfo.spanX, mInfo.spanY); |
| 1047 | } |
Adam Cohen | fb91f30 | 2012-06-11 15:45:18 -0700 | [diff] [blame] | 1048 | } |
| 1049 | }; |
Sunny Goyal | bc75335 | 2015-03-05 09:40:44 -0800 | [diff] [blame] | 1050 | View finalChild = mContent.getLastItem(); |
Adam Cohen | fb91f30 | 2012-06-11 15:45:18 -0700 | [diff] [blame] | 1051 | if (finalChild != null) { |
| 1052 | mFolderIcon.performDestroyAnimation(finalChild, onCompleteRunnable); |
Winson Chung | 33231f5 | 2013-12-09 16:57:45 -0800 | [diff] [blame] | 1053 | } else { |
| 1054 | onCompleteRunnable.run(); |
Adam Cohen | afb01ee | 2011-06-23 15:38:03 -0700 | [diff] [blame] | 1055 | } |
Adam Cohen | fb91f30 | 2012-06-11 15:45:18 -0700 | [diff] [blame] | 1056 | mDestroyed = true; |
| 1057 | } |
| 1058 | |
| 1059 | boolean isDestroyed() { |
| 1060 | return mDestroyed; |
Adam Cohen | 2801caf | 2011-05-13 20:57:39 -0700 | [diff] [blame] | 1061 | } |
| 1062 | |
Adam Cohen | ac56cff | 2011-09-28 20:45:37 -0700 | [diff] [blame] | 1063 | // This method keeps track of the last item in the folder for the purposes |
| 1064 | // of keyboard focus |
Sunny Goyal | 290800b | 2015-03-05 11:33:33 -0800 | [diff] [blame] | 1065 | public void updateTextViewFocus() { |
Sunny Goyal | bc75335 | 2015-03-05 09:40:44 -0800 | [diff] [blame] | 1066 | View lastChild = mContent.getLastItem(); |
Adam Cohen | ac56cff | 2011-09-28 20:45:37 -0700 | [diff] [blame] | 1067 | if (lastChild != null) { |
| 1068 | mFolderName.setNextFocusDownId(lastChild.getId()); |
| 1069 | mFolderName.setNextFocusRightId(lastChild.getId()); |
| 1070 | mFolderName.setNextFocusLeftId(lastChild.getId()); |
| 1071 | mFolderName.setNextFocusUpId(lastChild.getId()); |
| 1072 | } |
| 1073 | } |
| 1074 | |
Adam Cohen | bfbfd26 | 2011-06-13 16:55:12 -0700 | [diff] [blame] | 1075 | public void onDrop(DragObject d) { |
Jorim Jaggi | 55bd972 | 2014-01-16 15:30:42 -0800 | [diff] [blame] | 1076 | Runnable cleanUpRunnable = null; |
| 1077 | |
Adam Cohen | 689ff16 | 2014-05-08 17:27:56 -0700 | [diff] [blame] | 1078 | // If we are coming from All Apps space, we defer removing the extra empty screen |
| 1079 | // until the folder closes |
Jorim Jaggi | 55bd972 | 2014-01-16 15:30:42 -0800 | [diff] [blame] | 1080 | if (d.dragSource != mLauncher.getWorkspace() && !(d.dragSource instanceof Folder)) { |
| 1081 | cleanUpRunnable = new Runnable() { |
| 1082 | @Override |
| 1083 | public void run() { |
Adam Cohen | 689ff16 | 2014-05-08 17:27:56 -0700 | [diff] [blame] | 1084 | mLauncher.exitSpringLoadedDragModeDelayed(true, |
| 1085 | Launcher.EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, |
| 1086 | null); |
Jorim Jaggi | 55bd972 | 2014-01-16 15:30:42 -0800 | [diff] [blame] | 1087 | } |
| 1088 | }; |
Adam Cohen | bfbfd26 | 2011-06-13 16:55:12 -0700 | [diff] [blame] | 1089 | } |
Jorim Jaggi | 55bd972 | 2014-01-16 15:30:42 -0800 | [diff] [blame] | 1090 | |
Sunny Goyal | 4846193 | 2015-03-09 17:41:09 -0700 | [diff] [blame] | 1091 | if (ALLOW_FOLDER_SCROLL) { |
| 1092 | // If the icon was dropped while the page was being scrolled, we need to compute |
| 1093 | // the target location again such that the icon is placed of the final page. |
| 1094 | if (!mPagedView.rankOnCurrentPage(mEmptyCellRank)) { |
| 1095 | // Reorder again. |
| 1096 | mTargetRank = getTargetRank(d, null); |
| 1097 | |
| 1098 | // Rearrange items immediately. |
| 1099 | mReorderAlarmListener.onAlarm(mReorderAlarm); |
| 1100 | |
| 1101 | mOnScrollHintAlarm.cancelAlarm(); |
| 1102 | mScrollPauseAlarm.cancelAlarm(); |
| 1103 | } |
| 1104 | mPagedView.completePendingPageChanges(); |
| 1105 | } |
| 1106 | |
Jorim Jaggi | 55bd972 | 2014-01-16 15:30:42 -0800 | [diff] [blame] | 1107 | View currentDragView; |
| 1108 | ShortcutInfo si = mCurrentDragInfo; |
| 1109 | if (mIsExternalDrag) { |
Sunny Goyal | c3a609f | 2015-02-26 17:43:50 -0800 | [diff] [blame] | 1110 | currentDragView = mContent.createAndAddViewForRank(si, mEmptyCellRank); |
Sunny Goyal | 95abbb3 | 2014-08-04 10:53:22 -0700 | [diff] [blame] | 1111 | // Actually move the item in the database if it was an external drag. Call this |
| 1112 | // before creating the view, so that ShortcutInfo is updated appropriately. |
| 1113 | LauncherModel.addOrMoveItemInDatabase( |
| 1114 | mLauncher, si, mInfo.id, 0, si.cellX, si.cellY); |
| 1115 | |
| 1116 | // We only need to update the locations if it doesn't get handled in #onDropCompleted. |
| 1117 | if (d.dragSource != this) { |
| 1118 | updateItemLocationsInDatabaseBatch(); |
| 1119 | } |
| 1120 | mIsExternalDrag = false; |
Jorim Jaggi | 55bd972 | 2014-01-16 15:30:42 -0800 | [diff] [blame] | 1121 | } else { |
| 1122 | currentDragView = mCurrentDragView; |
Sunny Goyal | c3a609f | 2015-02-26 17:43:50 -0800 | [diff] [blame] | 1123 | mContent.addViewForRank(currentDragView, si, mEmptyCellRank); |
Adam Cohen | bfbfd26 | 2011-06-13 16:55:12 -0700 | [diff] [blame] | 1124 | } |
Jorim Jaggi | 55bd972 | 2014-01-16 15:30:42 -0800 | [diff] [blame] | 1125 | |
| 1126 | if (d.dragView.hasDrawn()) { |
| 1127 | |
| 1128 | // Temporarily reset the scale such that the animation target gets calculated correctly. |
| 1129 | float scaleX = getScaleX(); |
| 1130 | float scaleY = getScaleY(); |
| 1131 | setScaleX(1.0f); |
| 1132 | setScaleY(1.0f); |
| 1133 | mLauncher.getDragLayer().animateViewIntoPosition(d.dragView, currentDragView, |
| 1134 | cleanUpRunnable, null); |
| 1135 | setScaleX(scaleX); |
| 1136 | setScaleY(scaleY); |
| 1137 | } else { |
| 1138 | d.deferDragViewCleanupPostAnimation = false; |
| 1139 | currentDragView.setVisibility(VISIBLE); |
| 1140 | } |
| 1141 | mItemsInvalidated = true; |
Sunny Goyal | c3a609f | 2015-02-26 17:43:50 -0800 | [diff] [blame] | 1142 | rearrangeChildren(); |
Jorim Jaggi | 55bd972 | 2014-01-16 15:30:42 -0800 | [diff] [blame] | 1143 | |
Jorim Jaggi | 55bd972 | 2014-01-16 15:30:42 -0800 | [diff] [blame] | 1144 | // Temporarily suppress the listener, as we did all the work already here. |
| 1145 | mSuppressOnAdd = true; |
| 1146 | mInfo.add(si); |
| 1147 | mSuppressOnAdd = false; |
Sunny Goyal | 4b02017 | 2014-08-28 14:51:14 -0700 | [diff] [blame] | 1148 | // Clear the drag info, as it is no longer being dragged. |
| 1149 | mCurrentDragInfo = null; |
Adam Cohen | bfbfd26 | 2011-06-13 16:55:12 -0700 | [diff] [blame] | 1150 | } |
| 1151 | |
Adam Cohen | 7a8b82b | 2013-05-29 18:41:50 -0700 | [diff] [blame] | 1152 | // This is used so the item doesn't immediately appear in the folder when added. In one case |
| 1153 | // we need to create the illusion that the item isn't added back to the folder yet, to |
| 1154 | // to correspond to the animation of the icon back into the folder. This is |
| 1155 | public void hideItem(ShortcutInfo info) { |
| 1156 | View v = getViewForInfo(info); |
| 1157 | v.setVisibility(INVISIBLE); |
| 1158 | } |
| 1159 | public void showItem(ShortcutInfo info) { |
| 1160 | View v = getViewForInfo(info); |
| 1161 | v.setVisibility(VISIBLE); |
| 1162 | } |
| 1163 | |
Adam Cohen | bfbfd26 | 2011-06-13 16:55:12 -0700 | [diff] [blame] | 1164 | public void onAdd(ShortcutInfo item) { |
Adam Cohen | 05e0f40 | 2011-08-01 12:12:49 -0700 | [diff] [blame] | 1165 | // If the item was dropped onto this open folder, we have done the work associated |
| 1166 | // with adding the item to the folder, as indicated by mSuppressOnAdd being set |
Adam Cohen | bfbfd26 | 2011-06-13 16:55:12 -0700 | [diff] [blame] | 1167 | if (mSuppressOnAdd) return; |
Sunny Goyal | 5d85c44 | 2015-03-10 13:14:47 -0700 | [diff] [blame] | 1168 | mContent.createAndAddViewForRank(item, mContent.allocateRankForNewItem(item)); |
Sunny Goyal | 2e688a8 | 2015-03-18 10:23:39 -0700 | [diff] [blame] | 1169 | mItemsInvalidated = true; |
Adam Cohen | bfbfd26 | 2011-06-13 16:55:12 -0700 | [diff] [blame] | 1170 | LauncherModel.addOrMoveItemInDatabase( |
| 1171 | mLauncher, item, mInfo.id, 0, item.cellX, item.cellY); |
| 1172 | } |
| 1173 | |
Adam Cohen | a9cf38f | 2011-05-02 15:36:58 -0700 | [diff] [blame] | 1174 | public void onRemove(ShortcutInfo item) { |
Adam Cohen | 7c69321 | 2011-05-18 15:26:57 -0700 | [diff] [blame] | 1175 | mItemsInvalidated = true; |
Adam Cohen | 05e0f40 | 2011-08-01 12:12:49 -0700 | [diff] [blame] | 1176 | // If this item is being dragged from this open folder, we have already handled |
| 1177 | // the work associated with removing the item, so we don't have to do anything here. |
Adam Cohen | bfbfd26 | 2011-06-13 16:55:12 -0700 | [diff] [blame] | 1178 | if (item == mCurrentDragInfo) return; |
Adam Cohen | df1e4e8 | 2011-06-24 15:57:39 -0700 | [diff] [blame] | 1179 | View v = getViewForInfo(item); |
Sunny Goyal | 290800b | 2015-03-05 11:33:33 -0800 | [diff] [blame] | 1180 | mContent.removeItem(v); |
Adam Cohen | 2801caf | 2011-05-13 20:57:39 -0700 | [diff] [blame] | 1181 | if (mState == STATE_ANIMATING) { |
| 1182 | mRearrangeOnClose = true; |
| 1183 | } else { |
Sunny Goyal | c3a609f | 2015-02-26 17:43:50 -0800 | [diff] [blame] | 1184 | rearrangeChildren(); |
Adam Cohen | 2801caf | 2011-05-13 20:57:39 -0700 | [diff] [blame] | 1185 | } |
Adam Cohen | afb01ee | 2011-06-23 15:38:03 -0700 | [diff] [blame] | 1186 | if (getItemCount() <= 1) { |
| 1187 | replaceFolderWithFinalItem(); |
| 1188 | } |
Adam Cohen | a9cf38f | 2011-05-02 15:36:58 -0700 | [diff] [blame] | 1189 | } |
Adam Cohen | 7c69321 | 2011-05-18 15:26:57 -0700 | [diff] [blame] | 1190 | |
Sunny Goyal | bc75335 | 2015-03-05 09:40:44 -0800 | [diff] [blame] | 1191 | private View getViewForInfo(final ShortcutInfo item) { |
| 1192 | return mContent.iterateOverItems(new ItemOperator() { |
| 1193 | |
| 1194 | @Override |
| 1195 | public boolean evaluate(ItemInfo info, View view, View parent) { |
| 1196 | return info == item; |
Adam Cohen | df1e4e8 | 2011-06-24 15:57:39 -0700 | [diff] [blame] | 1197 | } |
Sunny Goyal | bc75335 | 2015-03-05 09:40:44 -0800 | [diff] [blame] | 1198 | }); |
Adam Cohen | df1e4e8 | 2011-06-24 15:57:39 -0700 | [diff] [blame] | 1199 | } |
| 1200 | |
Adam Cohen | 76078c4 | 2011-06-09 15:06:52 -0700 | [diff] [blame] | 1201 | public void onItemsChanged() { |
Adam Cohen | ac56cff | 2011-09-28 20:45:37 -0700 | [diff] [blame] | 1202 | updateTextViewFocus(); |
Adam Cohen | 76078c4 | 2011-06-09 15:06:52 -0700 | [diff] [blame] | 1203 | } |
Adam Cohen | ac56cff | 2011-09-28 20:45:37 -0700 | [diff] [blame] | 1204 | |
Adam Cohen | 76fc085 | 2011-06-17 13:26:23 -0700 | [diff] [blame] | 1205 | public void onTitleChanged(CharSequence title) { |
| 1206 | } |
Adam Cohen | 76078c4 | 2011-06-09 15:06:52 -0700 | [diff] [blame] | 1207 | |
Adam Cohen | 7c69321 | 2011-05-18 15:26:57 -0700 | [diff] [blame] | 1208 | public ArrayList<View> getItemsInReadingOrder() { |
| 1209 | if (mItemsInvalidated) { |
| 1210 | mItemsInReadingOrder.clear(); |
Sunny Goyal | bc75335 | 2015-03-05 09:40:44 -0800 | [diff] [blame] | 1211 | mContent.iterateOverItems(new ItemOperator() { |
| 1212 | |
| 1213 | @Override |
| 1214 | public boolean evaluate(ItemInfo info, View view, View parent) { |
| 1215 | mItemsInReadingOrder.add(view); |
| 1216 | return false; |
Adam Cohen | 7c69321 | 2011-05-18 15:26:57 -0700 | [diff] [blame] | 1217 | } |
Sunny Goyal | bc75335 | 2015-03-05 09:40:44 -0800 | [diff] [blame] | 1218 | }); |
Adam Cohen | 7c69321 | 2011-05-18 15:26:57 -0700 | [diff] [blame] | 1219 | mItemsInvalidated = false; |
| 1220 | } |
| 1221 | return mItemsInReadingOrder; |
| 1222 | } |
Adam Cohen | 8dfcba4 | 2011-07-07 16:38:18 -0700 | [diff] [blame] | 1223 | |
| 1224 | public void getLocationInDragLayer(int[] loc) { |
| 1225 | mLauncher.getDragLayer().getLocationInDragLayer(this, loc); |
| 1226 | } |
Adam Cohen | ea0818d | 2011-09-30 20:06:58 -0700 | [diff] [blame] | 1227 | |
| 1228 | public void onFocusChange(View v, boolean hasFocus) { |
| 1229 | if (v == mFolderName && hasFocus) { |
| 1230 | startEditingFolderName(); |
| 1231 | } |
| 1232 | } |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 1233 | |
| 1234 | @Override |
| 1235 | public void getHitRectRelativeToDragLayer(Rect outRect) { |
| 1236 | getHitRect(outRect); |
Sunny Goyal | 4846193 | 2015-03-09 17:41:09 -0700 | [diff] [blame] | 1237 | outRect.left -= mScrollAreaOffset; |
| 1238 | outRect.right += mScrollAreaOffset; |
| 1239 | } |
| 1240 | |
| 1241 | private class OnScrollHintListener implements OnAlarmListener { |
| 1242 | |
| 1243 | private final DragObject mDragObject; |
| 1244 | |
| 1245 | OnScrollHintListener(DragObject object) { |
| 1246 | mDragObject = object; |
| 1247 | } |
| 1248 | |
| 1249 | /** |
| 1250 | * Scroll hint has been shown long enough. Now scroll to appropriate page. |
| 1251 | */ |
| 1252 | @Override |
| 1253 | public void onAlarm(Alarm alarm) { |
| 1254 | if (mCurrentScrollDir == DragController.SCROLL_LEFT) { |
| 1255 | mPagedView.scrollLeft(); |
| 1256 | mScrollHintDir = DragController.SCROLL_NONE; |
| 1257 | } else if (mCurrentScrollDir == DragController.SCROLL_RIGHT) { |
| 1258 | mPagedView.scrollRight(); |
| 1259 | mScrollHintDir = DragController.SCROLL_NONE; |
| 1260 | } else { |
| 1261 | // This should not happen |
| 1262 | return; |
| 1263 | } |
| 1264 | mCurrentScrollDir = DragController.SCROLL_NONE; |
| 1265 | |
| 1266 | // Pause drag event until the scrolling is finished |
| 1267 | mScrollPauseAlarm.setOnAlarmListener(new OnScrollFinishedListener(mDragObject)); |
| 1268 | mScrollPauseAlarm.setAlarm(DragController.RESCROLL_DELAY); |
| 1269 | } |
| 1270 | } |
| 1271 | |
| 1272 | private class OnScrollFinishedListener implements OnAlarmListener { |
| 1273 | |
| 1274 | private final DragObject mDragObject; |
| 1275 | |
| 1276 | OnScrollFinishedListener(DragObject object) { |
| 1277 | mDragObject = object; |
| 1278 | } |
| 1279 | |
| 1280 | /** |
| 1281 | * Page scroll is complete. |
| 1282 | */ |
| 1283 | @Override |
| 1284 | public void onAlarm(Alarm alarm) { |
| 1285 | // Reorder immediately on page change. |
| 1286 | onDragOver(mDragObject, 1); |
| 1287 | } |
Adam Cohen | 7d30a37 | 2013-07-01 17:03:59 -0700 | [diff] [blame] | 1288 | } |
Sunny Goyal | bc75335 | 2015-03-05 09:40:44 -0800 | [diff] [blame] | 1289 | |
| 1290 | public static interface FolderContent { |
| 1291 | void setFolder(Folder f); |
| 1292 | |
Sunny Goyal | 290800b | 2015-03-05 11:33:33 -0800 | [diff] [blame] | 1293 | void removeItem(View v); |
Sunny Goyal | bc75335 | 2015-03-05 09:40:44 -0800 | [diff] [blame] | 1294 | |
| 1295 | boolean isFull(); |
| 1296 | int getItemCount(); |
| 1297 | |
| 1298 | int getDesiredWidth(); |
| 1299 | int getDesiredHeight(); |
| 1300 | void setFixedSize(int width, int height); |
| 1301 | |
| 1302 | /** |
| 1303 | * Iterates over all its items in a reading order. |
| 1304 | * @return the view for which the operator returned true. |
| 1305 | */ |
| 1306 | View iterateOverItems(ItemOperator op); |
| 1307 | View getLastItem(); |
| 1308 | |
| 1309 | String getAccessibilityDescription(); |
| 1310 | |
| 1311 | /** |
| 1312 | * Binds items to the layout. |
| 1313 | * @return list of items that could not be bound, probably because we hit the max size limit. |
| 1314 | */ |
| 1315 | ArrayList<ShortcutInfo> bindItems(ArrayList<ShortcutInfo> children); |
| 1316 | |
| 1317 | /** |
Sunny Goyal | 5d85c44 | 2015-03-10 13:14:47 -0700 | [diff] [blame] | 1318 | * Create space for a new item, and returns the rank for that item. |
Sunny Goyal | bc75335 | 2015-03-05 09:40:44 -0800 | [diff] [blame] | 1319 | * Resizes the content if necessary. |
| 1320 | */ |
Sunny Goyal | 5d85c44 | 2015-03-10 13:14:47 -0700 | [diff] [blame] | 1321 | int allocateRankForNewItem(ShortcutInfo info); |
Sunny Goyal | bc75335 | 2015-03-05 09:40:44 -0800 | [diff] [blame] | 1322 | |
| 1323 | View createAndAddViewForRank(ShortcutInfo item, int rank); |
| 1324 | |
| 1325 | /** |
| 1326 | * Adds the {@param view} to the layout based on {@param rank} and updated the position |
| 1327 | * related attributes. It assumes that {@param item} is already attached to the view. |
| 1328 | */ |
| 1329 | void addViewForRank(View view, ShortcutInfo item, int rank); |
| 1330 | |
| 1331 | /** |
| 1332 | * Reorders the items such that the {@param empty} spot moves to {@param target} |
| 1333 | */ |
| 1334 | void realTimeReorder(int empty, int target); |
| 1335 | |
| 1336 | /** |
| 1337 | * @return the rank of the cell nearest to the provided pixel position. |
| 1338 | */ |
| 1339 | int findNearestArea(int pixelX, int pixelY); |
| 1340 | |
| 1341 | /** |
| 1342 | * Updates position and rank of all the children in the view based. |
| 1343 | * @param list the ordered list of children. |
| 1344 | * @param itemCount if greater than the total children count, empty spaces are left |
| 1345 | * at the end. |
| 1346 | */ |
| 1347 | void arrangeChildren(ArrayList<View> list, int itemCount); |
| 1348 | |
| 1349 | /** |
| 1350 | * Sets the focus on the first visible child. |
| 1351 | */ |
| 1352 | void setFocusOnFirstChild(); |
| 1353 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1354 | } |