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