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