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