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