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