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