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