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; |
Adam Cohen | df2cc41 | 2011-04-27 16:56:57 -0700 | [diff] [blame] | 408 | } |
| 409 | }); |
Adam Cohen | 2801caf | 2011-05-13 20:57:39 -0700 | [diff] [blame] | 410 | oa.setDuration(mExpandDuration); |
| 411 | oa.start(); |
Adam Cohen | df2cc41 | 2011-04-27 16:56:57 -0700 | [diff] [blame] | 412 | } |
| 413 | |
| 414 | public void animateClosed() { |
Adam Cohen | 8e776a6 | 2011-06-28 18:10:06 -0700 | [diff] [blame] | 415 | if (!(getParent() instanceof DragLayer)) return; |
Adam Cohen | df2cc41 | 2011-04-27 16:56:57 -0700 | [diff] [blame] | 416 | |
Adam Cohen | 2801caf | 2011-05-13 20:57:39 -0700 | [diff] [blame] | 417 | ObjectAnimator oa; |
Adam Cohen | 2801caf | 2011-05-13 20:57:39 -0700 | [diff] [blame] | 418 | if (mMode == PARTIAL_GROW) { |
| 419 | PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0); |
| 420 | PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 0.9f); |
| 421 | PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 0.9f); |
| 422 | oa = ObjectAnimator.ofPropertyValuesHolder(this, alpha, scaleX, scaleY); |
| 423 | } else { |
Adam Cohen | 8e776a6 | 2011-06-28 18:10:06 -0700 | [diff] [blame] | 424 | DragLayer.LayoutParams lp = (DragLayer.LayoutParams) getLayoutParams(); |
Adam Cohen | df2cc41 | 2011-04-27 16:56:57 -0700 | [diff] [blame] | 425 | |
Adam Cohen | 3e8f811 | 2011-07-02 18:03:00 -0700 | [diff] [blame] | 426 | PropertyValuesHolder width = PropertyValuesHolder.ofInt("width", mIconRect.width()); |
| 427 | PropertyValuesHolder height = PropertyValuesHolder.ofInt("height", mIconRect.height()); |
| 428 | PropertyValuesHolder x = PropertyValuesHolder.ofInt("x", mIconRect.left); |
| 429 | PropertyValuesHolder y = PropertyValuesHolder.ofInt("y", mIconRect.top); |
Adam Cohen | 2801caf | 2011-05-13 20:57:39 -0700 | [diff] [blame] | 430 | oa = ObjectAnimator.ofPropertyValuesHolder(lp, width, height, x, y); |
| 431 | oa.addUpdateListener(new AnimatorUpdateListener() { |
| 432 | public void onAnimationUpdate(ValueAnimator animation) { |
| 433 | requestLayout(); |
| 434 | } |
| 435 | }); |
Adam Cohen | df2cc41 | 2011-04-27 16:56:57 -0700 | [diff] [blame] | 436 | |
Adam Cohen | 2801caf | 2011-05-13 20:57:39 -0700 | [diff] [blame] | 437 | PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0f); |
| 438 | ObjectAnimator alphaOa = ObjectAnimator.ofPropertyValuesHolder(mContent, alpha); |
| 439 | alphaOa.setDuration(mExpandDuration); |
| 440 | alphaOa.setInterpolator(new DecelerateInterpolator(2.0f)); |
| 441 | alphaOa.start(); |
| 442 | } |
Adam Cohen | df2cc41 | 2011-04-27 16:56:57 -0700 | [diff] [blame] | 443 | |
Adam Cohen | 2801caf | 2011-05-13 20:57:39 -0700 | [diff] [blame] | 444 | oa.addListener(new AnimatorListenerAdapter() { |
Adam Cohen | df2cc41 | 2011-04-27 16:56:57 -0700 | [diff] [blame] | 445 | @Override |
| 446 | public void onAnimationEnd(Animator animation) { |
Adam Cohen | 2801caf | 2011-05-13 20:57:39 -0700 | [diff] [blame] | 447 | onCloseComplete(); |
Adam Cohen | 2801caf | 2011-05-13 20:57:39 -0700 | [diff] [blame] | 448 | mState = STATE_SMALL; |
Adam Cohen | df2cc41 | 2011-04-27 16:56:57 -0700 | [diff] [blame] | 449 | } |
| 450 | @Override |
| 451 | public void onAnimationStart(Animator animation) { |
| 452 | mState = STATE_ANIMATING; |
| 453 | } |
| 454 | }); |
Adam Cohen | 2801caf | 2011-05-13 20:57:39 -0700 | [diff] [blame] | 455 | oa.setDuration(mExpandDuration); |
| 456 | oa.start(); |
Adam Cohen | df2cc41 | 2011-04-27 16:56:57 -0700 | [diff] [blame] | 457 | } |
| 458 | |
| 459 | void notifyDataSetChanged() { |
| 460 | // recreate all the children if the data set changes under us. We may want to do this more |
| 461 | // intelligently (ie just removing the views that should no longer exist) |
| 462 | mContent.removeAllViewsInLayout(); |
| 463 | bind(mInfo); |
| 464 | } |
| 465 | |
Adam Cohen | cb3382b | 2011-05-24 14:07:08 -0700 | [diff] [blame] | 466 | public boolean acceptDrop(DragObject d) { |
| 467 | final ItemInfo item = (ItemInfo) d.dragInfo; |
Adam Cohen | df2cc41 | 2011-04-27 16:56:57 -0700 | [diff] [blame] | 468 | final int itemType = item.itemType; |
Adam Cohen | 2801caf | 2011-05-13 20:57:39 -0700 | [diff] [blame] | 469 | return ((itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION || |
| 470 | itemType == LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT) && |
| 471 | !isFull()); |
Adam Cohen | df2cc41 | 2011-04-27 16:56:57 -0700 | [diff] [blame] | 472 | } |
| 473 | |
Adam Cohen | df2cc41 | 2011-04-27 16:56:57 -0700 | [diff] [blame] | 474 | protected boolean findAndSetEmptyCells(ShortcutInfo item) { |
| 475 | int[] emptyCell = new int[2]; |
| 476 | if (mContent.findCellForSpan(emptyCell, item.spanX, item.spanY)) { |
| 477 | item.cellX = emptyCell[0]; |
| 478 | item.cellY = emptyCell[1]; |
Adam Cohen | df2cc41 | 2011-04-27 16:56:57 -0700 | [diff] [blame] | 479 | return true; |
| 480 | } else { |
| 481 | return false; |
| 482 | } |
| 483 | } |
| 484 | |
Adam Cohen | c508b2d | 2011-06-28 14:41:44 -0700 | [diff] [blame] | 485 | protected boolean createAndAddShortcut(ShortcutInfo item) { |
Adam Cohen | df2cc41 | 2011-04-27 16:56:57 -0700 | [diff] [blame] | 486 | final TextView textView = |
Adam Cohen | e87b924 | 2011-06-29 14:01:26 -0700 | [diff] [blame] | 487 | (TextView) mInflater.inflate(R.layout.application, this, false); |
Adam Cohen | df2cc41 | 2011-04-27 16:56:57 -0700 | [diff] [blame] | 488 | textView.setCompoundDrawablesWithIntrinsicBounds(null, |
| 489 | new FastBitmapDrawable(item.getIcon(mIconCache)), null, null); |
| 490 | textView.setText(item.title); |
| 491 | textView.setTag(item); |
| 492 | |
| 493 | textView.setOnClickListener(this); |
| 494 | textView.setOnLongClickListener(this); |
| 495 | |
Adam Cohen | c508b2d | 2011-06-28 14:41:44 -0700 | [diff] [blame] | 496 | // We need to check here to verify that the given item's location isn't already occupied |
| 497 | // by another item. If it is, we need to find the next available slot and assign |
| 498 | // it that position. This is an issue when upgrading from the old Folders implementation |
| 499 | // which could contain an unlimited number of items. |
Adam Cohen | 0057bbc | 2011-08-12 18:30:51 -0700 | [diff] [blame] | 500 | if (mContent.getChildAt(item.cellX, item.cellY) != null || item.cellX < 0 || item.cellY < 0 |
| 501 | || item.cellX >= mContent.getCountX() || item.cellY >= mContent.getCountY()) { |
Adam Cohen | c508b2d | 2011-06-28 14:41:44 -0700 | [diff] [blame] | 502 | if (!findAndSetEmptyCells(item)) { |
| 503 | return false; |
| 504 | } |
| 505 | } |
| 506 | |
Adam Cohen | df2cc41 | 2011-04-27 16:56:57 -0700 | [diff] [blame] | 507 | CellLayout.LayoutParams lp = |
| 508 | new CellLayout.LayoutParams(item.cellX, item.cellY, item.spanX, item.spanY); |
| 509 | boolean insert = false; |
| 510 | mContent.addViewToCellLayout(textView, insert ? 0 : -1, (int)item.id, lp, true); |
Adam Cohen | c508b2d | 2011-06-28 14:41:44 -0700 | [diff] [blame] | 511 | return true; |
Adam Cohen | df2cc41 | 2011-04-27 16:56:57 -0700 | [diff] [blame] | 512 | } |
| 513 | |
Adam Cohen | cb3382b | 2011-05-24 14:07:08 -0700 | [diff] [blame] | 514 | public void onDragEnter(DragObject d) { |
Adam Cohen | bfbfd26 | 2011-06-13 16:55:12 -0700 | [diff] [blame] | 515 | mPreviousTargetCell[0] = -1; |
| 516 | mPreviousTargetCell[1] = -1; |
Adam Cohen | bfbfd26 | 2011-06-13 16:55:12 -0700 | [diff] [blame] | 517 | mOnExitAlarm.cancelAlarm(); |
| 518 | } |
| 519 | |
| 520 | OnAlarmListener mReorderAlarmListener = new OnAlarmListener() { |
| 521 | public void onAlarm(Alarm alarm) { |
| 522 | realTimeReorder(mEmptyCell, mTargetCell); |
| 523 | } |
| 524 | }; |
| 525 | |
| 526 | boolean readingOrderGreaterThan(int[] v1, int[] v2) { |
| 527 | if (v1[1] > v2[1] || (v1[1] == v2[1] && v1[0] > v2[0])) { |
| 528 | return true; |
| 529 | } else { |
| 530 | return false; |
| 531 | } |
| 532 | } |
| 533 | |
| 534 | private void realTimeReorder(int[] empty, int[] target) { |
| 535 | boolean wrap; |
| 536 | int startX; |
| 537 | int endX; |
| 538 | int startY; |
Adam Cohen | 76fc085 | 2011-06-17 13:26:23 -0700 | [diff] [blame] | 539 | int delay = 0; |
| 540 | float delayAmount = 30; |
Adam Cohen | bfbfd26 | 2011-06-13 16:55:12 -0700 | [diff] [blame] | 541 | if (readingOrderGreaterThan(target, empty)) { |
| 542 | wrap = empty[0] >= mContent.getCountX() - 1; |
| 543 | startY = wrap ? empty[1] + 1 : empty[1]; |
| 544 | for (int y = startY; y <= target[1]; y++) { |
| 545 | startX = y == empty[1] ? empty[0] + 1 : 0; |
| 546 | endX = y < target[1] ? mContent.getCountX() - 1 : target[0]; |
| 547 | for (int x = startX; x <= endX; x++) { |
| 548 | View v = mContent.getChildAt(x,y); |
| 549 | if (mContent.animateChildToPosition(v, empty[0], empty[1], |
Adam Cohen | 76fc085 | 2011-06-17 13:26:23 -0700 | [diff] [blame] | 550 | REORDER_ANIMATION_DURATION, delay)) { |
Adam Cohen | bfbfd26 | 2011-06-13 16:55:12 -0700 | [diff] [blame] | 551 | empty[0] = x; |
| 552 | empty[1] = y; |
Adam Cohen | 76fc085 | 2011-06-17 13:26:23 -0700 | [diff] [blame] | 553 | delay += delayAmount; |
| 554 | delayAmount *= 0.9; |
Adam Cohen | bfbfd26 | 2011-06-13 16:55:12 -0700 | [diff] [blame] | 555 | } |
| 556 | } |
| 557 | } |
| 558 | } else { |
| 559 | wrap = empty[0] == 0; |
| 560 | startY = wrap ? empty[1] - 1 : empty[1]; |
| 561 | for (int y = startY; y >= target[1]; y--) { |
| 562 | startX = y == empty[1] ? empty[0] - 1 : mContent.getCountX() - 1; |
| 563 | endX = y > target[1] ? 0 : target[0]; |
| 564 | for (int x = startX; x >= endX; x--) { |
| 565 | View v = mContent.getChildAt(x,y); |
| 566 | if (mContent.animateChildToPosition(v, empty[0], empty[1], |
Adam Cohen | 76fc085 | 2011-06-17 13:26:23 -0700 | [diff] [blame] | 567 | REORDER_ANIMATION_DURATION, delay)) { |
Adam Cohen | bfbfd26 | 2011-06-13 16:55:12 -0700 | [diff] [blame] | 568 | empty[0] = x; |
| 569 | empty[1] = y; |
Adam Cohen | 76fc085 | 2011-06-17 13:26:23 -0700 | [diff] [blame] | 570 | delay += delayAmount; |
| 571 | delayAmount *= 0.9; |
Adam Cohen | bfbfd26 | 2011-06-13 16:55:12 -0700 | [diff] [blame] | 572 | } |
| 573 | } |
| 574 | } |
| 575 | } |
Adam Cohen | df2cc41 | 2011-04-27 16:56:57 -0700 | [diff] [blame] | 576 | } |
| 577 | |
Adam Cohen | cb3382b | 2011-05-24 14:07:08 -0700 | [diff] [blame] | 578 | public void onDragOver(DragObject d) { |
Adam Cohen | bfbfd26 | 2011-06-13 16:55:12 -0700 | [diff] [blame] | 579 | float[] r = getDragViewVisualCenter(d.x, d.y, d.xOffset, d.yOffset, d.dragView, null); |
| 580 | mTargetCell = mContent.findNearestArea((int) r[0], (int) r[1], 1, 1, mTargetCell); |
| 581 | |
| 582 | if (mTargetCell[0] != mPreviousTargetCell[0] || mTargetCell[1] != mPreviousTargetCell[1]) { |
| 583 | mReorderAlarm.cancelAlarm(); |
| 584 | mReorderAlarm.setOnAlarmListener(mReorderAlarmListener); |
| 585 | mReorderAlarm.setAlarm(150); |
| 586 | mPreviousTargetCell[0] = mTargetCell[0]; |
| 587 | mPreviousTargetCell[1] = mTargetCell[1]; |
| 588 | } |
Adam Cohen | df2cc41 | 2011-04-27 16:56:57 -0700 | [diff] [blame] | 589 | } |
| 590 | |
Adam Cohen | bfbfd26 | 2011-06-13 16:55:12 -0700 | [diff] [blame] | 591 | // This is used to compute the visual center of the dragView. The idea is that |
| 592 | // the visual center represents the user's interpretation of where the item is, and hence |
| 593 | // is the appropriate point to use when determining drop location. |
| 594 | private float[] getDragViewVisualCenter(int x, int y, int xOffset, int yOffset, |
| 595 | DragView dragView, float[] recycle) { |
| 596 | float res[]; |
| 597 | if (recycle == null) { |
| 598 | res = new float[2]; |
| 599 | } else { |
| 600 | res = recycle; |
| 601 | } |
| 602 | |
| 603 | // These represent the visual top and left of drag view if a dragRect was provided. |
| 604 | // If a dragRect was not provided, then they correspond to the actual view left and |
| 605 | // top, as the dragRect is in that case taken to be the entire dragView. |
| 606 | // R.dimen.dragViewOffsetY. |
| 607 | int left = x - xOffset; |
| 608 | int top = y - yOffset; |
| 609 | |
| 610 | // In order to find the visual center, we shift by half the dragRect |
| 611 | res[0] = left + dragView.getDragRegion().width() / 2; |
| 612 | res[1] = top + dragView.getDragRegion().height() / 2; |
| 613 | |
| 614 | return res; |
| 615 | } |
| 616 | |
| 617 | OnAlarmListener mOnExitAlarmListener = new OnAlarmListener() { |
| 618 | public void onAlarm(Alarm alarm) { |
Adam Cohen | 3e8f811 | 2011-07-02 18:03:00 -0700 | [diff] [blame] | 619 | completeDragExit(); |
Adam Cohen | bfbfd26 | 2011-06-13 16:55:12 -0700 | [diff] [blame] | 620 | } |
| 621 | }; |
| 622 | |
Adam Cohen | 95bb800 | 2011-07-03 23:40:28 -0700 | [diff] [blame] | 623 | public void completeDragExit() { |
Adam Cohen | 3e8f811 | 2011-07-02 18:03:00 -0700 | [diff] [blame] | 624 | mLauncher.closeFolder(); |
| 625 | mCurrentDragInfo = null; |
| 626 | mCurrentDragView = null; |
| 627 | mSuppressOnAdd = false; |
| 628 | mRearrangeOnClose = true; |
| 629 | } |
| 630 | |
Adam Cohen | cb3382b | 2011-05-24 14:07:08 -0700 | [diff] [blame] | 631 | public void onDragExit(DragObject d) { |
Adam Cohen | bfbfd26 | 2011-06-13 16:55:12 -0700 | [diff] [blame] | 632 | // We only close the folder if this is a true drag exit, ie. not because a drop |
| 633 | // has occurred above the folder. |
| 634 | if (!d.dragComplete) { |
| 635 | mOnExitAlarm.setOnAlarmListener(mOnExitAlarmListener); |
| 636 | mOnExitAlarm.setAlarm(ON_EXIT_CLOSE_DELAY); |
| 637 | } |
| 638 | mReorderAlarm.cancelAlarm(); |
Adam Cohen | 2801caf | 2011-05-13 20:57:39 -0700 | [diff] [blame] | 639 | } |
| 640 | |
Adam Cohen | c0dcf59 | 2011-06-01 15:30:43 -0700 | [diff] [blame] | 641 | public void onDropCompleted(View target, DragObject d, boolean success) { |
Adam Cohen | 67bd9cc | 2011-07-29 14:07:04 -0700 | [diff] [blame] | 642 | if (success) { |
Adam Cohen | 05e0f40 | 2011-08-01 12:12:49 -0700 | [diff] [blame] | 643 | if (mDeleteFolderOnDropCompleted && !mItemAddedBackToSelfViaIcon) { |
Adam Cohen | 67bd9cc | 2011-07-29 14:07:04 -0700 | [diff] [blame] | 644 | replaceFolderWithFinalItem(); |
| 645 | } |
| 646 | } else { |
| 647 | // The drag failed, we need to return the item to the folder |
| 648 | mFolderIcon.onDrop(d); |
| 649 | |
| 650 | // We're going to trigger a "closeFolder" which may occur before this item has |
| 651 | // been added back to the folder -- this could cause the folder to be deleted |
| 652 | if (mOnExitAlarm.alarmPending()) { |
| 653 | mSuppressFolderDeletion = true; |
| 654 | } |
| 655 | } |
| 656 | |
| 657 | if (target != this) { |
| 658 | if (mOnExitAlarm.alarmPending()) { |
| 659 | mOnExitAlarm.cancelAlarm(); |
| 660 | completeDragExit(); |
| 661 | } |
| 662 | } |
| 663 | mDeleteFolderOnDropCompleted = false; |
| 664 | mDragInProgress = false; |
Adam Cohen | 05e0f40 | 2011-08-01 12:12:49 -0700 | [diff] [blame] | 665 | mItemAddedBackToSelfViaIcon = false; |
Adam Cohen | bfbfd26 | 2011-06-13 16:55:12 -0700 | [diff] [blame] | 666 | mCurrentDragInfo = null; |
| 667 | mCurrentDragView = null; |
| 668 | mSuppressOnAdd = false; |
Adam Cohen | 67bd9cc | 2011-07-29 14:07:04 -0700 | [diff] [blame] | 669 | } |
Adam Cohen | 228da5a | 2011-07-27 22:23:47 -0700 | [diff] [blame] | 670 | |
Adam Cohen | 67bd9cc | 2011-07-29 14:07:04 -0700 | [diff] [blame] | 671 | public void notifyDrop() { |
| 672 | if (mDragInProgress) { |
Adam Cohen | 05e0f40 | 2011-08-01 12:12:49 -0700 | [diff] [blame] | 673 | mItemAddedBackToSelfViaIcon = true; |
Adam Cohen | 76078c4 | 2011-06-09 15:06:52 -0700 | [diff] [blame] | 674 | } |
Adam Cohen | df2cc41 | 2011-04-27 16:56:57 -0700 | [diff] [blame] | 675 | } |
| 676 | |
| 677 | public boolean isDropEnabled() { |
| 678 | return true; |
| 679 | } |
| 680 | |
Adam Cohen | cb3382b | 2011-05-24 14:07:08 -0700 | [diff] [blame] | 681 | public DropTarget getDropTargetDelegate(DragObject d) { |
Adam Cohen | df2cc41 | 2011-04-27 16:56:57 -0700 | [diff] [blame] | 682 | return null; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 683 | } |
Adam Cohen | a9cf38f | 2011-05-02 15:36:58 -0700 | [diff] [blame] | 684 | |
Adam Cohen | 2801caf | 2011-05-13 20:57:39 -0700 | [diff] [blame] | 685 | private void setupContentDimension(int count) { |
| 686 | ArrayList<View> list = getItemsInReadingOrder(); |
| 687 | |
| 688 | int countX = mContent.getCountX(); |
| 689 | int countY = mContent.getCountY(); |
Adam Cohen | 7c69321 | 2011-05-18 15:26:57 -0700 | [diff] [blame] | 690 | boolean done = false; |
Adam Cohen | 2801caf | 2011-05-13 20:57:39 -0700 | [diff] [blame] | 691 | |
Adam Cohen | 7c69321 | 2011-05-18 15:26:57 -0700 | [diff] [blame] | 692 | while (!done) { |
| 693 | int oldCountX = countX; |
| 694 | int oldCountY = countY; |
| 695 | if (countX * countY < count) { |
| 696 | // Current grid is too small, expand it |
| 697 | if (countX <= countY && countX < mMaxCountX) { |
| 698 | countX++; |
| 699 | } else if (countY < mMaxCountY) { |
| 700 | countY++; |
| 701 | } |
| 702 | if (countY == 0) countY++; |
| 703 | } else if ((countY - 1) * countX >= count && countY >= countX) { |
| 704 | countY = Math.max(0, countY - 1); |
| 705 | } else if ((countX - 1) * countY >= count) { |
| 706 | countX = Math.max(0, countX - 1); |
Adam Cohen | 2801caf | 2011-05-13 20:57:39 -0700 | [diff] [blame] | 707 | } |
Adam Cohen | 7c69321 | 2011-05-18 15:26:57 -0700 | [diff] [blame] | 708 | done = countX == oldCountX && countY == oldCountY; |
Adam Cohen | 2801caf | 2011-05-13 20:57:39 -0700 | [diff] [blame] | 709 | } |
Adam Cohen | 7c69321 | 2011-05-18 15:26:57 -0700 | [diff] [blame] | 710 | mContent.setGridSize(countX, countY); |
Adam Cohen | 2801caf | 2011-05-13 20:57:39 -0700 | [diff] [blame] | 711 | arrangeChildren(list); |
| 712 | } |
| 713 | |
| 714 | public boolean isFull() { |
| 715 | return getItemCount() >= mMaxCountX * mMaxCountY; |
| 716 | } |
| 717 | |
| 718 | private void centerAboutIcon() { |
Adam Cohen | 8e776a6 | 2011-06-28 18:10:06 -0700 | [diff] [blame] | 719 | DragLayer.LayoutParams lp = (DragLayer.LayoutParams) getLayoutParams(); |
Adam Cohen | 2801caf | 2011-05-13 20:57:39 -0700 | [diff] [blame] | 720 | |
| 721 | int width = getPaddingLeft() + getPaddingRight() + mContent.getDesiredWidth(); |
Adam Cohen | f4bb1cd | 2011-07-22 14:36:03 -0700 | [diff] [blame] | 722 | int height = getPaddingTop() + getPaddingBottom() + mContent.getDesiredHeight() |
| 723 | + mFolderNameHeight; |
Adam Cohen | 8e776a6 | 2011-06-28 18:10:06 -0700 | [diff] [blame] | 724 | DragLayer parent = (DragLayer) mLauncher.findViewById(R.id.drag_layer); |
Adam Cohen | 2801caf | 2011-05-13 20:57:39 -0700 | [diff] [blame] | 725 | |
Adam Cohen | 8e776a6 | 2011-06-28 18:10:06 -0700 | [diff] [blame] | 726 | parent.getDescendantRectRelativeToSelf(mFolderIcon, mTempRect); |
| 727 | |
| 728 | int centerX = mTempRect.centerX(); |
| 729 | int centerY = mTempRect.centerY(); |
Adam Cohen | 2801caf | 2011-05-13 20:57:39 -0700 | [diff] [blame] | 730 | int centeredLeft = centerX - width / 2; |
| 731 | int centeredTop = centerY - height / 2; |
| 732 | |
Adam Cohen | 35e7e64 | 2011-07-17 14:47:18 -0700 | [diff] [blame] | 733 | // We first fetch the currently visible CellLayoutChildren |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 734 | CellLayout currentPage = mLauncher.getWorkspace().getCurrentDropLayout(); |
Adam Cohen | 35e7e64 | 2011-07-17 14:47:18 -0700 | [diff] [blame] | 735 | CellLayoutChildren boundingLayout = currentPage.getChildrenLayout(); |
| 736 | Rect bounds = new Rect(); |
| 737 | parent.getDescendantRectRelativeToSelf(boundingLayout, bounds); |
Adam Cohen | 2801caf | 2011-05-13 20:57:39 -0700 | [diff] [blame] | 738 | |
Adam Cohen | 35e7e64 | 2011-07-17 14:47:18 -0700 | [diff] [blame] | 739 | // We need to bound the folder to the currently visible CellLayoutChildren |
| 740 | int left = Math.min(Math.max(bounds.left, centeredLeft), |
| 741 | bounds.left + bounds.width() - width); |
| 742 | int top = Math.min(Math.max(bounds.top, centeredTop), |
| 743 | bounds.top + bounds.height() - height); |
| 744 | // If the folder doesn't fit within the bounds, center it about the desired bounds |
| 745 | if (width >= bounds.width()) { |
| 746 | left = bounds.left + (bounds.width() - width) / 2; |
Adam Cohen | 0e4857c | 2011-06-30 17:05:14 -0700 | [diff] [blame] | 747 | } |
Adam Cohen | 35e7e64 | 2011-07-17 14:47:18 -0700 | [diff] [blame] | 748 | if (height >= bounds.height()) { |
| 749 | top = bounds.top + (bounds.height() - height) / 2; |
Adam Cohen | 0e4857c | 2011-06-30 17:05:14 -0700 | [diff] [blame] | 750 | } |
Adam Cohen | 2801caf | 2011-05-13 20:57:39 -0700 | [diff] [blame] | 751 | |
| 752 | int folderPivotX = width / 2 + (centeredLeft - left); |
| 753 | int folderPivotY = height / 2 + (centeredTop - top); |
| 754 | setPivotX(folderPivotX); |
| 755 | setPivotY(folderPivotY); |
| 756 | int folderIconPivotX = (int) (mFolderIcon.getMeasuredWidth() * |
| 757 | (1.0f * folderPivotX / width)); |
| 758 | int folderIconPivotY = (int) (mFolderIcon.getMeasuredHeight() * |
| 759 | (1.0f * folderPivotY / height)); |
| 760 | mFolderIcon.setPivotX(folderIconPivotX); |
| 761 | mFolderIcon.setPivotY(folderIconPivotY); |
| 762 | |
| 763 | if (mMode == PARTIAL_GROW) { |
| 764 | lp.width = width; |
| 765 | lp.height = height; |
| 766 | lp.x = left; |
| 767 | lp.y = top; |
| 768 | } else { |
| 769 | mNewSize.set(left, top, left + width, top + height); |
| 770 | } |
| 771 | } |
| 772 | |
| 773 | private void setupContentForNumItems(int count) { |
Adam Cohen | 2801caf | 2011-05-13 20:57:39 -0700 | [diff] [blame] | 774 | setupContentDimension(count); |
| 775 | |
Adam Cohen | 8e776a6 | 2011-06-28 18:10:06 -0700 | [diff] [blame] | 776 | DragLayer.LayoutParams lp = (DragLayer.LayoutParams) getLayoutParams(); |
Adam Cohen | 2801caf | 2011-05-13 20:57:39 -0700 | [diff] [blame] | 777 | if (lp == null) { |
Adam Cohen | 8e776a6 | 2011-06-28 18:10:06 -0700 | [diff] [blame] | 778 | lp = new DragLayer.LayoutParams(0, 0); |
| 779 | lp.customPosition = true; |
Adam Cohen | 2801caf | 2011-05-13 20:57:39 -0700 | [diff] [blame] | 780 | setLayoutParams(lp); |
| 781 | } |
| 782 | centerAboutIcon(); |
| 783 | } |
| 784 | |
Adam Cohen | 234c4cd | 2011-07-17 21:03:04 -0700 | [diff] [blame] | 785 | protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { |
| 786 | int width = getPaddingLeft() + getPaddingRight() + mContent.getDesiredWidth(); |
Adam Cohen | f4bb1cd | 2011-07-22 14:36:03 -0700 | [diff] [blame] | 787 | int height = getPaddingTop() + getPaddingBottom() + mContent.getDesiredHeight() |
| 788 | + mFolderNameHeight; |
Adam Cohen | 234c4cd | 2011-07-17 21:03:04 -0700 | [diff] [blame] | 789 | |
| 790 | int contentWidthSpec = MeasureSpec.makeMeasureSpec(mContent.getDesiredWidth(), |
| 791 | MeasureSpec.EXACTLY); |
| 792 | int contentHeightSpec = MeasureSpec.makeMeasureSpec(mContent.getDesiredHeight(), |
| 793 | MeasureSpec.EXACTLY); |
| 794 | mContent.measure(contentWidthSpec, contentHeightSpec); |
| 795 | |
| 796 | mFolderName.measure(contentWidthSpec, |
| 797 | MeasureSpec.makeMeasureSpec(mFolderNameHeight, MeasureSpec.EXACTLY)); |
| 798 | setMeasuredDimension(width, height); |
| 799 | } |
| 800 | |
Adam Cohen | 2801caf | 2011-05-13 20:57:39 -0700 | [diff] [blame] | 801 | private void arrangeChildren(ArrayList<View> list) { |
| 802 | int[] vacant = new int[2]; |
| 803 | if (list == null) { |
| 804 | list = getItemsInReadingOrder(); |
| 805 | } |
| 806 | mContent.removeAllViews(); |
| 807 | |
| 808 | for (int i = 0; i < list.size(); i++) { |
| 809 | View v = list.get(i); |
| 810 | mContent.getVacantCell(vacant, 1, 1); |
| 811 | CellLayout.LayoutParams lp = (CellLayout.LayoutParams) v.getLayoutParams(); |
| 812 | lp.cellX = vacant[0]; |
| 813 | lp.cellY = vacant[1]; |
| 814 | ItemInfo info = (ItemInfo) v.getTag(); |
| 815 | info.cellX = vacant[0]; |
| 816 | info.cellY = vacant[1]; |
| 817 | boolean insert = false; |
| 818 | mContent.addViewToCellLayout(v, insert ? 0 : -1, (int)info.id, lp, true); |
Adam Cohen | 7c69321 | 2011-05-18 15:26:57 -0700 | [diff] [blame] | 819 | LauncherModel.addOrMoveItemInDatabase(mLauncher, info, mInfo.id, 0, |
| 820 | info.cellX, info.cellY); |
Adam Cohen | 2801caf | 2011-05-13 20:57:39 -0700 | [diff] [blame] | 821 | } |
Adam Cohen | 7c69321 | 2011-05-18 15:26:57 -0700 | [diff] [blame] | 822 | mItemsInvalidated = true; |
Adam Cohen | 2801caf | 2011-05-13 20:57:39 -0700 | [diff] [blame] | 823 | } |
| 824 | |
Adam Cohen | a9cf38f | 2011-05-02 15:36:58 -0700 | [diff] [blame] | 825 | public int getItemCount() { |
| 826 | return mContent.getChildrenLayout().getChildCount(); |
| 827 | } |
| 828 | |
| 829 | public View getItemAt(int index) { |
| 830 | return mContent.getChildrenLayout().getChildAt(index); |
| 831 | } |
| 832 | |
Adam Cohen | 2801caf | 2011-05-13 20:57:39 -0700 | [diff] [blame] | 833 | private void onCloseComplete() { |
Adam Cohen | 05e0f40 | 2011-08-01 12:12:49 -0700 | [diff] [blame] | 834 | DragLayer parent = (DragLayer) getParent(); |
Adam Cohen | 4554ee1 | 2011-08-03 16:13:21 -0700 | [diff] [blame] | 835 | parent.removeView(this); |
| 836 | mDragController.removeDropTarget((DropTarget) this); |
Adam Cohen | 05e0f40 | 2011-08-01 12:12:49 -0700 | [diff] [blame] | 837 | clearFocus(); |
| 838 | |
Adam Cohen | 2801caf | 2011-05-13 20:57:39 -0700 | [diff] [blame] | 839 | if (mRearrangeOnClose) { |
| 840 | setupContentForNumItems(getItemCount()); |
| 841 | mRearrangeOnClose = false; |
| 842 | } |
Adam Cohen | afb01ee | 2011-06-23 15:38:03 -0700 | [diff] [blame] | 843 | if (getItemCount() <= 1) { |
Adam Cohen | 67bd9cc | 2011-07-29 14:07:04 -0700 | [diff] [blame] | 844 | if (!mDragInProgress && !mSuppressFolderDeletion) { |
| 845 | replaceFolderWithFinalItem(); |
| 846 | } else if (mDragInProgress) { |
| 847 | mDeleteFolderOnDropCompleted = true; |
| 848 | } |
Adam Cohen | afb01ee | 2011-06-23 15:38:03 -0700 | [diff] [blame] | 849 | } |
Adam Cohen | 67bd9cc | 2011-07-29 14:07:04 -0700 | [diff] [blame] | 850 | mSuppressFolderDeletion = false; |
Adam Cohen | afb01ee | 2011-06-23 15:38:03 -0700 | [diff] [blame] | 851 | } |
| 852 | |
| 853 | private void replaceFolderWithFinalItem() { |
| 854 | ItemInfo finalItem = null; |
| 855 | |
| 856 | if (getItemCount() == 1) { |
| 857 | finalItem = mInfo.contents.get(0); |
| 858 | } |
| 859 | |
| 860 | // Remove the folder completely |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 861 | CellLayout cellLayout = mLauncher.getCellLayout(mInfo.container, mInfo.screen); |
Adam Cohen | afb01ee | 2011-06-23 15:38:03 -0700 | [diff] [blame] | 862 | cellLayout.removeView(mFolderIcon); |
| 863 | if (mFolderIcon instanceof DropTarget) { |
| 864 | mDragController.removeDropTarget((DropTarget) mFolderIcon); |
| 865 | } |
| 866 | mLauncher.removeFolder(mInfo); |
| 867 | |
| 868 | if (finalItem != null) { |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 869 | LauncherModel.addOrMoveItemInDatabase(mLauncher, finalItem, mInfo.container, |
| 870 | mInfo.screen, mInfo.cellX, mInfo.cellY); |
Adam Cohen | afb01ee | 2011-06-23 15:38:03 -0700 | [diff] [blame] | 871 | } |
Adam Cohen | 716b51e | 2011-06-30 12:09:54 -0700 | [diff] [blame] | 872 | LauncherModel.deleteItemFromDatabase(mLauncher, mInfo); |
Adam Cohen | afb01ee | 2011-06-23 15:38:03 -0700 | [diff] [blame] | 873 | |
| 874 | // Add the last remaining child to the workspace in place of the folder |
| 875 | if (finalItem != null) { |
| 876 | View child = mLauncher.createShortcut(R.layout.application, cellLayout, |
| 877 | (ShortcutInfo) finalItem); |
| 878 | |
Winson Chung | 3d503fb | 2011-07-13 17:25:49 -0700 | [diff] [blame] | 879 | mLauncher.getWorkspace().addInScreen(child, mInfo.container, mInfo.screen, mInfo.cellX, |
| 880 | mInfo.cellY, mInfo.spanX, mInfo.spanY); |
Adam Cohen | afb01ee | 2011-06-23 15:38:03 -0700 | [diff] [blame] | 881 | } |
Adam Cohen | 2801caf | 2011-05-13 20:57:39 -0700 | [diff] [blame] | 882 | } |
| 883 | |
Adam Cohen | bfbfd26 | 2011-06-13 16:55:12 -0700 | [diff] [blame] | 884 | public void onDrop(DragObject d) { |
| 885 | ShortcutInfo item; |
| 886 | if (d.dragInfo instanceof ApplicationInfo) { |
| 887 | // Came from all apps -- make a copy |
| 888 | item = ((ApplicationInfo) d.dragInfo).makeShortcut(); |
| 889 | item.spanX = 1; |
| 890 | item.spanY = 1; |
| 891 | } else { |
| 892 | item = (ShortcutInfo) d.dragInfo; |
| 893 | } |
Adam Cohen | 05e0f40 | 2011-08-01 12:12:49 -0700 | [diff] [blame] | 894 | // Dragged from self onto self, currently this is the only path possible, however |
| 895 | // we keep this as a distinct code path. |
Adam Cohen | bfbfd26 | 2011-06-13 16:55:12 -0700 | [diff] [blame] | 896 | if (item == mCurrentDragInfo) { |
| 897 | ShortcutInfo si = (ShortcutInfo) mCurrentDragView.getTag(); |
| 898 | CellLayout.LayoutParams lp = (CellLayout.LayoutParams) mCurrentDragView.getLayoutParams(); |
| 899 | si.cellX = lp.cellX = mEmptyCell[0]; |
| 900 | si.cellX = lp.cellY = mEmptyCell[1]; |
| 901 | mContent.addViewToCellLayout(mCurrentDragView, -1, (int)item.id, lp, true); |
Adam Cohen | fc53cd2 | 2011-07-20 15:45:11 -0700 | [diff] [blame] | 902 | if (d.dragView.hasDrawn()) { |
| 903 | mLauncher.getDragLayer().animateViewIntoPosition(d.dragView, mCurrentDragView); |
| 904 | } else { |
| 905 | mCurrentDragView.setVisibility(VISIBLE); |
| 906 | } |
Adam Cohen | e9166b2 | 2011-07-08 17:11:11 -0700 | [diff] [blame] | 907 | mItemsInvalidated = true; |
Adam Cohen | bfbfd26 | 2011-06-13 16:55:12 -0700 | [diff] [blame] | 908 | setupContentDimension(getItemCount()); |
Adam Cohen | 716b51e | 2011-06-30 12:09:54 -0700 | [diff] [blame] | 909 | mSuppressOnAdd = true; |
Adam Cohen | bfbfd26 | 2011-06-13 16:55:12 -0700 | [diff] [blame] | 910 | } |
| 911 | mInfo.add(item); |
| 912 | } |
| 913 | |
| 914 | public void onAdd(ShortcutInfo item) { |
| 915 | mItemsInvalidated = true; |
Adam Cohen | 05e0f40 | 2011-08-01 12:12:49 -0700 | [diff] [blame] | 916 | // If the item was dropped onto this open folder, we have done the work associated |
| 917 | // 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] | 918 | if (mSuppressOnAdd) return; |
| 919 | if (!findAndSetEmptyCells(item)) { |
| 920 | // The current layout is full, can we expand it? |
| 921 | setupContentForNumItems(getItemCount() + 1); |
| 922 | findAndSetEmptyCells(item); |
| 923 | } |
| 924 | createAndAddShortcut(item); |
| 925 | LauncherModel.addOrMoveItemInDatabase( |
| 926 | mLauncher, item, mInfo.id, 0, item.cellX, item.cellY); |
| 927 | } |
| 928 | |
Adam Cohen | a9cf38f | 2011-05-02 15:36:58 -0700 | [diff] [blame] | 929 | public void onRemove(ShortcutInfo item) { |
Adam Cohen | 7c69321 | 2011-05-18 15:26:57 -0700 | [diff] [blame] | 930 | mItemsInvalidated = true; |
Adam Cohen | 05e0f40 | 2011-08-01 12:12:49 -0700 | [diff] [blame] | 931 | // If this item is being dragged from this open folder, we have already handled |
| 932 | // 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] | 933 | if (item == mCurrentDragInfo) return; |
Adam Cohen | df1e4e8 | 2011-06-24 15:57:39 -0700 | [diff] [blame] | 934 | View v = getViewForInfo(item); |
Adam Cohen | a9cf38f | 2011-05-02 15:36:58 -0700 | [diff] [blame] | 935 | mContent.removeView(v); |
Adam Cohen | 2801caf | 2011-05-13 20:57:39 -0700 | [diff] [blame] | 936 | if (mState == STATE_ANIMATING) { |
| 937 | mRearrangeOnClose = true; |
| 938 | } else { |
| 939 | setupContentForNumItems(getItemCount()); |
| 940 | } |
Adam Cohen | afb01ee | 2011-06-23 15:38:03 -0700 | [diff] [blame] | 941 | if (getItemCount() <= 1) { |
| 942 | replaceFolderWithFinalItem(); |
| 943 | } |
Adam Cohen | a9cf38f | 2011-05-02 15:36:58 -0700 | [diff] [blame] | 944 | } |
Adam Cohen | 7c69321 | 2011-05-18 15:26:57 -0700 | [diff] [blame] | 945 | |
Adam Cohen | df1e4e8 | 2011-06-24 15:57:39 -0700 | [diff] [blame] | 946 | private View getViewForInfo(ShortcutInfo item) { |
| 947 | for (int j = 0; j < mContent.getCountY(); j++) { |
| 948 | for (int i = 0; i < mContent.getCountX(); i++) { |
| 949 | View v = mContent.getChildAt(i, j); |
| 950 | if (v.getTag() == item) { |
| 951 | return v; |
| 952 | } |
| 953 | } |
| 954 | } |
| 955 | return null; |
| 956 | } |
| 957 | |
Adam Cohen | 76078c4 | 2011-06-09 15:06:52 -0700 | [diff] [blame] | 958 | public void onItemsChanged() { |
| 959 | } |
Adam Cohen | 76fc085 | 2011-06-17 13:26:23 -0700 | [diff] [blame] | 960 | public void onTitleChanged(CharSequence title) { |
| 961 | } |
Adam Cohen | 76078c4 | 2011-06-09 15:06:52 -0700 | [diff] [blame] | 962 | |
Adam Cohen | 7c69321 | 2011-05-18 15:26:57 -0700 | [diff] [blame] | 963 | public ArrayList<View> getItemsInReadingOrder() { |
Adam Cohen | 76078c4 | 2011-06-09 15:06:52 -0700 | [diff] [blame] | 964 | return getItemsInReadingOrder(true); |
| 965 | } |
| 966 | |
| 967 | public ArrayList<View> getItemsInReadingOrder(boolean includeCurrentDragItem) { |
Adam Cohen | 7c69321 | 2011-05-18 15:26:57 -0700 | [diff] [blame] | 968 | if (mItemsInvalidated) { |
| 969 | mItemsInReadingOrder.clear(); |
| 970 | for (int j = 0; j < mContent.getCountY(); j++) { |
| 971 | for (int i = 0; i < mContent.getCountX(); i++) { |
| 972 | View v = mContent.getChildAt(i, j); |
| 973 | if (v != null) { |
Adam Cohen | 76078c4 | 2011-06-09 15:06:52 -0700 | [diff] [blame] | 974 | ShortcutInfo info = (ShortcutInfo) v.getTag(); |
| 975 | if (info != mCurrentDragInfo || includeCurrentDragItem) { |
| 976 | mItemsInReadingOrder.add(v); |
| 977 | } |
Adam Cohen | 7c69321 | 2011-05-18 15:26:57 -0700 | [diff] [blame] | 978 | } |
| 979 | } |
| 980 | } |
| 981 | mItemsInvalidated = false; |
| 982 | } |
| 983 | return mItemsInReadingOrder; |
| 984 | } |
Adam Cohen | 8dfcba4 | 2011-07-07 16:38:18 -0700 | [diff] [blame] | 985 | |
| 986 | public void getLocationInDragLayer(int[] loc) { |
| 987 | mLauncher.getDragLayer().getLocationInDragLayer(this, loc); |
| 988 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 989 | } |