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