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