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