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