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