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