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