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 | |
Joe Onorato | a590252 | 2009-07-30 13:37:37 -0700 | [diff] [blame] | 17 | package com.android.launcher2; |
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; |
| 23 | import android.animation.ValueAnimator; |
| 24 | import android.animation.ValueAnimator.AnimatorUpdateListener; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 25 | import android.content.Context; |
Adam Cohen | 76fc085 | 2011-06-17 13:26:23 -0700 | [diff] [blame] | 26 | import android.content.res.Resources; |
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 | 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 | 2801caf | 2011-05-13 20:57:39 -0700 | [diff] [blame] | 40 | import android.view.animation.AccelerateInterpolator; |
| 41 | import android.view.animation.DecelerateInterpolator; |
Adam Cohen | 76fc085 | 2011-06-17 13:26:23 -0700 | [diff] [blame] | 42 | import android.view.inputmethod.EditorInfo; |
| 43 | import android.view.inputmethod.InputMethodManager; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 44 | import android.widget.LinearLayout; |
Adam Cohen | df2cc41 | 2011-04-27 16:56:57 -0700 | [diff] [blame] | 45 | import android.widget.TextView; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 46 | |
Romain Guy | edcce09 | 2010-03-04 13:03:17 -0800 | [diff] [blame] | 47 | import com.android.launcher.R; |
Adam Cohen | a9cf38f | 2011-05-02 15:36:58 -0700 | [diff] [blame] | 48 | import com.android.launcher2.FolderInfo.FolderListener; |
Romain Guy | edcce09 | 2010-03-04 13:03:17 -0800 | [diff] [blame] | 49 | |
Adam Cohen | c0dcf59 | 2011-06-01 15:30:43 -0700 | [diff] [blame] | 50 | import java.util.ArrayList; |
| 51 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 52 | /** |
| 53 | * Represents a set of icons chosen by the user or generated by the system. |
| 54 | */ |
Adam Cohen | 8dfcba4 | 2011-07-07 16:38:18 -0700 | [diff] [blame] | 55 | public class Folder extends LinearLayout implements DragSource, View.OnClickListener, |
| 56 | View.OnLongClickListener, DropTarget, FolderListener, TextView.OnEditorActionListener { |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 57 | |
Adam Cohen | df2cc41 | 2011-04-27 16:56:57 -0700 | [diff] [blame] | 58 | private static final String TAG = "Launcher.Folder"; |
| 59 | |
Adam Cohen | 4eac29a | 2011-07-11 17:53:37 -0700 | [diff] [blame] | 60 | protected DragController mDragController; |
| 61 | protected Launcher mLauncher; |
| 62 | protected FolderInfo mInfo; |
| 63 | |
Adam Cohen | df2cc41 | 2011-04-27 16:56:57 -0700 | [diff] [blame] | 64 | static final int STATE_NONE = -1; |
| 65 | static final int STATE_SMALL = 0; |
| 66 | static final int STATE_ANIMATING = 1; |
| 67 | static final int STATE_OPEN = 2; |
| 68 | |
| 69 | private int mExpandDuration; |
| 70 | protected CellLayout mContent; |
| 71 | private final LayoutInflater mInflater; |
| 72 | private final IconCache mIconCache; |
| 73 | private int mState = STATE_NONE; |
Adam Cohen | 2801caf | 2011-05-13 20:57:39 -0700 | [diff] [blame] | 74 | private static final int FULL_GROW = 0; |
| 75 | private static final int PARTIAL_GROW = 1; |
Adam Cohen | bfbfd26 | 2011-06-13 16:55:12 -0700 | [diff] [blame] | 76 | private static final int REORDER_ANIMATION_DURATION = 230; |
| 77 | private static final int ON_EXIT_CLOSE_DELAY = 800; |
Adam Cohen | 2801caf | 2011-05-13 20:57:39 -0700 | [diff] [blame] | 78 | private int mMode = PARTIAL_GROW; |
| 79 | private boolean mRearrangeOnClose = false; |
| 80 | private FolderIcon mFolderIcon; |
| 81 | private int mMaxCountX; |
| 82 | private int mMaxCountY; |
| 83 | private Rect mNewSize = new Rect(); |
Adam Cohen | 3e8f811 | 2011-07-02 18:03:00 -0700 | [diff] [blame] | 84 | private Rect mIconRect = new Rect(); |
Adam Cohen | 7c69321 | 2011-05-18 15:26:57 -0700 | [diff] [blame] | 85 | private ArrayList<View> mItemsInReadingOrder = new ArrayList<View>(); |
Adam Cohen | badf71e | 2011-05-26 19:08:29 -0700 | [diff] [blame] | 86 | private Drawable mIconDrawable; |
Adam Cohen | 7c69321 | 2011-05-18 15:26:57 -0700 | [diff] [blame] | 87 | boolean mItemsInvalidated = false; |
Adam Cohen | bfbfd26 | 2011-06-13 16:55:12 -0700 | [diff] [blame] | 88 | private ShortcutInfo mCurrentDragInfo; |
| 89 | private View mCurrentDragView; |
| 90 | boolean mSuppressOnAdd = false; |
| 91 | private int[] mTargetCell = new int[2]; |
| 92 | private int[] mPreviousTargetCell = new int[2]; |
| 93 | private int[] mEmptyCell = new int[2]; |
| 94 | private Alarm mReorderAlarm = new Alarm(); |
| 95 | private Alarm mOnExitAlarm = new Alarm(); |
Adam Cohen | 76fc085 | 2011-06-17 13:26:23 -0700 | [diff] [blame] | 96 | private TextView mFolderName; |
| 97 | private int mFolderNameHeight; |
Adam Cohen | 4ef610f | 2011-06-21 22:37:36 -0700 | [diff] [blame] | 98 | private Rect mHitRect = new Rect(); |
Adam Cohen | 8e776a6 | 2011-06-28 18:10:06 -0700 | [diff] [blame] | 99 | private Rect mTempRect = new Rect(); |
Adam Cohen | 67bd9cc | 2011-07-29 14:07:04 -0700 | [diff] [blame] | 100 | private boolean mDragInProgress = false; |
| 101 | private boolean mDeleteFolderOnDropCompleted = false; |
| 102 | private boolean mSuppressFolderDeletion = false; |
Adam Cohen | 05e0f40 | 2011-08-01 12:12:49 -0700 | [diff] [blame] | 103 | private boolean mItemAddedBackToSelfViaIcon = false; |
Adam Cohen | 228da5a | 2011-07-27 22:23:47 -0700 | [diff] [blame] | 104 | |
Adam Cohen | 76fc085 | 2011-06-17 13:26:23 -0700 | [diff] [blame] | 105 | private boolean mIsEditingName = false; |
| 106 | private InputMethodManager mInputMethodManager; |
Adam Cohen | df2cc41 | 2011-04-27 16:56:57 -0700 | [diff] [blame] | 107 | |
Adam Cohen | a65beee | 2011-06-27 21:32:23 -0700 | [diff] [blame] | 108 | private static String sDefaultFolderName; |
| 109 | private static String sHintText; |
| 110 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 111 | /** |
| 112 | * Used to inflate the Workspace from XML. |
| 113 | * |
| 114 | * @param context The application's context. |
| 115 | * @param attrs The attribtues set containing the Workspace's customization values. |
| 116 | */ |
| 117 | public Folder(Context context, AttributeSet attrs) { |
| 118 | super(context, attrs); |
| 119 | setAlwaysDrawnWithCacheEnabled(false); |
Adam Cohen | df2cc41 | 2011-04-27 16:56:57 -0700 | [diff] [blame] | 120 | mInflater = LayoutInflater.from(context); |
| 121 | mIconCache = ((LauncherApplication)context.getApplicationContext()).getIconCache(); |
Adam Cohen | 8e776a6 | 2011-06-28 18:10:06 -0700 | [diff] [blame] | 122 | mMaxCountX = LauncherModel.getCellCountX(); |
| 123 | mMaxCountY = LauncherModel.getCellCountY(); |
Adam Cohen | 76fc085 | 2011-06-17 13:26:23 -0700 | [diff] [blame] | 124 | |
| 125 | mInputMethodManager = (InputMethodManager) |
| 126 | mContext.getSystemService(Context.INPUT_METHOD_SERVICE); |
| 127 | |
| 128 | Resources res = getResources(); |
| 129 | mExpandDuration = res.getInteger(R.integer.config_folderAnimDuration); |
Adam Cohen | 4ef610f | 2011-06-21 22:37:36 -0700 | [diff] [blame] | 130 | |
| 131 | if (sDefaultFolderName == null) { |
| 132 | sDefaultFolderName = res.getString(R.string.folder_name); |
| 133 | } |
Adam Cohen | a65beee | 2011-06-27 21:32:23 -0700 | [diff] [blame] | 134 | if (sHintText == null) { |
| 135 | sHintText = res.getString(R.string.folder_hint_text); |
| 136 | } |
Adam Cohen | 4eac29a | 2011-07-11 17:53:37 -0700 | [diff] [blame] | 137 | |
| 138 | mLauncher = (Launcher) context; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | @Override |
| 142 | protected void onFinishInflate() { |
| 143 | super.onFinishInflate(); |
Adam Cohen | df2cc41 | 2011-04-27 16:56:57 -0700 | [diff] [blame] | 144 | mContent = (CellLayout) findViewById(R.id.folder_content); |
Adam Cohen | 2801caf | 2011-05-13 20:57:39 -0700 | [diff] [blame] | 145 | mContent.setGridSize(0, 0); |
Adam Cohen | 76fc085 | 2011-06-17 13:26:23 -0700 | [diff] [blame] | 146 | mFolderName = (TextView) findViewById(R.id.folder_name); |
| 147 | |
| 148 | // We find out how tall the text view wants to be (it is set to wrap_content), so that |
| 149 | // we can allocate the appropriate amount of space for it. |
| 150 | int measureSpec = MeasureSpec.UNSPECIFIED; |
| 151 | mFolderName.measure(measureSpec, measureSpec); |
| 152 | mFolderNameHeight = mFolderName.getMeasuredHeight(); |
| 153 | |
| 154 | // We disable action mode for now since it messes up the view on phones |
| 155 | mFolderName.setCustomSelectionActionModeCallback(mActionModeCallback); |
| 156 | mFolderName.setCursorVisible(false); |
| 157 | mFolderName.setOnEditorActionListener(this); |
Adam Cohen | 4ef610f | 2011-06-21 22:37:36 -0700 | [diff] [blame] | 158 | mFolderName.setSelectAllOnFocus(true); |
Adam Cohen | 7a14d0b | 2011-06-24 11:36:35 -0700 | [diff] [blame] | 159 | mFolderName.setInputType(mFolderName.getInputType() | |
| 160 | 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] | 161 | } |
Adam Cohen | 2801caf | 2011-05-13 20:57:39 -0700 | [diff] [blame] | 162 | |
Adam Cohen | 76fc085 | 2011-06-17 13:26:23 -0700 | [diff] [blame] | 163 | private ActionMode.Callback mActionModeCallback = new ActionMode.Callback() { |
| 164 | public boolean onActionItemClicked(ActionMode mode, MenuItem item) { |
| 165 | return false; |
| 166 | } |
| 167 | |
| 168 | public boolean onCreateActionMode(ActionMode mode, Menu menu) { |
| 169 | return false; |
| 170 | } |
| 171 | |
| 172 | public void onDestroyActionMode(ActionMode mode) { |
| 173 | } |
| 174 | |
| 175 | public boolean onPrepareActionMode(ActionMode mode, Menu menu) { |
| 176 | return false; |
| 177 | } |
| 178 | }; |
| 179 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 180 | public void onClick(View v) { |
Adam Cohen | df2cc41 | 2011-04-27 16:56:57 -0700 | [diff] [blame] | 181 | Object tag = v.getTag(); |
| 182 | if (tag instanceof ShortcutInfo) { |
| 183 | // refactor this code from Folder |
| 184 | ShortcutInfo item = (ShortcutInfo) tag; |
| 185 | int[] pos = new int[2]; |
| 186 | v.getLocationOnScreen(pos); |
| 187 | item.intent.setSourceBounds(new Rect(pos[0], pos[1], |
| 188 | pos[0] + v.getWidth(), pos[1] + v.getHeight())); |
| 189 | mLauncher.startActivitySafely(item.intent, item); |
Adam Cohen | df2cc41 | 2011-04-27 16:56:57 -0700 | [diff] [blame] | 190 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 191 | } |
| 192 | |
Adam Cohen | 76fc085 | 2011-06-17 13:26:23 -0700 | [diff] [blame] | 193 | public boolean onInterceptTouchEvent(MotionEvent ev) { |
| 194 | if (ev.getAction() == MotionEvent.ACTION_DOWN) { |
| 195 | mFolderName.getHitRect(mHitRect); |
| 196 | if (mHitRect.contains((int) ev.getX(), (int) ev.getY()) && !mIsEditingName) { |
| 197 | startEditingFolderName(); |
| 198 | } |
| 199 | } |
| 200 | return false; |
| 201 | } |
| 202 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 203 | public boolean onLongClick(View v) { |
Adam Cohen | df2cc41 | 2011-04-27 16:56:57 -0700 | [diff] [blame] | 204 | Object tag = v.getTag(); |
| 205 | if (tag instanceof ShortcutInfo) { |
Adam Cohen | df2cc41 | 2011-04-27 16:56:57 -0700 | [diff] [blame] | 206 | ShortcutInfo item = (ShortcutInfo) tag; |
| 207 | if (!v.isInTouchMode()) { |
| 208 | return false; |
| 209 | } |
| 210 | |
Winson Chung | 7d7541e | 2011-09-16 20:14:36 -0700 | [diff] [blame] | 211 | mLauncher.dismissFolderCling(null); |
| 212 | |
Adam Cohen | df2cc41 | 2011-04-27 16:56:57 -0700 | [diff] [blame] | 213 | mLauncher.getWorkspace().onDragStartedWithItem(v); |
Adam Cohen | ac8c876 | 2011-07-13 11:15:27 -0700 | [diff] [blame] | 214 | mLauncher.getWorkspace().beginDragShared(v, this); |
Adam Cohen | badf71e | 2011-05-26 19:08:29 -0700 | [diff] [blame] | 215 | mIconDrawable = ((TextView) v).getCompoundDrawables()[1]; |
Adam Cohen | 76078c4 | 2011-06-09 15:06:52 -0700 | [diff] [blame] | 216 | |
| 217 | mCurrentDragInfo = item; |
Adam Cohen | bfbfd26 | 2011-06-13 16:55:12 -0700 | [diff] [blame] | 218 | mEmptyCell[0] = item.cellX; |
| 219 | mEmptyCell[1] = item.cellY; |
| 220 | mCurrentDragView = v; |
Adam Cohen | fc53cd2 | 2011-07-20 15:45:11 -0700 | [diff] [blame] | 221 | |
| 222 | mContent.removeView(mCurrentDragView); |
| 223 | mInfo.remove(mCurrentDragInfo); |
Adam Cohen | 67bd9cc | 2011-07-29 14:07:04 -0700 | [diff] [blame] | 224 | mDragInProgress = true; |
Adam Cohen | 05e0f40 | 2011-08-01 12:12:49 -0700 | [diff] [blame] | 225 | mItemAddedBackToSelfViaIcon = false; |
Adam Cohen | df2cc41 | 2011-04-27 16:56:57 -0700 | [diff] [blame] | 226 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 227 | return true; |
| 228 | } |
| 229 | |
Adam Cohen | 76fc085 | 2011-06-17 13:26:23 -0700 | [diff] [blame] | 230 | public boolean isEditingName() { |
| 231 | return mIsEditingName; |
| 232 | } |
| 233 | |
| 234 | public void startEditingFolderName() { |
Adam Cohen | a65beee | 2011-06-27 21:32:23 -0700 | [diff] [blame] | 235 | mFolderName.setHint(""); |
Adam Cohen | 76fc085 | 2011-06-17 13:26:23 -0700 | [diff] [blame] | 236 | mFolderName.setCursorVisible(true); |
| 237 | mIsEditingName = true; |
| 238 | } |
| 239 | |
| 240 | public void dismissEditingName() { |
| 241 | mInputMethodManager.hideSoftInputFromWindow(getWindowToken(), 0); |
| 242 | doneEditingFolderName(true); |
| 243 | } |
| 244 | |
| 245 | public void doneEditingFolderName(boolean commit) { |
Adam Cohen | a65beee | 2011-06-27 21:32:23 -0700 | [diff] [blame] | 246 | mFolderName.setHint(sHintText); |
Adam Cohen | 1df26a3 | 2011-08-12 16:15:23 -0700 | [diff] [blame] | 247 | // Convert to a string here to ensure that no other state associated with the text field |
| 248 | // gets saved. |
| 249 | mInfo.setTitle(mFolderName.getText().toString()); |
Adam Cohen | 76fc085 | 2011-06-17 13:26:23 -0700 | [diff] [blame] | 250 | LauncherModel.updateItemInDatabase(mLauncher, mInfo); |
| 251 | mFolderName.setCursorVisible(false); |
| 252 | mFolderName.clearFocus(); |
Adam Cohen | e601a43 | 2011-07-26 21:51:30 -0700 | [diff] [blame] | 253 | Selection.setSelection((Spannable) mFolderName.getText(), 0, 0); |
Adam Cohen | 76fc085 | 2011-06-17 13:26:23 -0700 | [diff] [blame] | 254 | mIsEditingName = false; |
| 255 | } |
| 256 | |
| 257 | public boolean onEditorAction(TextView v, int actionId, KeyEvent event) { |
| 258 | if (actionId == EditorInfo.IME_ACTION_DONE) { |
| 259 | dismissEditingName(); |
| 260 | return true; |
| 261 | } |
| 262 | return false; |
| 263 | } |
| 264 | |
| 265 | public View getEditTextRegion() { |
| 266 | return mFolderName; |
| 267 | } |
| 268 | |
Adam Cohen | badf71e | 2011-05-26 19:08:29 -0700 | [diff] [blame] | 269 | public Drawable getDragDrawable() { |
| 270 | return mIconDrawable; |
| 271 | } |
| 272 | |
Adam Cohen | 0c872ba | 2011-05-05 10:34:16 -0700 | [diff] [blame] | 273 | /** |
| 274 | * We need to handle touch events to prevent them from falling through to the workspace below. |
| 275 | */ |
| 276 | @Override |
| 277 | public boolean onTouchEvent(MotionEvent ev) { |
| 278 | return true; |
| 279 | } |
| 280 | |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 281 | public void setDragController(DragController dragController) { |
| 282 | mDragController = dragController; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 283 | } |
| 284 | |
Adam Cohen | 2801caf | 2011-05-13 20:57:39 -0700 | [diff] [blame] | 285 | void setFolderIcon(FolderIcon icon) { |
| 286 | mFolderIcon = icon; |
| 287 | } |
| 288 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 289 | /** |
| 290 | * @return the FolderInfo object associated with this folder |
| 291 | */ |
| 292 | FolderInfo getInfo() { |
| 293 | return mInfo; |
| 294 | } |
| 295 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 296 | void bind(FolderInfo info) { |
| 297 | mInfo = info; |
Adam Cohen | df2cc41 | 2011-04-27 16:56:57 -0700 | [diff] [blame] | 298 | ArrayList<ShortcutInfo> children = info.contents; |
Adam Cohen | c508b2d | 2011-06-28 14:41:44 -0700 | [diff] [blame] | 299 | ArrayList<ShortcutInfo> overflow = new ArrayList<ShortcutInfo>(); |
Adam Cohen | 7c69321 | 2011-05-18 15:26:57 -0700 | [diff] [blame] | 300 | setupContentForNumItems(children.size()); |
Adam Cohen | 0057bbc | 2011-08-12 18:30:51 -0700 | [diff] [blame] | 301 | int count = 0; |
Adam Cohen | df2cc41 | 2011-04-27 16:56:57 -0700 | [diff] [blame] | 302 | for (int i = 0; i < children.size(); i++) { |
| 303 | ShortcutInfo child = (ShortcutInfo) children.get(i); |
Adam Cohen | c508b2d | 2011-06-28 14:41:44 -0700 | [diff] [blame] | 304 | if (!createAndAddShortcut(child)) { |
| 305 | overflow.add(child); |
Adam Cohen | 0057bbc | 2011-08-12 18:30:51 -0700 | [diff] [blame] | 306 | } else { |
| 307 | count++; |
Adam Cohen | c508b2d | 2011-06-28 14:41:44 -0700 | [diff] [blame] | 308 | } |
Adam Cohen | df2cc41 | 2011-04-27 16:56:57 -0700 | [diff] [blame] | 309 | } |
Adam Cohen | c508b2d | 2011-06-28 14:41:44 -0700 | [diff] [blame] | 310 | |
Adam Cohen | 0057bbc | 2011-08-12 18:30:51 -0700 | [diff] [blame] | 311 | // We rearrange the items in case there are any empty gaps |
| 312 | setupContentForNumItems(count); |
| 313 | |
Adam Cohen | c508b2d | 2011-06-28 14:41:44 -0700 | [diff] [blame] | 314 | // If our folder has too many items we prune them from the list. This is an issue |
| 315 | // when upgrading from the old Folders implementation which could contain an unlimited |
| 316 | // number of items. |
| 317 | for (ShortcutInfo item: overflow) { |
| 318 | mInfo.remove(item); |
| 319 | LauncherModel.deleteItemFromDatabase(mLauncher, item); |
| 320 | } |
| 321 | |
Adam Cohen | 4dbe6d9 | 2011-05-18 17:14:15 -0700 | [diff] [blame] | 322 | mItemsInvalidated = true; |
Adam Cohen | a9cf38f | 2011-05-02 15:36:58 -0700 | [diff] [blame] | 323 | mInfo.addListener(this); |
Adam Cohen | 4ef610f | 2011-06-21 22:37:36 -0700 | [diff] [blame] | 324 | |
Adam Cohen | afb01ee | 2011-06-23 15:38:03 -0700 | [diff] [blame] | 325 | if (!sDefaultFolderName.contentEquals(mInfo.title)) { |
Adam Cohen | 4ef610f | 2011-06-21 22:37:36 -0700 | [diff] [blame] | 326 | mFolderName.setText(mInfo.title); |
| 327 | } else { |
| 328 | mFolderName.setText(""); |
| 329 | } |
Adam Cohen | df2cc41 | 2011-04-27 16:56:57 -0700 | [diff] [blame] | 330 | } |
| 331 | |
| 332 | /** |
| 333 | * Creates a new UserFolder, inflated from R.layout.user_folder. |
| 334 | * |
| 335 | * @param context The application's context. |
| 336 | * |
| 337 | * @return A new UserFolder. |
| 338 | */ |
| 339 | static Folder fromXml(Context context) { |
| 340 | return (Folder) LayoutInflater.from(context).inflate(R.layout.user_folder, null); |
| 341 | } |
| 342 | |
| 343 | /** |
| 344 | * This method is intended to make the UserFolder to be visually identical in size and position |
| 345 | * to its associated FolderIcon. This allows for a seamless transition into the expanded state. |
| 346 | */ |
| 347 | private void positionAndSizeAsIcon() { |
Adam Cohen | 3e8f811 | 2011-07-02 18:03:00 -0700 | [diff] [blame] | 348 | if (!(getParent() instanceof DragLayer)) return; |
Adam Cohen | df2cc41 | 2011-04-27 16:56:57 -0700 | [diff] [blame] | 349 | |
Adam Cohen | 8e776a6 | 2011-06-28 18:10:06 -0700 | [diff] [blame] | 350 | DragLayer.LayoutParams lp = (DragLayer.LayoutParams) getLayoutParams(); |
Adam Cohen | df2cc41 | 2011-04-27 16:56:57 -0700 | [diff] [blame] | 351 | |
Adam Cohen | 2801caf | 2011-05-13 20:57:39 -0700 | [diff] [blame] | 352 | if (mMode == PARTIAL_GROW) { |
| 353 | setScaleX(0.8f); |
| 354 | setScaleY(0.8f); |
| 355 | setAlpha(0f); |
| 356 | } else { |
Adam Cohen | 3e8f811 | 2011-07-02 18:03:00 -0700 | [diff] [blame] | 357 | mLauncher.getDragLayer().getDescendantRectRelativeToSelf(mFolderIcon, mIconRect); |
| 358 | lp.width = mIconRect.width(); |
| 359 | lp.height = mIconRect.height(); |
| 360 | lp.x = mIconRect.left; |
| 361 | lp.y = mIconRect.top; |
Adam Cohen | 2801caf | 2011-05-13 20:57:39 -0700 | [diff] [blame] | 362 | mContent.setAlpha(0); |
| 363 | } |
Adam Cohen | df2cc41 | 2011-04-27 16:56:57 -0700 | [diff] [blame] | 364 | mState = STATE_SMALL; |
| 365 | } |
| 366 | |
| 367 | public void animateOpen() { |
Adam Cohen | 3e8f811 | 2011-07-02 18:03:00 -0700 | [diff] [blame] | 368 | positionAndSizeAsIcon(); |
| 369 | |
Adam Cohen | 8e776a6 | 2011-06-28 18:10:06 -0700 | [diff] [blame] | 370 | if (!(getParent() instanceof DragLayer)) return; |
Adam Cohen | df2cc41 | 2011-04-27 16:56:57 -0700 | [diff] [blame] | 371 | |
Adam Cohen | 2801caf | 2011-05-13 20:57:39 -0700 | [diff] [blame] | 372 | ObjectAnimator oa; |
Adam Cohen | 8e776a6 | 2011-06-28 18:10:06 -0700 | [diff] [blame] | 373 | DragLayer.LayoutParams lp = (DragLayer.LayoutParams) getLayoutParams(); |
Adam Cohen | df2cc41 | 2011-04-27 16:56:57 -0700 | [diff] [blame] | 374 | |
Adam Cohen | 2801caf | 2011-05-13 20:57:39 -0700 | [diff] [blame] | 375 | centerAboutIcon(); |
| 376 | if (mMode == PARTIAL_GROW) { |
| 377 | PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1); |
| 378 | PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.0f); |
| 379 | PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.0f); |
| 380 | oa = ObjectAnimator.ofPropertyValuesHolder(this, alpha, scaleX, scaleY); |
| 381 | } else { |
| 382 | PropertyValuesHolder width = PropertyValuesHolder.ofInt("width", mNewSize.width()); |
| 383 | PropertyValuesHolder height = PropertyValuesHolder.ofInt("height", mNewSize.height()); |
| 384 | PropertyValuesHolder x = PropertyValuesHolder.ofInt("x", mNewSize.left); |
| 385 | PropertyValuesHolder y = PropertyValuesHolder.ofInt("y", mNewSize.top); |
| 386 | oa = ObjectAnimator.ofPropertyValuesHolder(lp, width, height, x, y); |
| 387 | oa.addUpdateListener(new AnimatorUpdateListener() { |
| 388 | public void onAnimationUpdate(ValueAnimator animation) { |
| 389 | requestLayout(); |
| 390 | } |
| 391 | }); |
Adam Cohen | df2cc41 | 2011-04-27 16:56:57 -0700 | [diff] [blame] | 392 | |
Adam Cohen | 2801caf | 2011-05-13 20:57:39 -0700 | [diff] [blame] | 393 | PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1.0f); |
| 394 | ObjectAnimator alphaOa = ObjectAnimator.ofPropertyValuesHolder(mContent, alpha); |
| 395 | alphaOa.setDuration(mExpandDuration); |
| 396 | alphaOa.setInterpolator(new AccelerateInterpolator(2.0f)); |
| 397 | alphaOa.start(); |
| 398 | } |
Adam Cohen | df2cc41 | 2011-04-27 16:56:57 -0700 | [diff] [blame] | 399 | |
Adam Cohen | 2801caf | 2011-05-13 20:57:39 -0700 | [diff] [blame] | 400 | oa.addListener(new AnimatorListenerAdapter() { |
Adam Cohen | df2cc41 | 2011-04-27 16:56:57 -0700 | [diff] [blame] | 401 | @Override |
| 402 | public void onAnimationStart(Animator animation) { |
| 403 | mState = STATE_ANIMATING; |
| 404 | } |
| 405 | @Override |
| 406 | public void onAnimationEnd(Animator animation) { |
Adam Cohen | 2801caf | 2011-05-13 20:57:39 -0700 | [diff] [blame] | 407 | mState = STATE_OPEN; |
Winson Chung | 7a74ac9 | 2011-09-20 17:43:51 -0700 | [diff] [blame] | 408 | |
| 409 | Cling cling = mLauncher.showFirstRunFoldersCling(); |
| 410 | if (cling != null) { |
| 411 | cling.bringToFront(); |
| 412 | } |
Adam Cohen | df2cc41 | 2011-04-27 16:56:57 -0700 | [diff] [blame] | 413 | } |
| 414 | }); |
Adam Cohen | 2801caf | 2011-05-13 20:57:39 -0700 | [diff] [blame] | 415 | oa.setDuration(mExpandDuration); |
| 416 | oa.start(); |
Adam Cohen | df2cc41 | 2011-04-27 16:56:57 -0700 | [diff] [blame] | 417 | } |
| 418 | |
| 419 | public void animateClosed() { |
Adam Cohen | 8e776a6 | 2011-06-28 18:10:06 -0700 | [diff] [blame] | 420 | if (!(getParent() instanceof DragLayer)) return; |
Adam Cohen | df2cc41 | 2011-04-27 16:56:57 -0700 | [diff] [blame] | 421 | |
Adam Cohen | 2801caf | 2011-05-13 20:57:39 -0700 | [diff] [blame] | 422 | ObjectAnimator oa; |
Adam Cohen | 2801caf | 2011-05-13 20:57:39 -0700 | [diff] [blame] | 423 | if (mMode == PARTIAL_GROW) { |
| 424 | PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0); |
| 425 | PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 0.9f); |
| 426 | PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 0.9f); |
| 427 | oa = ObjectAnimator.ofPropertyValuesHolder(this, alpha, scaleX, scaleY); |
| 428 | } else { |
Adam Cohen | 8e776a6 | 2011-06-28 18:10:06 -0700 | [diff] [blame] | 429 | DragLayer.LayoutParams lp = (DragLayer.LayoutParams) getLayoutParams(); |
Adam Cohen | df2cc41 | 2011-04-27 16:56:57 -0700 | [diff] [blame] | 430 | |
Adam Cohen | 3e8f811 | 2011-07-02 18:03:00 -0700 | [diff] [blame] | 431 | PropertyValuesHolder width = PropertyValuesHolder.ofInt("width", mIconRect.width()); |
| 432 | PropertyValuesHolder height = PropertyValuesHolder.ofInt("height", mIconRect.height()); |
| 433 | PropertyValuesHolder x = PropertyValuesHolder.ofInt("x", mIconRect.left); |
| 434 | PropertyValuesHolder y = PropertyValuesHolder.ofInt("y", mIconRect.top); |
Adam Cohen | 2801caf | 2011-05-13 20:57:39 -0700 | [diff] [blame] | 435 | oa = ObjectAnimator.ofPropertyValuesHolder(lp, width, height, x, y); |
| 436 | oa.addUpdateListener(new AnimatorUpdateListener() { |
| 437 | public void onAnimationUpdate(ValueAnimator animation) { |
| 438 | requestLayout(); |
| 439 | } |
| 440 | }); |
Adam Cohen | df2cc41 | 2011-04-27 16:56:57 -0700 | [diff] [blame] | 441 | |
Adam Cohen | 2801caf | 2011-05-13 20:57:39 -0700 | [diff] [blame] | 442 | PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0f); |
| 443 | ObjectAnimator alphaOa = ObjectAnimator.ofPropertyValuesHolder(mContent, alpha); |
| 444 | alphaOa.setDuration(mExpandDuration); |
| 445 | alphaOa.setInterpolator(new DecelerateInterpolator(2.0f)); |
| 446 | alphaOa.start(); |
| 447 | } |
Adam Cohen | df2cc41 | 2011-04-27 16:56:57 -0700 | [diff] [blame] | 448 | |
Adam Cohen | 2801caf | 2011-05-13 20:57:39 -0700 | [diff] [blame] | 449 | oa.addListener(new AnimatorListenerAdapter() { |
Adam Cohen | df2cc41 | 2011-04-27 16:56:57 -0700 | [diff] [blame] | 450 | @Override |
| 451 | public void onAnimationEnd(Animator animation) { |
Adam Cohen | 2801caf | 2011-05-13 20:57:39 -0700 | [diff] [blame] | 452 | onCloseComplete(); |
Adam Cohen | 2801caf | 2011-05-13 20:57:39 -0700 | [diff] [blame] | 453 | mState = STATE_SMALL; |
Adam Cohen | df2cc41 | 2011-04-27 16:56:57 -0700 | [diff] [blame] | 454 | } |
| 455 | @Override |
| 456 | public void onAnimationStart(Animator animation) { |
| 457 | mState = STATE_ANIMATING; |
| 458 | } |
| 459 | }); |
Adam Cohen | 2801caf | 2011-05-13 20:57:39 -0700 | [diff] [blame] | 460 | oa.setDuration(mExpandDuration); |
| 461 | oa.start(); |
Adam Cohen | df2cc41 | 2011-04-27 16:56:57 -0700 | [diff] [blame] | 462 | } |
| 463 | |
| 464 | void notifyDataSetChanged() { |
| 465 | // recreate all the children if the data set changes under us. We may want to do this more |
| 466 | // intelligently (ie just removing the views that should no longer exist) |
| 467 | mContent.removeAllViewsInLayout(); |
| 468 | bind(mInfo); |
| 469 | } |
| 470 | |
Adam Cohen | cb3382b | 2011-05-24 14:07:08 -0700 | [diff] [blame] | 471 | public boolean acceptDrop(DragObject d) { |
| 472 | final ItemInfo item = (ItemInfo) d.dragInfo; |
Adam Cohen | df2cc41 | 2011-04-27 16:56:57 -0700 | [diff] [blame] | 473 | final int itemType = item.itemType; |
Adam Cohen | 2801caf | 2011-05-13 20:57:39 -0700 | [diff] [blame] | 474 | return ((itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION || |
| 475 | itemType == LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT) && |
| 476 | !isFull()); |
Adam Cohen | df2cc41 | 2011-04-27 16:56:57 -0700 | [diff] [blame] | 477 | } |
| 478 | |
Adam Cohen | df2cc41 | 2011-04-27 16:56:57 -0700 | [diff] [blame] | 479 | protected boolean findAndSetEmptyCells(ShortcutInfo item) { |
| 480 | int[] emptyCell = new int[2]; |
| 481 | if (mContent.findCellForSpan(emptyCell, item.spanX, item.spanY)) { |
| 482 | item.cellX = emptyCell[0]; |
| 483 | item.cellY = emptyCell[1]; |
Adam Cohen | df2cc41 | 2011-04-27 16:56:57 -0700 | [diff] [blame] | 484 | return true; |
| 485 | } else { |
| 486 | return false; |
| 487 | } |
| 488 | } |
| 489 | |
Adam Cohen | c508b2d | 2011-06-28 14:41:44 -0700 | [diff] [blame] | 490 | protected boolean createAndAddShortcut(ShortcutInfo item) { |
Adam Cohen | df2cc41 | 2011-04-27 16:56:57 -0700 | [diff] [blame] | 491 | final TextView textView = |
Adam Cohen | e87b924 | 2011-06-29 14:01:26 -0700 | [diff] [blame] | 492 | (TextView) mInflater.inflate(R.layout.application, this, false); |
Adam Cohen | df2cc41 | 2011-04-27 16:56:57 -0700 | [diff] [blame] | 493 | textView.setCompoundDrawablesWithIntrinsicBounds(null, |
| 494 | new FastBitmapDrawable(item.getIcon(mIconCache)), null, null); |
| 495 | textView.setText(item.title); |
| 496 | textView.setTag(item); |
| 497 | |
| 498 | textView.setOnClickListener(this); |
| 499 | textView.setOnLongClickListener(this); |
| 500 | |
Adam Cohen | c508b2d | 2011-06-28 14:41:44 -0700 | [diff] [blame] | 501 | // We need to check here to verify that the given item's location isn't already occupied |
| 502 | // by another item. If it is, we need to find the next available slot and assign |
| 503 | // it that position. This is an issue when upgrading from the old Folders implementation |
| 504 | // which could contain an unlimited number of items. |
Adam Cohen | 0057bbc | 2011-08-12 18:30:51 -0700 | [diff] [blame] | 505 | if (mContent.getChildAt(item.cellX, item.cellY) != null || item.cellX < 0 || item.cellY < 0 |
| 506 | || item.cellX >= mContent.getCountX() || item.cellY >= mContent.getCountY()) { |
Adam Cohen | c508b2d | 2011-06-28 14:41:44 -0700 | [diff] [blame] | 507 | if (!findAndSetEmptyCells(item)) { |
| 508 | return false; |
| 509 | } |
| 510 | } |
| 511 | |
Adam Cohen | df2cc41 | 2011-04-27 16:56:57 -0700 | [diff] [blame] | 512 | CellLayout.LayoutParams lp = |
| 513 | new CellLayout.LayoutParams(item.cellX, item.cellY, item.spanX, item.spanY); |
| 514 | boolean insert = false; |
| 515 | mContent.addViewToCellLayout(textView, insert ? 0 : -1, (int)item.id, lp, true); |
Adam Cohen | c508b2d | 2011-06-28 14:41:44 -0700 | [diff] [blame] | 516 | return true; |
Adam Cohen | df2cc41 | 2011-04-27 16:56:57 -0700 | [diff] [blame] | 517 | } |
| 518 | |
Adam Cohen | cb3382b | 2011-05-24 14:07:08 -0700 | [diff] [blame] | 519 | public void onDragEnter(DragObject d) { |
Adam Cohen | bfbfd26 | 2011-06-13 16:55:12 -0700 | [diff] [blame] | 520 | mPreviousTargetCell[0] = -1; |
| 521 | mPreviousTargetCell[1] = -1; |
Adam Cohen | bfbfd26 | 2011-06-13 16:55:12 -0700 | [diff] [blame] | 522 | mOnExitAlarm.cancelAlarm(); |
| 523 | } |
| 524 | |
| 525 | OnAlarmListener mReorderAlarmListener = new OnAlarmListener() { |
| 526 | public void onAlarm(Alarm alarm) { |
| 527 | realTimeReorder(mEmptyCell, mTargetCell); |
| 528 | } |
| 529 | }; |
| 530 | |
| 531 | boolean readingOrderGreaterThan(int[] v1, int[] v2) { |
| 532 | if (v1[1] > v2[1] || (v1[1] == v2[1] && v1[0] > v2[0])) { |
| 533 | return true; |
| 534 | } else { |
| 535 | return false; |
| 536 | } |
| 537 | } |
| 538 | |
| 539 | private void realTimeReorder(int[] empty, int[] target) { |
| 540 | boolean wrap; |
| 541 | int startX; |
| 542 | int endX; |
| 543 | int startY; |
Adam Cohen | 76fc085 | 2011-06-17 13:26:23 -0700 | [diff] [blame] | 544 | int delay = 0; |
| 545 | float delayAmount = 30; |
Adam Cohen | bfbfd26 | 2011-06-13 16:55:12 -0700 | [diff] [blame] | 546 | if (readingOrderGreaterThan(target, empty)) { |
| 547 | wrap = empty[0] >= mContent.getCountX() - 1; |
| 548 | startY = wrap ? empty[1] + 1 : empty[1]; |
| 549 | for (int y = startY; y <= target[1]; y++) { |
| 550 | startX = y == empty[1] ? empty[0] + 1 : 0; |
| 551 | endX = y < target[1] ? mContent.getCountX() - 1 : target[0]; |
| 552 | for (int x = startX; x <= endX; x++) { |
| 553 | View v = mContent.getChildAt(x,y); |
| 554 | if (mContent.animateChildToPosition(v, empty[0], empty[1], |
Adam Cohen | 76fc085 | 2011-06-17 13:26:23 -0700 | [diff] [blame] | 555 | REORDER_ANIMATION_DURATION, delay)) { |
Adam Cohen | bfbfd26 | 2011-06-13 16:55:12 -0700 | [diff] [blame] | 556 | empty[0] = x; |
| 557 | empty[1] = y; |
Adam Cohen | 76fc085 | 2011-06-17 13:26:23 -0700 | [diff] [blame] | 558 | delay += delayAmount; |
| 559 | delayAmount *= 0.9; |
Adam Cohen | bfbfd26 | 2011-06-13 16:55:12 -0700 | [diff] [blame] | 560 | } |
| 561 | } |
| 562 | } |
| 563 | } else { |
| 564 | wrap = empty[0] == 0; |
| 565 | startY = wrap ? empty[1] - 1 : empty[1]; |
| 566 | for (int y = startY; y >= target[1]; y--) { |
| 567 | startX = y == empty[1] ? empty[0] - 1 : mContent.getCountX() - 1; |
| 568 | endX = y > target[1] ? 0 : target[0]; |
| 569 | for (int x = startX; x >= endX; x--) { |
| 570 | View v = mContent.getChildAt(x,y); |
| 571 | if (mContent.animateChildToPosition(v, empty[0], empty[1], |
Adam Cohen | 76fc085 | 2011-06-17 13:26:23 -0700 | [diff] [blame] | 572 | REORDER_ANIMATION_DURATION, delay)) { |
Adam Cohen | bfbfd26 | 2011-06-13 16:55:12 -0700 | [diff] [blame] | 573 | empty[0] = x; |
| 574 | empty[1] = y; |
Adam Cohen | 76fc085 | 2011-06-17 13:26:23 -0700 | [diff] [blame] | 575 | delay += delayAmount; |
| 576 | delayAmount *= 0.9; |
Adam Cohen | bfbfd26 | 2011-06-13 16:55:12 -0700 | [diff] [blame] | 577 | } |
| 578 | } |
| 579 | } |
| 580 | } |
Adam Cohen | df2cc41 | 2011-04-27 16:56:57 -0700 | [diff] [blame] | 581 | } |
| 582 | |
Adam Cohen | cb3382b | 2011-05-24 14:07:08 -0700 | [diff] [blame] | 583 | public void onDragOver(DragObject d) { |
Adam Cohen | bfbfd26 | 2011-06-13 16:55:12 -0700 | [diff] [blame] | 584 | float[] r = getDragViewVisualCenter(d.x, d.y, d.xOffset, d.yOffset, d.dragView, null); |
| 585 | mTargetCell = mContent.findNearestArea((int) r[0], (int) r[1], 1, 1, mTargetCell); |
| 586 | |
| 587 | if (mTargetCell[0] != mPreviousTargetCell[0] || mTargetCell[1] != mPreviousTargetCell[1]) { |
| 588 | mReorderAlarm.cancelAlarm(); |
| 589 | mReorderAlarm.setOnAlarmListener(mReorderAlarmListener); |
| 590 | mReorderAlarm.setAlarm(150); |
| 591 | mPreviousTargetCell[0] = mTargetCell[0]; |
| 592 | mPreviousTargetCell[1] = mTargetCell[1]; |
| 593 | } |
Adam Cohen | df2cc41 | 2011-04-27 16:56:57 -0700 | [diff] [blame] | 594 | } |
| 595 | |
Adam Cohen | bfbfd26 | 2011-06-13 16:55:12 -0700 | [diff] [blame] | 596 | // This is used to compute the visual center of the dragView. The idea is that |
| 597 | // the visual center represents the user's interpretation of where the item is, and hence |
| 598 | // is the appropriate point to use when determining drop location. |
| 599 | private float[] getDragViewVisualCenter(int x, int y, int xOffset, int yOffset, |
| 600 | DragView dragView, float[] recycle) { |
| 601 | float res[]; |
| 602 | if (recycle == null) { |
| 603 | res = new float[2]; |
| 604 | } else { |
| 605 | res = recycle; |
| 606 | } |
| 607 | |
| 608 | // These represent the visual top and left of drag view if a dragRect was provided. |
| 609 | // If a dragRect was not provided, then they correspond to the actual view left and |
| 610 | // top, as the dragRect is in that case taken to be the entire dragView. |
| 611 | // R.dimen.dragViewOffsetY. |
| 612 | int left = x - xOffset; |
| 613 | int top = y - yOffset; |
| 614 | |
| 615 | // In order to find the visual center, we shift by half the dragRect |
| 616 | res[0] = left + dragView.getDragRegion().width() / 2; |
| 617 | res[1] = top + dragView.getDragRegion().height() / 2; |
| 618 | |
| 619 | return res; |
| 620 | } |
| 621 | |
| 622 | OnAlarmListener mOnExitAlarmListener = new OnAlarmListener() { |
| 623 | public void onAlarm(Alarm alarm) { |
Adam Cohen | 3e8f811 | 2011-07-02 18:03:00 -0700 | [diff] [blame] | 624 | completeDragExit(); |
Adam Cohen | bfbfd26 | 2011-06-13 16:55:12 -0700 | [diff] [blame] | 625 | } |
| 626 | }; |
| 627 | |
Adam Cohen | 95bb800 | 2011-07-03 23:40:28 -0700 | [diff] [blame] | 628 | public void completeDragExit() { |
Adam Cohen | 3e8f811 | 2011-07-02 18:03:00 -0700 | [diff] [blame] | 629 | mLauncher.closeFolder(); |
| 630 | mCurrentDragInfo = null; |
| 631 | mCurrentDragView = null; |
| 632 | mSuppressOnAdd = false; |
| 633 | mRearrangeOnClose = true; |
| 634 | } |
| 635 | |
Adam Cohen | cb3382b | 2011-05-24 14:07:08 -0700 | [diff] [blame] | 636 | public void onDragExit(DragObject d) { |
Adam Cohen | bfbfd26 | 2011-06-13 16:55:12 -0700 | [diff] [blame] | 637 | // We only close the folder if this is a true drag exit, ie. not because a drop |
| 638 | // has occurred above the folder. |
| 639 | if (!d.dragComplete) { |
| 640 | mOnExitAlarm.setOnAlarmListener(mOnExitAlarmListener); |
| 641 | mOnExitAlarm.setAlarm(ON_EXIT_CLOSE_DELAY); |
| 642 | } |
| 643 | mReorderAlarm.cancelAlarm(); |
Adam Cohen | 2801caf | 2011-05-13 20:57:39 -0700 | [diff] [blame] | 644 | } |
| 645 | |
Adam Cohen | c0dcf59 | 2011-06-01 15:30:43 -0700 | [diff] [blame] | 646 | public void onDropCompleted(View target, DragObject d, boolean success) { |
Adam Cohen | 67bd9cc | 2011-07-29 14:07:04 -0700 | [diff] [blame] | 647 | if (success) { |
Adam Cohen | 05e0f40 | 2011-08-01 12:12:49 -0700 | [diff] [blame] | 648 | if (mDeleteFolderOnDropCompleted && !mItemAddedBackToSelfViaIcon) { |
Adam Cohen | 67bd9cc | 2011-07-29 14:07:04 -0700 | [diff] [blame] | 649 | replaceFolderWithFinalItem(); |
| 650 | } |
| 651 | } else { |
| 652 | // The drag failed, we need to return the item to the folder |
| 653 | mFolderIcon.onDrop(d); |
| 654 | |
| 655 | // We're going to trigger a "closeFolder" which may occur before this item has |
| 656 | // been added back to the folder -- this could cause the folder to be deleted |
| 657 | if (mOnExitAlarm.alarmPending()) { |
| 658 | mSuppressFolderDeletion = true; |
| 659 | } |
| 660 | } |
| 661 | |
| 662 | if (target != this) { |
| 663 | if (mOnExitAlarm.alarmPending()) { |
| 664 | mOnExitAlarm.cancelAlarm(); |
| 665 | completeDragExit(); |
| 666 | } |
| 667 | } |
| 668 | mDeleteFolderOnDropCompleted = false; |
| 669 | mDragInProgress = false; |
Adam Cohen | 05e0f40 | 2011-08-01 12:12:49 -0700 | [diff] [blame] | 670 | mItemAddedBackToSelfViaIcon = false; |
Adam Cohen | bfbfd26 | 2011-06-13 16:55:12 -0700 | [diff] [blame] | 671 | mCurrentDragInfo = null; |
| 672 | mCurrentDragView = null; |
| 673 | mSuppressOnAdd = false; |
Adam Cohen | 67bd9cc | 2011-07-29 14:07:04 -0700 | [diff] [blame] | 674 | } |
Adam Cohen | 228da5a | 2011-07-27 22:23:47 -0700 | [diff] [blame] | 675 | |
Adam Cohen | 67bd9cc | 2011-07-29 14:07:04 -0700 | [diff] [blame] | 676 | public void notifyDrop() { |
| 677 | if (mDragInProgress) { |
Adam Cohen | 05e0f40 | 2011-08-01 12:12:49 -0700 | [diff] [blame] | 678 | mItemAddedBackToSelfViaIcon = true; |
Adam Cohen | 76078c4 | 2011-06-09 15:06:52 -0700 | [diff] [blame] | 679 | } |
Adam Cohen | df2cc41 | 2011-04-27 16:56:57 -0700 | [diff] [blame] | 680 | } |
| 681 | |
| 682 | public boolean isDropEnabled() { |
| 683 | return true; |
| 684 | } |
| 685 | |
Adam Cohen | cb3382b | 2011-05-24 14:07:08 -0700 | [diff] [blame] | 686 | public DropTarget getDropTargetDelegate(DragObject d) { |
Adam Cohen | df2cc41 | 2011-04-27 16:56:57 -0700 | [diff] [blame] | 687 | return null; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 688 | } |
Adam Cohen | a9cf38f | 2011-05-02 15:36:58 -0700 | [diff] [blame] | 689 | |
Adam Cohen | 2801caf | 2011-05-13 20:57:39 -0700 | [diff] [blame] | 690 | private void setupContentDimension(int count) { |
| 691 | ArrayList<View> list = getItemsInReadingOrder(); |
| 692 | |
| 693 | int countX = mContent.getCountX(); |
| 694 | int countY = mContent.getCountY(); |
Adam Cohen | 7c69321 | 2011-05-18 15:26:57 -0700 | [diff] [blame] | 695 | boolean done = false; |
Adam Cohen | 2801caf | 2011-05-13 20:57:39 -0700 | [diff] [blame] | 696 | |
Adam Cohen | 7c69321 | 2011-05-18 15:26:57 -0700 | [diff] [blame] | 697 | while (!done) { |
| 698 | int oldCountX = countX; |
| 699 | int oldCountY = countY; |
| 700 | if (countX * countY < count) { |
| 701 | // Current grid is too small, expand it |
| 702 | if (countX <= countY && countX < mMaxCountX) { |
| 703 | countX++; |
| 704 | } else if (countY < mMaxCountY) { |
| 705 | countY++; |
| 706 | } |
| 707 | if (countY == 0) countY++; |
| 708 | } else if ((countY - 1) * countX >= count && countY >= countX) { |
| 709 | countY = Math.max(0, countY - 1); |
| 710 | } else if ((countX - 1) * countY >= count) { |
| 711 | countX = Math.max(0, countX - 1); |
Adam Cohen | 2801caf | 2011-05-13 20:57:39 -0700 | [diff] [blame] | 712 | } |
Adam Cohen | 7c69321 | 2011-05-18 15:26:57 -0700 | [diff] [blame] | 713 | done = countX == oldCountX && countY == oldCountY; |
Adam Cohen | 2801caf | 2011-05-13 20:57:39 -0700 | [diff] [blame] | 714 | } |
Adam Cohen | 7c69321 | 2011-05-18 15:26:57 -0700 | [diff] [blame] | 715 | mContent.setGridSize(countX, countY); |
Adam Cohen | 2801caf | 2011-05-13 20:57:39 -0700 | [diff] [blame] | 716 | arrangeChildren(list); |
| 717 | } |
| 718 | |
| 719 | public boolean isFull() { |
| 720 | return getItemCount() >= mMaxCountX * mMaxCountY; |
| 721 | } |
| 722 | |
| 723 | private void centerAboutIcon() { |
Adam Cohen | 8e776a6 | 2011-06-28 18:10:06 -0700 | [diff] [blame] | 724 | DragLayer.LayoutParams lp = (DragLayer.LayoutParams) getLayoutParams(); |
Adam Cohen | 2801caf | 2011-05-13 20:57:39 -0700 | [diff] [blame] | 725 | |
| 726 | int width = getPaddingLeft() + getPaddingRight() + mContent.getDesiredWidth(); |
Adam Cohen | f4bb1cd | 2011-07-22 14:36:03 -0700 | [diff] [blame] | 727 | int height = getPaddingTop() + getPaddingBottom() + mContent.getDesiredHeight() |
| 728 | + mFolderNameHeight; |
Adam Cohen | 8e776a6 | 2011-06-28 18:10:06 -0700 | [diff] [blame] | 729 | DragLayer parent = (DragLayer) mLauncher.findViewById(R.id.drag_layer); |
Adam Cohen | 2801caf | 2011-05-13 20:57:39 -0700 | [diff] [blame] | 730 | |
Adam Cohen | 8e776a6 | 2011-06-28 18:10:06 -0700 | [diff] [blame] | 731 | parent.getDescendantRectRelativeToSelf(mFolderIcon, mTempRect); |
| 732 | |
| 733 | int centerX = mTempRect.centerX(); |
| 734 | int centerY = mTempRect.centerY(); |
Adam Cohen | 2801caf | 2011-05-13 20:57:39 -0700 | [diff] [blame] | 735 | int centeredLeft = centerX - width / 2; |
| 736 | int centeredTop = centerY - height / 2; |
| 737 | |
Adam Cohen | 35e7e64 | 2011-07-17 14:47:18 -0700 | [diff] [blame] | 738 | // We first fetch the currently visible CellLayoutChildren |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 739 | CellLayout currentPage = mLauncher.getWorkspace().getCurrentDropLayout(); |
Adam Cohen | 35e7e64 | 2011-07-17 14:47:18 -0700 | [diff] [blame] | 740 | CellLayoutChildren boundingLayout = currentPage.getChildrenLayout(); |
| 741 | Rect bounds = new Rect(); |
| 742 | parent.getDescendantRectRelativeToSelf(boundingLayout, bounds); |
Adam Cohen | 2801caf | 2011-05-13 20:57:39 -0700 | [diff] [blame] | 743 | |
Adam Cohen | 35e7e64 | 2011-07-17 14:47:18 -0700 | [diff] [blame] | 744 | // We need to bound the folder to the currently visible CellLayoutChildren |
| 745 | int left = Math.min(Math.max(bounds.left, centeredLeft), |
| 746 | bounds.left + bounds.width() - width); |
| 747 | int top = Math.min(Math.max(bounds.top, centeredTop), |
| 748 | bounds.top + bounds.height() - height); |
| 749 | // If the folder doesn't fit within the bounds, center it about the desired bounds |
| 750 | if (width >= bounds.width()) { |
| 751 | left = bounds.left + (bounds.width() - width) / 2; |
Adam Cohen | 0e4857c | 2011-06-30 17:05:14 -0700 | [diff] [blame] | 752 | } |
Adam Cohen | 35e7e64 | 2011-07-17 14:47:18 -0700 | [diff] [blame] | 753 | if (height >= bounds.height()) { |
| 754 | top = bounds.top + (bounds.height() - height) / 2; |
Adam Cohen | 0e4857c | 2011-06-30 17:05:14 -0700 | [diff] [blame] | 755 | } |
Adam Cohen | 2801caf | 2011-05-13 20:57:39 -0700 | [diff] [blame] | 756 | |
| 757 | int folderPivotX = width / 2 + (centeredLeft - left); |
| 758 | int folderPivotY = height / 2 + (centeredTop - top); |
| 759 | setPivotX(folderPivotX); |
| 760 | setPivotY(folderPivotY); |
| 761 | int folderIconPivotX = (int) (mFolderIcon.getMeasuredWidth() * |
| 762 | (1.0f * folderPivotX / width)); |
| 763 | int folderIconPivotY = (int) (mFolderIcon.getMeasuredHeight() * |
| 764 | (1.0f * folderPivotY / height)); |
| 765 | mFolderIcon.setPivotX(folderIconPivotX); |
| 766 | mFolderIcon.setPivotY(folderIconPivotY); |
| 767 | |
| 768 | if (mMode == PARTIAL_GROW) { |
| 769 | lp.width = width; |
| 770 | lp.height = height; |
| 771 | lp.x = left; |
| 772 | lp.y = top; |
| 773 | } else { |
| 774 | mNewSize.set(left, top, left + width, top + height); |
| 775 | } |
| 776 | } |
| 777 | |
| 778 | private void setupContentForNumItems(int count) { |
Adam Cohen | 2801caf | 2011-05-13 20:57:39 -0700 | [diff] [blame] | 779 | setupContentDimension(count); |
| 780 | |
Adam Cohen | 8e776a6 | 2011-06-28 18:10:06 -0700 | [diff] [blame] | 781 | DragLayer.LayoutParams lp = (DragLayer.LayoutParams) getLayoutParams(); |
Adam Cohen | 2801caf | 2011-05-13 20:57:39 -0700 | [diff] [blame] | 782 | if (lp == null) { |
Adam Cohen | 8e776a6 | 2011-06-28 18:10:06 -0700 | [diff] [blame] | 783 | lp = new DragLayer.LayoutParams(0, 0); |
| 784 | lp.customPosition = true; |
Adam Cohen | 2801caf | 2011-05-13 20:57:39 -0700 | [diff] [blame] | 785 | setLayoutParams(lp); |
| 786 | } |
| 787 | centerAboutIcon(); |
| 788 | } |
| 789 | |
Adam Cohen | 234c4cd | 2011-07-17 21:03:04 -0700 | [diff] [blame] | 790 | protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { |
| 791 | int width = getPaddingLeft() + getPaddingRight() + mContent.getDesiredWidth(); |
Adam Cohen | f4bb1cd | 2011-07-22 14:36:03 -0700 | [diff] [blame] | 792 | int height = getPaddingTop() + getPaddingBottom() + mContent.getDesiredHeight() |
| 793 | + mFolderNameHeight; |
Adam Cohen | 234c4cd | 2011-07-17 21:03:04 -0700 | [diff] [blame] | 794 | |
| 795 | int contentWidthSpec = MeasureSpec.makeMeasureSpec(mContent.getDesiredWidth(), |
| 796 | MeasureSpec.EXACTLY); |
| 797 | int contentHeightSpec = MeasureSpec.makeMeasureSpec(mContent.getDesiredHeight(), |
| 798 | MeasureSpec.EXACTLY); |
| 799 | mContent.measure(contentWidthSpec, contentHeightSpec); |
| 800 | |
| 801 | mFolderName.measure(contentWidthSpec, |
| 802 | MeasureSpec.makeMeasureSpec(mFolderNameHeight, MeasureSpec.EXACTLY)); |
| 803 | setMeasuredDimension(width, height); |
| 804 | } |
| 805 | |
Adam Cohen | 2801caf | 2011-05-13 20:57:39 -0700 | [diff] [blame] | 806 | private void arrangeChildren(ArrayList<View> list) { |
| 807 | int[] vacant = new int[2]; |
| 808 | if (list == null) { |
| 809 | list = getItemsInReadingOrder(); |
| 810 | } |
| 811 | mContent.removeAllViews(); |
| 812 | |
| 813 | for (int i = 0; i < list.size(); i++) { |
| 814 | View v = list.get(i); |
| 815 | mContent.getVacantCell(vacant, 1, 1); |
| 816 | CellLayout.LayoutParams lp = (CellLayout.LayoutParams) v.getLayoutParams(); |
| 817 | lp.cellX = vacant[0]; |
| 818 | lp.cellY = vacant[1]; |
| 819 | ItemInfo info = (ItemInfo) v.getTag(); |
Adam Cohen | 2792a33 | 2011-09-26 21:09:47 -0700 | [diff] [blame] | 820 | if (info.cellX != vacant[0] || info.cellY != vacant[1]) { |
| 821 | info.cellX = vacant[0]; |
| 822 | info.cellY = vacant[1]; |
| 823 | LauncherModel.addOrMoveItemInDatabase(mLauncher, info, mInfo.id, 0, |
| 824 | info.cellX, info.cellY); |
| 825 | } |
Adam Cohen | 2801caf | 2011-05-13 20:57:39 -0700 | [diff] [blame] | 826 | boolean insert = false; |
| 827 | mContent.addViewToCellLayout(v, insert ? 0 : -1, (int)info.id, lp, true); |
| 828 | } |
Adam Cohen | 7c69321 | 2011-05-18 15:26:57 -0700 | [diff] [blame] | 829 | mItemsInvalidated = true; |
Adam Cohen | 2801caf | 2011-05-13 20:57:39 -0700 | [diff] [blame] | 830 | } |
| 831 | |
Adam Cohen | a9cf38f | 2011-05-02 15:36:58 -0700 | [diff] [blame] | 832 | public int getItemCount() { |
| 833 | return mContent.getChildrenLayout().getChildCount(); |
| 834 | } |
| 835 | |
| 836 | public View getItemAt(int index) { |
| 837 | return mContent.getChildrenLayout().getChildAt(index); |
| 838 | } |
| 839 | |
Adam Cohen | 2801caf | 2011-05-13 20:57:39 -0700 | [diff] [blame] | 840 | private void onCloseComplete() { |
Adam Cohen | 05e0f40 | 2011-08-01 12:12:49 -0700 | [diff] [blame] | 841 | DragLayer parent = (DragLayer) getParent(); |
Adam Cohen | 4554ee1 | 2011-08-03 16:13:21 -0700 | [diff] [blame] | 842 | parent.removeView(this); |
| 843 | mDragController.removeDropTarget((DropTarget) this); |
Adam Cohen | 05e0f40 | 2011-08-01 12:12:49 -0700 | [diff] [blame] | 844 | clearFocus(); |
| 845 | |
Adam Cohen | 2801caf | 2011-05-13 20:57:39 -0700 | [diff] [blame] | 846 | if (mRearrangeOnClose) { |
| 847 | setupContentForNumItems(getItemCount()); |
| 848 | mRearrangeOnClose = false; |
| 849 | } |
Adam Cohen | afb01ee | 2011-06-23 15:38:03 -0700 | [diff] [blame] | 850 | if (getItemCount() <= 1) { |
Adam Cohen | 67bd9cc | 2011-07-29 14:07:04 -0700 | [diff] [blame] | 851 | if (!mDragInProgress && !mSuppressFolderDeletion) { |
| 852 | replaceFolderWithFinalItem(); |
| 853 | } else if (mDragInProgress) { |
| 854 | mDeleteFolderOnDropCompleted = true; |
| 855 | } |
Adam Cohen | afb01ee | 2011-06-23 15:38:03 -0700 | [diff] [blame] | 856 | } |
Adam Cohen | 67bd9cc | 2011-07-29 14:07:04 -0700 | [diff] [blame] | 857 | mSuppressFolderDeletion = false; |
Adam Cohen | afb01ee | 2011-06-23 15:38:03 -0700 | [diff] [blame] | 858 | } |
| 859 | |
| 860 | private void replaceFolderWithFinalItem() { |
| 861 | ItemInfo finalItem = null; |
| 862 | |
| 863 | if (getItemCount() == 1) { |
| 864 | finalItem = mInfo.contents.get(0); |
| 865 | } |
| 866 | |
| 867 | // Remove the folder completely |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 868 | CellLayout cellLayout = mLauncher.getCellLayout(mInfo.container, mInfo.screen); |
Adam Cohen | afb01ee | 2011-06-23 15:38:03 -0700 | [diff] [blame] | 869 | cellLayout.removeView(mFolderIcon); |
| 870 | if (mFolderIcon instanceof DropTarget) { |
| 871 | mDragController.removeDropTarget((DropTarget) mFolderIcon); |
| 872 | } |
| 873 | mLauncher.removeFolder(mInfo); |
| 874 | |
| 875 | if (finalItem != null) { |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 876 | LauncherModel.addOrMoveItemInDatabase(mLauncher, finalItem, mInfo.container, |
| 877 | mInfo.screen, mInfo.cellX, mInfo.cellY); |
Adam Cohen | afb01ee | 2011-06-23 15:38:03 -0700 | [diff] [blame] | 878 | } |
Adam Cohen | 716b51e | 2011-06-30 12:09:54 -0700 | [diff] [blame] | 879 | LauncherModel.deleteItemFromDatabase(mLauncher, mInfo); |
Adam Cohen | afb01ee | 2011-06-23 15:38:03 -0700 | [diff] [blame] | 880 | |
| 881 | // Add the last remaining child to the workspace in place of the folder |
| 882 | if (finalItem != null) { |
| 883 | View child = mLauncher.createShortcut(R.layout.application, cellLayout, |
| 884 | (ShortcutInfo) finalItem); |
| 885 | |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 886 | mLauncher.getWorkspace().addInScreen(child, mInfo.container, mInfo.screen, mInfo.cellX, |
| 887 | mInfo.cellY, mInfo.spanX, mInfo.spanY); |
Adam Cohen | afb01ee | 2011-06-23 15:38:03 -0700 | [diff] [blame] | 888 | } |
Adam Cohen | 2801caf | 2011-05-13 20:57:39 -0700 | [diff] [blame] | 889 | } |
| 890 | |
Adam Cohen | bfbfd26 | 2011-06-13 16:55:12 -0700 | [diff] [blame] | 891 | public void onDrop(DragObject d) { |
| 892 | ShortcutInfo item; |
| 893 | if (d.dragInfo instanceof ApplicationInfo) { |
| 894 | // Came from all apps -- make a copy |
| 895 | item = ((ApplicationInfo) d.dragInfo).makeShortcut(); |
| 896 | item.spanX = 1; |
| 897 | item.spanY = 1; |
| 898 | } else { |
| 899 | item = (ShortcutInfo) d.dragInfo; |
| 900 | } |
Adam Cohen | 05e0f40 | 2011-08-01 12:12:49 -0700 | [diff] [blame] | 901 | // Dragged from self onto self, currently this is the only path possible, however |
| 902 | // we keep this as a distinct code path. |
Adam Cohen | bfbfd26 | 2011-06-13 16:55:12 -0700 | [diff] [blame] | 903 | if (item == mCurrentDragInfo) { |
| 904 | ShortcutInfo si = (ShortcutInfo) mCurrentDragView.getTag(); |
| 905 | CellLayout.LayoutParams lp = (CellLayout.LayoutParams) mCurrentDragView.getLayoutParams(); |
| 906 | si.cellX = lp.cellX = mEmptyCell[0]; |
| 907 | si.cellX = lp.cellY = mEmptyCell[1]; |
| 908 | mContent.addViewToCellLayout(mCurrentDragView, -1, (int)item.id, lp, true); |
Adam Cohen | fc53cd2 | 2011-07-20 15:45:11 -0700 | [diff] [blame] | 909 | if (d.dragView.hasDrawn()) { |
| 910 | mLauncher.getDragLayer().animateViewIntoPosition(d.dragView, mCurrentDragView); |
| 911 | } else { |
| 912 | mCurrentDragView.setVisibility(VISIBLE); |
| 913 | } |
Adam Cohen | e9166b2 | 2011-07-08 17:11:11 -0700 | [diff] [blame] | 914 | mItemsInvalidated = true; |
Adam Cohen | bfbfd26 | 2011-06-13 16:55:12 -0700 | [diff] [blame] | 915 | setupContentDimension(getItemCount()); |
Adam Cohen | 716b51e | 2011-06-30 12:09:54 -0700 | [diff] [blame] | 916 | mSuppressOnAdd = true; |
Adam Cohen | bfbfd26 | 2011-06-13 16:55:12 -0700 | [diff] [blame] | 917 | } |
| 918 | mInfo.add(item); |
| 919 | } |
| 920 | |
| 921 | public void onAdd(ShortcutInfo item) { |
| 922 | mItemsInvalidated = true; |
Adam Cohen | 05e0f40 | 2011-08-01 12:12:49 -0700 | [diff] [blame] | 923 | // If the item was dropped onto this open folder, we have done the work associated |
| 924 | // 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] | 925 | if (mSuppressOnAdd) return; |
| 926 | if (!findAndSetEmptyCells(item)) { |
| 927 | // The current layout is full, can we expand it? |
| 928 | setupContentForNumItems(getItemCount() + 1); |
| 929 | findAndSetEmptyCells(item); |
| 930 | } |
| 931 | createAndAddShortcut(item); |
| 932 | LauncherModel.addOrMoveItemInDatabase( |
| 933 | mLauncher, item, mInfo.id, 0, item.cellX, item.cellY); |
| 934 | } |
| 935 | |
Adam Cohen | a9cf38f | 2011-05-02 15:36:58 -0700 | [diff] [blame] | 936 | public void onRemove(ShortcutInfo item) { |
Adam Cohen | 7c69321 | 2011-05-18 15:26:57 -0700 | [diff] [blame] | 937 | mItemsInvalidated = true; |
Adam Cohen | 05e0f40 | 2011-08-01 12:12:49 -0700 | [diff] [blame] | 938 | // If this item is being dragged from this open folder, we have already handled |
| 939 | // 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] | 940 | if (item == mCurrentDragInfo) return; |
Adam Cohen | df1e4e8 | 2011-06-24 15:57:39 -0700 | [diff] [blame] | 941 | View v = getViewForInfo(item); |
Adam Cohen | a9cf38f | 2011-05-02 15:36:58 -0700 | [diff] [blame] | 942 | mContent.removeView(v); |
Adam Cohen | 2801caf | 2011-05-13 20:57:39 -0700 | [diff] [blame] | 943 | if (mState == STATE_ANIMATING) { |
| 944 | mRearrangeOnClose = true; |
| 945 | } else { |
| 946 | setupContentForNumItems(getItemCount()); |
| 947 | } |
Adam Cohen | afb01ee | 2011-06-23 15:38:03 -0700 | [diff] [blame] | 948 | if (getItemCount() <= 1) { |
| 949 | replaceFolderWithFinalItem(); |
| 950 | } |
Adam Cohen | a9cf38f | 2011-05-02 15:36:58 -0700 | [diff] [blame] | 951 | } |
Adam Cohen | 7c69321 | 2011-05-18 15:26:57 -0700 | [diff] [blame] | 952 | |
Adam Cohen | df1e4e8 | 2011-06-24 15:57:39 -0700 | [diff] [blame] | 953 | private View getViewForInfo(ShortcutInfo item) { |
| 954 | for (int j = 0; j < mContent.getCountY(); j++) { |
| 955 | for (int i = 0; i < mContent.getCountX(); i++) { |
| 956 | View v = mContent.getChildAt(i, j); |
| 957 | if (v.getTag() == item) { |
| 958 | return v; |
| 959 | } |
| 960 | } |
| 961 | } |
| 962 | return null; |
| 963 | } |
| 964 | |
Adam Cohen | 76078c4 | 2011-06-09 15:06:52 -0700 | [diff] [blame] | 965 | public void onItemsChanged() { |
| 966 | } |
Adam Cohen | 76fc085 | 2011-06-17 13:26:23 -0700 | [diff] [blame] | 967 | public void onTitleChanged(CharSequence title) { |
| 968 | } |
Adam Cohen | 76078c4 | 2011-06-09 15:06:52 -0700 | [diff] [blame] | 969 | |
Adam Cohen | 7c69321 | 2011-05-18 15:26:57 -0700 | [diff] [blame] | 970 | public ArrayList<View> getItemsInReadingOrder() { |
Adam Cohen | 76078c4 | 2011-06-09 15:06:52 -0700 | [diff] [blame] | 971 | return getItemsInReadingOrder(true); |
| 972 | } |
| 973 | |
| 974 | public ArrayList<View> getItemsInReadingOrder(boolean includeCurrentDragItem) { |
Adam Cohen | 7c69321 | 2011-05-18 15:26:57 -0700 | [diff] [blame] | 975 | if (mItemsInvalidated) { |
| 976 | mItemsInReadingOrder.clear(); |
| 977 | for (int j = 0; j < mContent.getCountY(); j++) { |
| 978 | for (int i = 0; i < mContent.getCountX(); i++) { |
| 979 | View v = mContent.getChildAt(i, j); |
| 980 | if (v != null) { |
Adam Cohen | 76078c4 | 2011-06-09 15:06:52 -0700 | [diff] [blame] | 981 | ShortcutInfo info = (ShortcutInfo) v.getTag(); |
| 982 | if (info != mCurrentDragInfo || includeCurrentDragItem) { |
| 983 | mItemsInReadingOrder.add(v); |
| 984 | } |
Adam Cohen | 7c69321 | 2011-05-18 15:26:57 -0700 | [diff] [blame] | 985 | } |
| 986 | } |
| 987 | } |
| 988 | mItemsInvalidated = false; |
| 989 | } |
| 990 | return mItemsInReadingOrder; |
| 991 | } |
Adam Cohen | 8dfcba4 | 2011-07-07 16:38:18 -0700 | [diff] [blame] | 992 | |
| 993 | public void getLocationInDragLayer(int[] loc) { |
| 994 | mLauncher.getDragLayer().getLocationInDragLayer(this, loc); |
| 995 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 996 | } |