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