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