blob: db65a31eec578651c832e07e2b9facf96ca367b1 [file] [log] [blame]
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001/*
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 Onoratoa5902522009-07-30 13:37:37 -070017package com.android.launcher2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080018
Adam Cohendf2cc412011-04-27 16:56:57 -070019import android.animation.Animator;
20import android.animation.AnimatorListenerAdapter;
Adam Cohendf2cc412011-04-27 16:56:57 -070021import android.animation.ObjectAnimator;
22import android.animation.PropertyValuesHolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080023import android.content.Context;
Adam Cohen76fc0852011-06-17 13:26:23 -070024import android.content.res.Resources;
Winson Chung043f2af2012-03-01 16:09:54 -080025import android.graphics.PointF;
Romain Guyfb5411e2010-02-24 10:04:17 -080026import android.graphics.Rect;
Adam Cohenbadf71e2011-05-26 19:08:29 -070027import android.graphics.drawable.Drawable;
Adam Cohen7a14d0b2011-06-24 11:36:35 -070028import android.text.InputType;
Adam Cohene601a432011-07-26 21:51:30 -070029import android.text.Selection;
30import android.text.Spannable;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080031import android.util.AttributeSet;
Adam Cohen3bf84d32012-05-07 20:17:14 -070032import android.util.Log;
Adam Cohen76fc0852011-06-17 13:26:23 -070033import android.view.ActionMode;
34import android.view.KeyEvent;
Adam Cohendf2cc412011-04-27 16:56:57 -070035import android.view.LayoutInflater;
Adam Cohen76fc0852011-06-17 13:26:23 -070036import android.view.Menu;
37import android.view.MenuItem;
Adam Cohen0c872ba2011-05-05 10:34:16 -070038import android.view.MotionEvent;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080039import android.view.View;
Adam Cohen3371da02011-10-25 21:38:29 -070040import android.view.accessibility.AccessibilityEvent;
41import android.view.accessibility.AccessibilityManager;
Adam Cohen76fc0852011-06-17 13:26:23 -070042import android.view.inputmethod.EditorInfo;
43import android.view.inputmethod.InputMethodManager;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080044import android.widget.LinearLayout;
Adam Cohendf2cc412011-04-27 16:56:57 -070045import android.widget.TextView;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080046
Romain Guyedcce092010-03-04 13:03:17 -080047import com.android.launcher.R;
Adam Cohena9cf38f2011-05-02 15:36:58 -070048import com.android.launcher2.FolderInfo.FolderListener;
Romain Guyedcce092010-03-04 13:03:17 -080049
Adam Cohenc0dcf592011-06-01 15:30:43 -070050import java.util.ArrayList;
Adam Cohen3bf84d32012-05-07 20:17:14 -070051import java.util.Collections;
52import java.util.Comparator;
Adam Cohenc0dcf592011-06-01 15:30:43 -070053
The Android Open Source Project31dd5032009-03-03 19:32:27 -080054/**
55 * Represents a set of icons chosen by the user or generated by the system.
56 */
Adam Cohen8dfcba42011-07-07 16:38:18 -070057public class Folder extends LinearLayout implements DragSource, View.OnClickListener,
Adam Cohenea0818d2011-09-30 20:06:58 -070058 View.OnLongClickListener, DropTarget, FolderListener, TextView.OnEditorActionListener,
59 View.OnFocusChangeListener {
The Android Open Source Project31dd5032009-03-03 19:32:27 -080060
Michael Jurka3a9fced2012-04-13 14:44:29 -070061 @SuppressWarnings("unused")
Adam Cohendf2cc412011-04-27 16:56:57 -070062 private static final String TAG = "Launcher.Folder";
63
Adam Cohen4eac29a2011-07-11 17:53:37 -070064 protected DragController mDragController;
65 protected Launcher mLauncher;
66 protected FolderInfo mInfo;
67
Adam Cohendf2cc412011-04-27 16:56:57 -070068 static final int STATE_NONE = -1;
69 static final int STATE_SMALL = 0;
70 static final int STATE_ANIMATING = 1;
71 static final int STATE_OPEN = 2;
72
73 private int mExpandDuration;
74 protected CellLayout mContent;
75 private final LayoutInflater mInflater;
76 private final IconCache mIconCache;
77 private int mState = STATE_NONE;
Adam Cohenbfbfd262011-06-13 16:55:12 -070078 private static final int REORDER_ANIMATION_DURATION = 230;
79 private static final int ON_EXIT_CLOSE_DELAY = 800;
Adam Cohen2801caf2011-05-13 20:57:39 -070080 private boolean mRearrangeOnClose = false;
81 private FolderIcon mFolderIcon;
82 private int mMaxCountX;
83 private int mMaxCountY;
Adam Cohen78dc83e2011-11-15 17:10:00 -080084 private int mMaxNumItems;
Adam Cohen7c693212011-05-18 15:26:57 -070085 private ArrayList<View> mItemsInReadingOrder = new ArrayList<View>();
Adam Cohenbadf71e2011-05-26 19:08:29 -070086 private Drawable mIconDrawable;
Adam Cohen7c693212011-05-18 15:26:57 -070087 boolean mItemsInvalidated = false;
Adam Cohenbfbfd262011-06-13 16:55:12 -070088 private ShortcutInfo mCurrentDragInfo;
89 private View mCurrentDragView;
90 boolean mSuppressOnAdd = false;
91 private int[] mTargetCell = new int[2];
92 private int[] mPreviousTargetCell = new int[2];
93 private int[] mEmptyCell = new int[2];
94 private Alarm mReorderAlarm = new Alarm();
95 private Alarm mOnExitAlarm = new Alarm();
Adam Cohen76fc0852011-06-17 13:26:23 -070096 private int mFolderNameHeight;
Adam Cohen8e776a62011-06-28 18:10:06 -070097 private Rect mTempRect = new Rect();
Adam Cohen67bd9cc2011-07-29 14:07:04 -070098 private boolean mDragInProgress = false;
99 private boolean mDeleteFolderOnDropCompleted = false;
100 private boolean mSuppressFolderDeletion = false;
Adam Cohen05e0f402011-08-01 12:12:49 -0700101 private boolean mItemAddedBackToSelfViaIcon = false;
Adam Cohenac56cff2011-09-28 20:45:37 -0700102 FolderEditText mFolderName;
Adam Cohen228da5a2011-07-27 22:23:47 -0700103
Adam Cohen76fc0852011-06-17 13:26:23 -0700104 private boolean mIsEditingName = false;
105 private InputMethodManager mInputMethodManager;
Adam Cohendf2cc412011-04-27 16:56:57 -0700106
Adam Cohena65beee2011-06-27 21:32:23 -0700107 private static String sDefaultFolderName;
108 private static String sHintText;
Michael Jurka0121c3e2012-05-31 08:36:04 -0700109 private ObjectAnimator mOpenCloseAnimator;
Adam Cohena65beee2011-06-27 21:32:23 -0700110
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800111 /**
112 * Used to inflate the Workspace from XML.
113 *
114 * @param context The application's context.
115 * @param attrs The attribtues set containing the Workspace's customization values.
116 */
117 public Folder(Context context, AttributeSet attrs) {
118 super(context, attrs);
119 setAlwaysDrawnWithCacheEnabled(false);
Adam Cohendf2cc412011-04-27 16:56:57 -0700120 mInflater = LayoutInflater.from(context);
121 mIconCache = ((LauncherApplication)context.getApplicationContext()).getIconCache();
Adam Cohen78dc83e2011-11-15 17:10:00 -0800122
123 Resources res = getResources();
124 mMaxCountX = res.getInteger(R.integer.folder_max_count_x);
125 mMaxCountY = res.getInteger(R.integer.folder_max_count_y);
126 mMaxNumItems = res.getInteger(R.integer.folder_max_num_items);
127 if (mMaxCountX < 0 || mMaxCountY < 0 || mMaxNumItems < 0) {
128 mMaxCountX = LauncherModel.getCellCountX();
129 mMaxCountY = LauncherModel.getCellCountY();
130 mMaxNumItems = mMaxCountX * mMaxCountY;
131 }
Adam Cohen76fc0852011-06-17 13:26:23 -0700132
133 mInputMethodManager = (InputMethodManager)
Michael Jurka8b805b12012-04-18 14:23:14 -0700134 getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
Adam Cohen76fc0852011-06-17 13:26:23 -0700135
Adam Cohen76fc0852011-06-17 13:26:23 -0700136 mExpandDuration = res.getInteger(R.integer.config_folderAnimDuration);
Adam Cohen4ef610f2011-06-21 22:37:36 -0700137
138 if (sDefaultFolderName == null) {
139 sDefaultFolderName = res.getString(R.string.folder_name);
140 }
Adam Cohena65beee2011-06-27 21:32:23 -0700141 if (sHintText == null) {
142 sHintText = res.getString(R.string.folder_hint_text);
143 }
Adam Cohen4eac29a2011-07-11 17:53:37 -0700144 mLauncher = (Launcher) context;
Adam Cohenac56cff2011-09-28 20:45:37 -0700145 // We need this view to be focusable in touch mode so that when text editing of the folder
146 // name is complete, we have something to focus on, thus hiding the cursor and giving
147 // reliable behvior when clicking the text field (since it will always gain focus on click).
148 setFocusableInTouchMode(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800149 }
150
151 @Override
152 protected void onFinishInflate() {
153 super.onFinishInflate();
Adam Cohendf2cc412011-04-27 16:56:57 -0700154 mContent = (CellLayout) findViewById(R.id.folder_content);
Adam Cohen2801caf2011-05-13 20:57:39 -0700155 mContent.setGridSize(0, 0);
Michael Jurkaa52570f2012-03-20 03:18:20 -0700156 mContent.getShortcutsAndWidgets().setMotionEventSplittingEnabled(false);
Adam Cohenac56cff2011-09-28 20:45:37 -0700157 mFolderName = (FolderEditText) findViewById(R.id.folder_name);
158 mFolderName.setFolder(this);
Adam Cohenea0818d2011-09-30 20:06:58 -0700159 mFolderName.setOnFocusChangeListener(this);
Adam Cohen76fc0852011-06-17 13:26:23 -0700160
161 // We find out how tall the text view wants to be (it is set to wrap_content), so that
162 // we can allocate the appropriate amount of space for it.
163 int measureSpec = MeasureSpec.UNSPECIFIED;
164 mFolderName.measure(measureSpec, measureSpec);
165 mFolderNameHeight = mFolderName.getMeasuredHeight();
166
167 // We disable action mode for now since it messes up the view on phones
168 mFolderName.setCustomSelectionActionModeCallback(mActionModeCallback);
Adam Cohen76fc0852011-06-17 13:26:23 -0700169 mFolderName.setOnEditorActionListener(this);
Adam Cohen4ef610f2011-06-21 22:37:36 -0700170 mFolderName.setSelectAllOnFocus(true);
Adam Cohen7a14d0b2011-06-24 11:36:35 -0700171 mFolderName.setInputType(mFolderName.getInputType() |
172 InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS | InputType.TYPE_TEXT_FLAG_CAP_WORDS);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800173 }
Adam Cohen2801caf2011-05-13 20:57:39 -0700174
Adam Cohen76fc0852011-06-17 13:26:23 -0700175 private ActionMode.Callback mActionModeCallback = new ActionMode.Callback() {
176 public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
177 return false;
178 }
179
180 public boolean onCreateActionMode(ActionMode mode, Menu menu) {
181 return false;
182 }
183
184 public void onDestroyActionMode(ActionMode mode) {
185 }
186
187 public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
188 return false;
189 }
190 };
191
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800192 public void onClick(View v) {
Adam Cohendf2cc412011-04-27 16:56:57 -0700193 Object tag = v.getTag();
194 if (tag instanceof ShortcutInfo) {
195 // refactor this code from Folder
196 ShortcutInfo item = (ShortcutInfo) tag;
197 int[] pos = new int[2];
198 v.getLocationOnScreen(pos);
199 item.intent.setSourceBounds(new Rect(pos[0], pos[1],
200 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Winson Chungc7450e32012-04-17 17:34:08 -0700201
202 mLauncher.startActivitySafely(v, item.intent, item);
Adam Cohendf2cc412011-04-27 16:56:57 -0700203 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800204 }
205
206 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -0700207 // Return if global dragging is not enabled
208 if (!mLauncher.isDraggingEnabled()) return true;
209
Adam Cohendf2cc412011-04-27 16:56:57 -0700210 Object tag = v.getTag();
211 if (tag instanceof ShortcutInfo) {
Adam Cohendf2cc412011-04-27 16:56:57 -0700212 ShortcutInfo item = (ShortcutInfo) tag;
213 if (!v.isInTouchMode()) {
214 return false;
215 }
216
Winson Chung7d7541e2011-09-16 20:14:36 -0700217 mLauncher.dismissFolderCling(null);
218
Adam Cohendf2cc412011-04-27 16:56:57 -0700219 mLauncher.getWorkspace().onDragStartedWithItem(v);
Adam Cohenac8c8762011-07-13 11:15:27 -0700220 mLauncher.getWorkspace().beginDragShared(v, this);
Adam Cohenbadf71e2011-05-26 19:08:29 -0700221 mIconDrawable = ((TextView) v).getCompoundDrawables()[1];
Adam Cohen76078c42011-06-09 15:06:52 -0700222
223 mCurrentDragInfo = item;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700224 mEmptyCell[0] = item.cellX;
225 mEmptyCell[1] = item.cellY;
226 mCurrentDragView = v;
Adam Cohenfc53cd22011-07-20 15:45:11 -0700227
228 mContent.removeView(mCurrentDragView);
229 mInfo.remove(mCurrentDragInfo);
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700230 mDragInProgress = true;
Adam Cohen05e0f402011-08-01 12:12:49 -0700231 mItemAddedBackToSelfViaIcon = false;
Adam Cohendf2cc412011-04-27 16:56:57 -0700232 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800233 return true;
234 }
235
Adam Cohen76fc0852011-06-17 13:26:23 -0700236 public boolean isEditingName() {
237 return mIsEditingName;
238 }
239
240 public void startEditingFolderName() {
Adam Cohena65beee2011-06-27 21:32:23 -0700241 mFolderName.setHint("");
Adam Cohen76fc0852011-06-17 13:26:23 -0700242 mIsEditingName = true;
243 }
244
245 public void dismissEditingName() {
246 mInputMethodManager.hideSoftInputFromWindow(getWindowToken(), 0);
247 doneEditingFolderName(true);
248 }
249
250 public void doneEditingFolderName(boolean commit) {
Adam Cohena65beee2011-06-27 21:32:23 -0700251 mFolderName.setHint(sHintText);
Adam Cohen1df26a32011-08-12 16:15:23 -0700252 // Convert to a string here to ensure that no other state associated with the text field
253 // gets saved.
Adam Cohen3371da02011-10-25 21:38:29 -0700254 String newTitle = mFolderName.getText().toString();
255 mInfo.setTitle(newTitle);
Adam Cohen76fc0852011-06-17 13:26:23 -0700256 LauncherModel.updateItemInDatabase(mLauncher, mInfo);
Adam Cohenac56cff2011-09-28 20:45:37 -0700257
Adam Cohen3371da02011-10-25 21:38:29 -0700258 if (commit) {
259 sendCustomAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED,
Michael Jurka8b805b12012-04-18 14:23:14 -0700260 String.format(getContext().getString(R.string.folder_renamed), newTitle));
Adam Cohen3371da02011-10-25 21:38:29 -0700261 }
Adam Cohenac56cff2011-09-28 20:45:37 -0700262 // In order to clear the focus from the text field, we set the focus on ourself. This
263 // ensures that every time the field is clicked, focus is gained, giving reliable behavior.
264 requestFocus();
265
Adam Cohene601a432011-07-26 21:51:30 -0700266 Selection.setSelection((Spannable) mFolderName.getText(), 0, 0);
Adam Cohen76fc0852011-06-17 13:26:23 -0700267 mIsEditingName = false;
268 }
269
270 public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
271 if (actionId == EditorInfo.IME_ACTION_DONE) {
272 dismissEditingName();
273 return true;
274 }
275 return false;
276 }
277
278 public View getEditTextRegion() {
279 return mFolderName;
280 }
281
Adam Cohenbadf71e2011-05-26 19:08:29 -0700282 public Drawable getDragDrawable() {
283 return mIconDrawable;
284 }
285
Adam Cohen0c872ba2011-05-05 10:34:16 -0700286 /**
287 * We need to handle touch events to prevent them from falling through to the workspace below.
288 */
289 @Override
290 public boolean onTouchEvent(MotionEvent ev) {
291 return true;
292 }
293
Joe Onorato00acb122009-08-04 16:04:30 -0400294 public void setDragController(DragController dragController) {
295 mDragController = dragController;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800296 }
297
Adam Cohen2801caf2011-05-13 20:57:39 -0700298 void setFolderIcon(FolderIcon icon) {
299 mFolderIcon = icon;
300 }
301
Adam Cohen3371da02011-10-25 21:38:29 -0700302 @Override
303 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
304 // When the folder gets focus, we don't want to announce the list of items.
305 return true;
306 }
307
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800308 /**
309 * @return the FolderInfo object associated with this folder
310 */
311 FolderInfo getInfo() {
312 return mInfo;
313 }
314
Adam Cohen3bf84d32012-05-07 20:17:14 -0700315 private class GridComparator implements Comparator<ShortcutInfo> {
316 int mNumCols;
317 public GridComparator(int numCols) {
318 mNumCols = numCols;
319 }
320
321 @Override
322 public int compare(ShortcutInfo lhs, ShortcutInfo rhs) {
323 int lhIndex = lhs.cellY * mNumCols + lhs.cellX;
324 int rhIndex = rhs.cellY * mNumCols + rhs.cellX;
325 return (lhIndex - rhIndex);
326 }
Adam Cohen3bf84d32012-05-07 20:17:14 -0700327 }
328
329 private void placeInReadingOrder(ArrayList<ShortcutInfo> items) {
330 int maxX = 0;
331 int count = items.size();
332 for (int i = 0; i < count; i++) {
333 ShortcutInfo item = items.get(i);
334 if (item.cellX > maxX) {
335 maxX = item.cellX;
336 }
337 }
Adam Cohen691a5792012-05-11 14:27:30 -0700338
339 GridComparator gridComparator = new GridComparator(maxX + 1);
Adam Cohen3bf84d32012-05-07 20:17:14 -0700340 Collections.sort(items, gridComparator);
341 final int countX = mContent.getCountX();
342 for (int i = 0; i < count; i++) {
343 int x = i % countX;
344 int y = i / countX;
345 ShortcutInfo item = items.get(i);
346 item.cellX = x;
347 item.cellY = y;
348 }
349 }
350
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800351 void bind(FolderInfo info) {
352 mInfo = info;
Adam Cohendf2cc412011-04-27 16:56:57 -0700353 ArrayList<ShortcutInfo> children = info.contents;
Adam Cohenc508b2d2011-06-28 14:41:44 -0700354 ArrayList<ShortcutInfo> overflow = new ArrayList<ShortcutInfo>();
Adam Cohen7c693212011-05-18 15:26:57 -0700355 setupContentForNumItems(children.size());
Adam Cohen3bf84d32012-05-07 20:17:14 -0700356 placeInReadingOrder(children);
Adam Cohen0057bbc2011-08-12 18:30:51 -0700357 int count = 0;
Adam Cohendf2cc412011-04-27 16:56:57 -0700358 for (int i = 0; i < children.size(); i++) {
359 ShortcutInfo child = (ShortcutInfo) children.get(i);
Adam Cohenc508b2d2011-06-28 14:41:44 -0700360 if (!createAndAddShortcut(child)) {
361 overflow.add(child);
Adam Cohen0057bbc2011-08-12 18:30:51 -0700362 } else {
363 count++;
Adam Cohenc508b2d2011-06-28 14:41:44 -0700364 }
Adam Cohendf2cc412011-04-27 16:56:57 -0700365 }
Adam Cohenc508b2d2011-06-28 14:41:44 -0700366
Adam Cohen0057bbc2011-08-12 18:30:51 -0700367 // We rearrange the items in case there are any empty gaps
368 setupContentForNumItems(count);
369
Adam Cohenc508b2d2011-06-28 14:41:44 -0700370 // If our folder has too many items we prune them from the list. This is an issue
371 // when upgrading from the old Folders implementation which could contain an unlimited
372 // number of items.
373 for (ShortcutInfo item: overflow) {
374 mInfo.remove(item);
375 LauncherModel.deleteItemFromDatabase(mLauncher, item);
376 }
377
Adam Cohen4dbe6d92011-05-18 17:14:15 -0700378 mItemsInvalidated = true;
Adam Cohenac56cff2011-09-28 20:45:37 -0700379 updateTextViewFocus();
Adam Cohena9cf38f2011-05-02 15:36:58 -0700380 mInfo.addListener(this);
Adam Cohen4ef610f2011-06-21 22:37:36 -0700381
Adam Cohenafb01ee2011-06-23 15:38:03 -0700382 if (!sDefaultFolderName.contentEquals(mInfo.title)) {
Adam Cohen4ef610f2011-06-21 22:37:36 -0700383 mFolderName.setText(mInfo.title);
384 } else {
385 mFolderName.setText("");
386 }
Adam Cohen691a5792012-05-11 14:27:30 -0700387 updateItemLocationsInDatabase();
Adam Cohendf2cc412011-04-27 16:56:57 -0700388 }
389
390 /**
391 * Creates a new UserFolder, inflated from R.layout.user_folder.
392 *
393 * @param context The application's context.
394 *
395 * @return A new UserFolder.
396 */
397 static Folder fromXml(Context context) {
398 return (Folder) LayoutInflater.from(context).inflate(R.layout.user_folder, null);
399 }
400
401 /**
402 * This method is intended to make the UserFolder to be visually identical in size and position
403 * to its associated FolderIcon. This allows for a seamless transition into the expanded state.
404 */
405 private void positionAndSizeAsIcon() {
Adam Cohen3e8f8112011-07-02 18:03:00 -0700406 if (!(getParent() instanceof DragLayer)) return;
Adam Cohen662b5982011-12-13 17:45:21 -0800407 setScaleX(0.8f);
408 setScaleY(0.8f);
409 setAlpha(0f);
Adam Cohendf2cc412011-04-27 16:56:57 -0700410 mState = STATE_SMALL;
411 }
412
413 public void animateOpen() {
Adam Cohen3e8f8112011-07-02 18:03:00 -0700414 positionAndSizeAsIcon();
415
Adam Cohen8e776a62011-06-28 18:10:06 -0700416 if (!(getParent() instanceof DragLayer)) return;
Adam Cohen2801caf2011-05-13 20:57:39 -0700417 centerAboutIcon();
Adam Cohen662b5982011-12-13 17:45:21 -0800418 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1);
419 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.0f);
420 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.0f);
Michael Jurka0121c3e2012-05-31 08:36:04 -0700421 final ObjectAnimator oa = mOpenCloseAnimator =
422 ObjectAnimator.ofPropertyValuesHolder(this, alpha, scaleX, scaleY);
Adam Cohen6441de02011-12-14 14:25:32 -0800423
Adam Cohen2801caf2011-05-13 20:57:39 -0700424 oa.addListener(new AnimatorListenerAdapter() {
Adam Cohendf2cc412011-04-27 16:56:57 -0700425 @Override
426 public void onAnimationStart(Animator animation) {
Adam Cohen3371da02011-10-25 21:38:29 -0700427 sendCustomAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED,
Michael Jurka8b805b12012-04-18 14:23:14 -0700428 String.format(getContext().getString(R.string.folder_opened),
Adam Cohen3371da02011-10-25 21:38:29 -0700429 mContent.getCountX(), mContent.getCountY()));
Adam Cohendf2cc412011-04-27 16:56:57 -0700430 mState = STATE_ANIMATING;
431 }
432 @Override
433 public void onAnimationEnd(Animator animation) {
Adam Cohen2801caf2011-05-13 20:57:39 -0700434 mState = STATE_OPEN;
Michael Jurka0121c3e2012-05-31 08:36:04 -0700435 setLayerType(LAYER_TYPE_NONE, null);
Winson Chung7a74ac92011-09-20 17:43:51 -0700436 Cling cling = mLauncher.showFirstRunFoldersCling();
437 if (cling != null) {
438 cling.bringToFront();
439 }
Adam Cohenac56cff2011-09-28 20:45:37 -0700440 setFocusOnFirstChild();
Adam Cohendf2cc412011-04-27 16:56:57 -0700441 }
442 });
Adam Cohen2801caf2011-05-13 20:57:39 -0700443 oa.setDuration(mExpandDuration);
Michael Jurka0121c3e2012-05-31 08:36:04 -0700444 setLayerType(LAYER_TYPE_HARDWARE, null);
445 buildLayer();
446 post(new Runnable() {
447 public void run() {
448 // Check if the animator changed in the meantime
449 if (oa != mOpenCloseAnimator)
450 return;
451 oa.start();
452 }
453 });
Adam Cohendf2cc412011-04-27 16:56:57 -0700454 }
455
Adam Cohen3371da02011-10-25 21:38:29 -0700456 private void sendCustomAccessibilityEvent(int type, String text) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700457 AccessibilityManager accessibilityManager = (AccessibilityManager)
458 getContext().getSystemService(Context.ACCESSIBILITY_SERVICE);
459 if (accessibilityManager.isEnabled()) {
Adam Cohen3371da02011-10-25 21:38:29 -0700460 AccessibilityEvent event = AccessibilityEvent.obtain(type);
461 onInitializeAccessibilityEvent(event);
462 event.getText().add(text);
Michael Jurka8b805b12012-04-18 14:23:14 -0700463 accessibilityManager.sendAccessibilityEvent(event);
Adam Cohen3371da02011-10-25 21:38:29 -0700464 }
465 }
466
Adam Cohenac56cff2011-09-28 20:45:37 -0700467 private void setFocusOnFirstChild() {
468 View firstChild = mContent.getChildAt(0, 0);
469 if (firstChild != null) {
470 firstChild.requestFocus();
471 }
472 }
473
Adam Cohendf2cc412011-04-27 16:56:57 -0700474 public void animateClosed() {
Adam Cohen8e776a62011-06-28 18:10:06 -0700475 if (!(getParent() instanceof DragLayer)) return;
Adam Cohen662b5982011-12-13 17:45:21 -0800476 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0);
477 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 0.9f);
478 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 0.9f);
Michael Jurka0121c3e2012-05-31 08:36:04 -0700479 final ObjectAnimator oa = mOpenCloseAnimator =
480 ObjectAnimator.ofPropertyValuesHolder(this, alpha, scaleX, scaleY);
Adam Cohendf2cc412011-04-27 16:56:57 -0700481
Adam Cohen2801caf2011-05-13 20:57:39 -0700482 oa.addListener(new AnimatorListenerAdapter() {
Adam Cohendf2cc412011-04-27 16:56:57 -0700483 @Override
484 public void onAnimationEnd(Animator animation) {
Adam Cohen2801caf2011-05-13 20:57:39 -0700485 onCloseComplete();
Michael Jurka0121c3e2012-05-31 08:36:04 -0700486 setLayerType(LAYER_TYPE_NONE, null);
Adam Cohen2801caf2011-05-13 20:57:39 -0700487 mState = STATE_SMALL;
Adam Cohendf2cc412011-04-27 16:56:57 -0700488 }
489 @Override
490 public void onAnimationStart(Animator animation) {
Adam Cohen3371da02011-10-25 21:38:29 -0700491 sendCustomAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED,
Michael Jurka8b805b12012-04-18 14:23:14 -0700492 getContext().getString(R.string.folder_closed));
Adam Cohendf2cc412011-04-27 16:56:57 -0700493 mState = STATE_ANIMATING;
494 }
495 });
Adam Cohen2801caf2011-05-13 20:57:39 -0700496 oa.setDuration(mExpandDuration);
Michael Jurka0121c3e2012-05-31 08:36:04 -0700497 setLayerType(LAYER_TYPE_HARDWARE, null);
498 buildLayer();
499 post(new Runnable() {
500 public void run() {
501 // Check if the animator changed in the meantime
502 if (oa != mOpenCloseAnimator)
503 return;
504 oa.start();
505 }
506 });
Adam Cohendf2cc412011-04-27 16:56:57 -0700507 }
508
509 void notifyDataSetChanged() {
510 // recreate all the children if the data set changes under us. We may want to do this more
511 // intelligently (ie just removing the views that should no longer exist)
512 mContent.removeAllViewsInLayout();
513 bind(mInfo);
514 }
515
Adam Cohencb3382b2011-05-24 14:07:08 -0700516 public boolean acceptDrop(DragObject d) {
517 final ItemInfo item = (ItemInfo) d.dragInfo;
Adam Cohendf2cc412011-04-27 16:56:57 -0700518 final int itemType = item.itemType;
Adam Cohen2801caf2011-05-13 20:57:39 -0700519 return ((itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION ||
520 itemType == LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT) &&
521 !isFull());
Adam Cohendf2cc412011-04-27 16:56:57 -0700522 }
523
Adam Cohendf2cc412011-04-27 16:56:57 -0700524 protected boolean findAndSetEmptyCells(ShortcutInfo item) {
525 int[] emptyCell = new int[2];
526 if (mContent.findCellForSpan(emptyCell, item.spanX, item.spanY)) {
527 item.cellX = emptyCell[0];
528 item.cellY = emptyCell[1];
Adam Cohendf2cc412011-04-27 16:56:57 -0700529 return true;
530 } else {
531 return false;
532 }
533 }
534
Adam Cohenc508b2d2011-06-28 14:41:44 -0700535 protected boolean createAndAddShortcut(ShortcutInfo item) {
Adam Cohendf2cc412011-04-27 16:56:57 -0700536 final TextView textView =
Adam Cohene87b9242011-06-29 14:01:26 -0700537 (TextView) mInflater.inflate(R.layout.application, this, false);
Adam Cohendf2cc412011-04-27 16:56:57 -0700538 textView.setCompoundDrawablesWithIntrinsicBounds(null,
539 new FastBitmapDrawable(item.getIcon(mIconCache)), null, null);
540 textView.setText(item.title);
541 textView.setTag(item);
542
543 textView.setOnClickListener(this);
544 textView.setOnLongClickListener(this);
545
Adam Cohenc508b2d2011-06-28 14:41:44 -0700546 // We need to check here to verify that the given item's location isn't already occupied
Adam Cohen3bf84d32012-05-07 20:17:14 -0700547 // by another item.
Adam Cohen0057bbc2011-08-12 18:30:51 -0700548 if (mContent.getChildAt(item.cellX, item.cellY) != null || item.cellX < 0 || item.cellY < 0
549 || item.cellX >= mContent.getCountX() || item.cellY >= mContent.getCountY()) {
Adam Cohen3bf84d32012-05-07 20:17:14 -0700550 // This shouldn't happen, log it.
551 Log.e(TAG, "Folder order not properly persisted during bind");
Adam Cohenc508b2d2011-06-28 14:41:44 -0700552 if (!findAndSetEmptyCells(item)) {
553 return false;
554 }
555 }
556
Adam Cohendf2cc412011-04-27 16:56:57 -0700557 CellLayout.LayoutParams lp =
558 new CellLayout.LayoutParams(item.cellX, item.cellY, item.spanX, item.spanY);
559 boolean insert = false;
Adam Cohenac56cff2011-09-28 20:45:37 -0700560 textView.setOnKeyListener(new FolderKeyEventListener());
Adam Cohendf2cc412011-04-27 16:56:57 -0700561 mContent.addViewToCellLayout(textView, insert ? 0 : -1, (int)item.id, lp, true);
Adam Cohenc508b2d2011-06-28 14:41:44 -0700562 return true;
Adam Cohendf2cc412011-04-27 16:56:57 -0700563 }
564
Adam Cohencb3382b2011-05-24 14:07:08 -0700565 public void onDragEnter(DragObject d) {
Adam Cohenbfbfd262011-06-13 16:55:12 -0700566 mPreviousTargetCell[0] = -1;
567 mPreviousTargetCell[1] = -1;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700568 mOnExitAlarm.cancelAlarm();
569 }
570
571 OnAlarmListener mReorderAlarmListener = new OnAlarmListener() {
572 public void onAlarm(Alarm alarm) {
573 realTimeReorder(mEmptyCell, mTargetCell);
574 }
575 };
576
577 boolean readingOrderGreaterThan(int[] v1, int[] v2) {
578 if (v1[1] > v2[1] || (v1[1] == v2[1] && v1[0] > v2[0])) {
579 return true;
580 } else {
581 return false;
582 }
583 }
584
585 private void realTimeReorder(int[] empty, int[] target) {
586 boolean wrap;
587 int startX;
588 int endX;
589 int startY;
Adam Cohen76fc0852011-06-17 13:26:23 -0700590 int delay = 0;
591 float delayAmount = 30;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700592 if (readingOrderGreaterThan(target, empty)) {
593 wrap = empty[0] >= mContent.getCountX() - 1;
594 startY = wrap ? empty[1] + 1 : empty[1];
595 for (int y = startY; y <= target[1]; y++) {
596 startX = y == empty[1] ? empty[0] + 1 : 0;
597 endX = y < target[1] ? mContent.getCountX() - 1 : target[0];
598 for (int x = startX; x <= endX; x++) {
599 View v = mContent.getChildAt(x,y);
600 if (mContent.animateChildToPosition(v, empty[0], empty[1],
Adam Cohen482ed822012-03-02 14:15:13 -0800601 REORDER_ANIMATION_DURATION, delay, true, true)) {
Adam Cohenbfbfd262011-06-13 16:55:12 -0700602 empty[0] = x;
603 empty[1] = y;
Adam Cohen76fc0852011-06-17 13:26:23 -0700604 delay += delayAmount;
605 delayAmount *= 0.9;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700606 }
607 }
608 }
609 } else {
610 wrap = empty[0] == 0;
611 startY = wrap ? empty[1] - 1 : empty[1];
612 for (int y = startY; y >= target[1]; y--) {
613 startX = y == empty[1] ? empty[0] - 1 : mContent.getCountX() - 1;
614 endX = y > target[1] ? 0 : target[0];
615 for (int x = startX; x >= endX; x--) {
616 View v = mContent.getChildAt(x,y);
617 if (mContent.animateChildToPosition(v, empty[0], empty[1],
Adam Cohen482ed822012-03-02 14:15:13 -0800618 REORDER_ANIMATION_DURATION, delay, true, true)) {
Adam Cohenbfbfd262011-06-13 16:55:12 -0700619 empty[0] = x;
620 empty[1] = y;
Adam Cohen76fc0852011-06-17 13:26:23 -0700621 delay += delayAmount;
622 delayAmount *= 0.9;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700623 }
624 }
625 }
626 }
Adam Cohendf2cc412011-04-27 16:56:57 -0700627 }
628
Adam Cohencb3382b2011-05-24 14:07:08 -0700629 public void onDragOver(DragObject d) {
Adam Cohenbfbfd262011-06-13 16:55:12 -0700630 float[] r = getDragViewVisualCenter(d.x, d.y, d.xOffset, d.yOffset, d.dragView, null);
631 mTargetCell = mContent.findNearestArea((int) r[0], (int) r[1], 1, 1, mTargetCell);
632
633 if (mTargetCell[0] != mPreviousTargetCell[0] || mTargetCell[1] != mPreviousTargetCell[1]) {
634 mReorderAlarm.cancelAlarm();
635 mReorderAlarm.setOnAlarmListener(mReorderAlarmListener);
636 mReorderAlarm.setAlarm(150);
637 mPreviousTargetCell[0] = mTargetCell[0];
638 mPreviousTargetCell[1] = mTargetCell[1];
639 }
Adam Cohendf2cc412011-04-27 16:56:57 -0700640 }
641
Adam Cohenbfbfd262011-06-13 16:55:12 -0700642 // This is used to compute the visual center of the dragView. The idea is that
643 // the visual center represents the user's interpretation of where the item is, and hence
644 // is the appropriate point to use when determining drop location.
645 private float[] getDragViewVisualCenter(int x, int y, int xOffset, int yOffset,
646 DragView dragView, float[] recycle) {
647 float res[];
648 if (recycle == null) {
649 res = new float[2];
650 } else {
651 res = recycle;
652 }
653
654 // These represent the visual top and left of drag view if a dragRect was provided.
655 // If a dragRect was not provided, then they correspond to the actual view left and
656 // top, as the dragRect is in that case taken to be the entire dragView.
657 // R.dimen.dragViewOffsetY.
658 int left = x - xOffset;
659 int top = y - yOffset;
660
661 // In order to find the visual center, we shift by half the dragRect
662 res[0] = left + dragView.getDragRegion().width() / 2;
663 res[1] = top + dragView.getDragRegion().height() / 2;
664
665 return res;
666 }
667
668 OnAlarmListener mOnExitAlarmListener = new OnAlarmListener() {
669 public void onAlarm(Alarm alarm) {
Adam Cohen3e8f8112011-07-02 18:03:00 -0700670 completeDragExit();
Adam Cohenbfbfd262011-06-13 16:55:12 -0700671 }
672 };
673
Adam Cohen95bb8002011-07-03 23:40:28 -0700674 public void completeDragExit() {
Adam Cohen3e8f8112011-07-02 18:03:00 -0700675 mLauncher.closeFolder();
676 mCurrentDragInfo = null;
677 mCurrentDragView = null;
678 mSuppressOnAdd = false;
679 mRearrangeOnClose = true;
680 }
681
Adam Cohencb3382b2011-05-24 14:07:08 -0700682 public void onDragExit(DragObject d) {
Adam Cohenbfbfd262011-06-13 16:55:12 -0700683 // We only close the folder if this is a true drag exit, ie. not because a drop
684 // has occurred above the folder.
685 if (!d.dragComplete) {
686 mOnExitAlarm.setOnAlarmListener(mOnExitAlarmListener);
687 mOnExitAlarm.setAlarm(ON_EXIT_CLOSE_DELAY);
688 }
689 mReorderAlarm.cancelAlarm();
Adam Cohen2801caf2011-05-13 20:57:39 -0700690 }
691
Winson Chunga48487a2012-03-20 16:19:37 -0700692 public void onDropCompleted(View target, DragObject d, boolean isFlingToDelete,
693 boolean success) {
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700694 if (success) {
Adam Cohen05e0f402011-08-01 12:12:49 -0700695 if (mDeleteFolderOnDropCompleted && !mItemAddedBackToSelfViaIcon) {
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700696 replaceFolderWithFinalItem();
697 }
698 } else {
699 // The drag failed, we need to return the item to the folder
700 mFolderIcon.onDrop(d);
701
702 // We're going to trigger a "closeFolder" which may occur before this item has
703 // been added back to the folder -- this could cause the folder to be deleted
704 if (mOnExitAlarm.alarmPending()) {
705 mSuppressFolderDeletion = true;
706 }
707 }
708
709 if (target != this) {
710 if (mOnExitAlarm.alarmPending()) {
711 mOnExitAlarm.cancelAlarm();
712 completeDragExit();
713 }
714 }
715 mDeleteFolderOnDropCompleted = false;
716 mDragInProgress = false;
Adam Cohen05e0f402011-08-01 12:12:49 -0700717 mItemAddedBackToSelfViaIcon = false;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700718 mCurrentDragInfo = null;
719 mCurrentDragView = null;
720 mSuppressOnAdd = false;
Adam Cohen4045eb72011-10-06 11:44:26 -0700721
722 // Reordering may have occured, and we need to save the new item locations. We do this once
723 // at the end to prevent unnecessary database operations.
724 updateItemLocationsInDatabase();
725 }
726
Winson Chunga48487a2012-03-20 16:19:37 -0700727 @Override
Winson Chung043f2af2012-03-01 16:09:54 -0800728 public boolean supportsFlingToDelete() {
729 return true;
730 }
731
Winson Chunga48487a2012-03-20 16:19:37 -0700732 public void onFlingToDelete(DragObject d, int x, int y, PointF vec) {
733 // Do nothing
734 }
735
736 @Override
737 public void onFlingToDeleteCompleted() {
738 // Do nothing
739 }
740
Adam Cohen4045eb72011-10-06 11:44:26 -0700741 private void updateItemLocationsInDatabase() {
742 ArrayList<View> list = getItemsInReadingOrder();
743 for (int i = 0; i < list.size(); i++) {
744 View v = list.get(i);
745 ItemInfo info = (ItemInfo) v.getTag();
746 LauncherModel.moveItemInDatabase(mLauncher, info, mInfo.id, 0,
747 info.cellX, info.cellY);
748 }
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700749 }
Adam Cohen228da5a2011-07-27 22:23:47 -0700750
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700751 public void notifyDrop() {
752 if (mDragInProgress) {
Adam Cohen05e0f402011-08-01 12:12:49 -0700753 mItemAddedBackToSelfViaIcon = true;
Adam Cohen76078c42011-06-09 15:06:52 -0700754 }
Adam Cohendf2cc412011-04-27 16:56:57 -0700755 }
756
757 public boolean isDropEnabled() {
758 return true;
759 }
760
Adam Cohencb3382b2011-05-24 14:07:08 -0700761 public DropTarget getDropTargetDelegate(DragObject d) {
Adam Cohendf2cc412011-04-27 16:56:57 -0700762 return null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800763 }
Adam Cohena9cf38f2011-05-02 15:36:58 -0700764
Adam Cohen4045eb72011-10-06 11:44:26 -0700765 private void setupContentDimensions(int count) {
Adam Cohen2801caf2011-05-13 20:57:39 -0700766 ArrayList<View> list = getItemsInReadingOrder();
767
768 int countX = mContent.getCountX();
769 int countY = mContent.getCountY();
Adam Cohen7c693212011-05-18 15:26:57 -0700770 boolean done = false;
Adam Cohen2801caf2011-05-13 20:57:39 -0700771
Adam Cohen7c693212011-05-18 15:26:57 -0700772 while (!done) {
773 int oldCountX = countX;
774 int oldCountY = countY;
775 if (countX * countY < count) {
776 // Current grid is too small, expand it
Adam Cohen78dc83e2011-11-15 17:10:00 -0800777 if ((countX <= countY || countY == mMaxCountY) && countX < mMaxCountX) {
Adam Cohen7c693212011-05-18 15:26:57 -0700778 countX++;
779 } else if (countY < mMaxCountY) {
780 countY++;
781 }
782 if (countY == 0) countY++;
783 } else if ((countY - 1) * countX >= count && countY >= countX) {
784 countY = Math.max(0, countY - 1);
785 } else if ((countX - 1) * countY >= count) {
786 countX = Math.max(0, countX - 1);
Adam Cohen2801caf2011-05-13 20:57:39 -0700787 }
Adam Cohen7c693212011-05-18 15:26:57 -0700788 done = countX == oldCountX && countY == oldCountY;
Adam Cohen2801caf2011-05-13 20:57:39 -0700789 }
Adam Cohen7c693212011-05-18 15:26:57 -0700790 mContent.setGridSize(countX, countY);
Adam Cohen2801caf2011-05-13 20:57:39 -0700791 arrangeChildren(list);
792 }
793
794 public boolean isFull() {
Adam Cohen78dc83e2011-11-15 17:10:00 -0800795 return getItemCount() >= mMaxNumItems;
Adam Cohen2801caf2011-05-13 20:57:39 -0700796 }
797
798 private void centerAboutIcon() {
Adam Cohen8e776a62011-06-28 18:10:06 -0700799 DragLayer.LayoutParams lp = (DragLayer.LayoutParams) getLayoutParams();
Adam Cohen2801caf2011-05-13 20:57:39 -0700800
801 int width = getPaddingLeft() + getPaddingRight() + mContent.getDesiredWidth();
Adam Cohenf4bb1cd2011-07-22 14:36:03 -0700802 int height = getPaddingTop() + getPaddingBottom() + mContent.getDesiredHeight()
803 + mFolderNameHeight;
Adam Cohen8e776a62011-06-28 18:10:06 -0700804 DragLayer parent = (DragLayer) mLauncher.findViewById(R.id.drag_layer);
Adam Cohen2801caf2011-05-13 20:57:39 -0700805
Adam Cohen8e776a62011-06-28 18:10:06 -0700806 parent.getDescendantRectRelativeToSelf(mFolderIcon, mTempRect);
807
808 int centerX = mTempRect.centerX();
809 int centerY = mTempRect.centerY();
Adam Cohen2801caf2011-05-13 20:57:39 -0700810 int centeredLeft = centerX - width / 2;
811 int centeredTop = centerY - height / 2;
812
Adam Cohen7cc1bc42011-12-13 21:28:43 -0800813 int currentPage = mLauncher.getWorkspace().getCurrentPage();
814 // In case the workspace is scrolling, we need to use the final scroll to compute
815 // the folders bounds.
816 mLauncher.getWorkspace().setFinalScrollForPageChange(currentPage);
Adam Cohen35e7e642011-07-17 14:47:18 -0700817 // We first fetch the currently visible CellLayoutChildren
Adam Cohen7cc1bc42011-12-13 21:28:43 -0800818 CellLayout currentLayout = (CellLayout) mLauncher.getWorkspace().getChildAt(currentPage);
Michael Jurkaa52570f2012-03-20 03:18:20 -0700819 ShortcutAndWidgetContainer boundingLayout = currentLayout.getShortcutsAndWidgets();
Adam Cohen35e7e642011-07-17 14:47:18 -0700820 Rect bounds = new Rect();
821 parent.getDescendantRectRelativeToSelf(boundingLayout, bounds);
Adam Cohen7cc1bc42011-12-13 21:28:43 -0800822 // We reset the workspaces scroll
823 mLauncher.getWorkspace().resetFinalScrollForPageChange(currentPage);
Adam Cohen2801caf2011-05-13 20:57:39 -0700824
Adam Cohen35e7e642011-07-17 14:47:18 -0700825 // We need to bound the folder to the currently visible CellLayoutChildren
826 int left = Math.min(Math.max(bounds.left, centeredLeft),
827 bounds.left + bounds.width() - width);
828 int top = Math.min(Math.max(bounds.top, centeredTop),
829 bounds.top + bounds.height() - height);
830 // If the folder doesn't fit within the bounds, center it about the desired bounds
831 if (width >= bounds.width()) {
832 left = bounds.left + (bounds.width() - width) / 2;
Adam Cohen0e4857c2011-06-30 17:05:14 -0700833 }
Adam Cohen35e7e642011-07-17 14:47:18 -0700834 if (height >= bounds.height()) {
835 top = bounds.top + (bounds.height() - height) / 2;
Adam Cohen0e4857c2011-06-30 17:05:14 -0700836 }
Adam Cohen2801caf2011-05-13 20:57:39 -0700837
838 int folderPivotX = width / 2 + (centeredLeft - left);
839 int folderPivotY = height / 2 + (centeredTop - top);
840 setPivotX(folderPivotX);
841 setPivotY(folderPivotY);
842 int folderIconPivotX = (int) (mFolderIcon.getMeasuredWidth() *
843 (1.0f * folderPivotX / width));
844 int folderIconPivotY = (int) (mFolderIcon.getMeasuredHeight() *
845 (1.0f * folderPivotY / height));
Adam Cohen3bf84d32012-05-07 20:17:14 -0700846
Adam Cohen2801caf2011-05-13 20:57:39 -0700847 mFolderIcon.setPivotX(folderIconPivotX);
848 mFolderIcon.setPivotY(folderIconPivotY);
849
Adam Cohen662b5982011-12-13 17:45:21 -0800850 lp.width = width;
851 lp.height = height;
852 lp.x = left;
853 lp.y = top;
Adam Cohen2801caf2011-05-13 20:57:39 -0700854 }
855
856 private void setupContentForNumItems(int count) {
Adam Cohen4045eb72011-10-06 11:44:26 -0700857 setupContentDimensions(count);
Adam Cohen2801caf2011-05-13 20:57:39 -0700858
Adam Cohen8e776a62011-06-28 18:10:06 -0700859 DragLayer.LayoutParams lp = (DragLayer.LayoutParams) getLayoutParams();
Adam Cohen2801caf2011-05-13 20:57:39 -0700860 if (lp == null) {
Adam Cohen8e776a62011-06-28 18:10:06 -0700861 lp = new DragLayer.LayoutParams(0, 0);
862 lp.customPosition = true;
Adam Cohen2801caf2011-05-13 20:57:39 -0700863 setLayoutParams(lp);
864 }
865 centerAboutIcon();
866 }
867
Adam Cohen234c4cd2011-07-17 21:03:04 -0700868 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
869 int width = getPaddingLeft() + getPaddingRight() + mContent.getDesiredWidth();
Adam Cohenf4bb1cd2011-07-22 14:36:03 -0700870 int height = getPaddingTop() + getPaddingBottom() + mContent.getDesiredHeight()
871 + mFolderNameHeight;
Adam Cohen234c4cd2011-07-17 21:03:04 -0700872
873 int contentWidthSpec = MeasureSpec.makeMeasureSpec(mContent.getDesiredWidth(),
874 MeasureSpec.EXACTLY);
875 int contentHeightSpec = MeasureSpec.makeMeasureSpec(mContent.getDesiredHeight(),
876 MeasureSpec.EXACTLY);
877 mContent.measure(contentWidthSpec, contentHeightSpec);
878
879 mFolderName.measure(contentWidthSpec,
880 MeasureSpec.makeMeasureSpec(mFolderNameHeight, MeasureSpec.EXACTLY));
881 setMeasuredDimension(width, height);
882 }
883
Adam Cohen2801caf2011-05-13 20:57:39 -0700884 private void arrangeChildren(ArrayList<View> list) {
885 int[] vacant = new int[2];
886 if (list == null) {
887 list = getItemsInReadingOrder();
888 }
889 mContent.removeAllViews();
890
891 for (int i = 0; i < list.size(); i++) {
892 View v = list.get(i);
893 mContent.getVacantCell(vacant, 1, 1);
894 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) v.getLayoutParams();
895 lp.cellX = vacant[0];
896 lp.cellY = vacant[1];
897 ItemInfo info = (ItemInfo) v.getTag();
Adam Cohen2792a332011-09-26 21:09:47 -0700898 if (info.cellX != vacant[0] || info.cellY != vacant[1]) {
899 info.cellX = vacant[0];
900 info.cellY = vacant[1];
901 LauncherModel.addOrMoveItemInDatabase(mLauncher, info, mInfo.id, 0,
902 info.cellX, info.cellY);
903 }
Adam Cohen2801caf2011-05-13 20:57:39 -0700904 boolean insert = false;
905 mContent.addViewToCellLayout(v, insert ? 0 : -1, (int)info.id, lp, true);
906 }
Adam Cohen7c693212011-05-18 15:26:57 -0700907 mItemsInvalidated = true;
Adam Cohen2801caf2011-05-13 20:57:39 -0700908 }
909
Adam Cohena9cf38f2011-05-02 15:36:58 -0700910 public int getItemCount() {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700911 return mContent.getShortcutsAndWidgets().getChildCount();
Adam Cohena9cf38f2011-05-02 15:36:58 -0700912 }
913
914 public View getItemAt(int index) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700915 return mContent.getShortcutsAndWidgets().getChildAt(index);
Adam Cohena9cf38f2011-05-02 15:36:58 -0700916 }
917
Adam Cohen2801caf2011-05-13 20:57:39 -0700918 private void onCloseComplete() {
Adam Cohen05e0f402011-08-01 12:12:49 -0700919 DragLayer parent = (DragLayer) getParent();
Adam Cohen4554ee12011-08-03 16:13:21 -0700920 parent.removeView(this);
921 mDragController.removeDropTarget((DropTarget) this);
Adam Cohen05e0f402011-08-01 12:12:49 -0700922 clearFocus();
Adam Cohenac56cff2011-09-28 20:45:37 -0700923 mFolderIcon.requestFocus();
Adam Cohen05e0f402011-08-01 12:12:49 -0700924
Adam Cohen2801caf2011-05-13 20:57:39 -0700925 if (mRearrangeOnClose) {
926 setupContentForNumItems(getItemCount());
927 mRearrangeOnClose = false;
928 }
Adam Cohenafb01ee2011-06-23 15:38:03 -0700929 if (getItemCount() <= 1) {
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700930 if (!mDragInProgress && !mSuppressFolderDeletion) {
931 replaceFolderWithFinalItem();
932 } else if (mDragInProgress) {
933 mDeleteFolderOnDropCompleted = true;
934 }
Adam Cohenafb01ee2011-06-23 15:38:03 -0700935 }
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700936 mSuppressFolderDeletion = false;
Adam Cohenafb01ee2011-06-23 15:38:03 -0700937 }
938
939 private void replaceFolderWithFinalItem() {
940 ItemInfo finalItem = null;
941
942 if (getItemCount() == 1) {
943 finalItem = mInfo.contents.get(0);
944 }
945
946 // Remove the folder completely
Winson Chung3d503fb2011-07-13 17:25:49 -0700947 CellLayout cellLayout = mLauncher.getCellLayout(mInfo.container, mInfo.screen);
Adam Cohenafb01ee2011-06-23 15:38:03 -0700948 cellLayout.removeView(mFolderIcon);
949 if (mFolderIcon instanceof DropTarget) {
950 mDragController.removeDropTarget((DropTarget) mFolderIcon);
951 }
952 mLauncher.removeFolder(mInfo);
953
954 if (finalItem != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -0700955 LauncherModel.addOrMoveItemInDatabase(mLauncher, finalItem, mInfo.container,
956 mInfo.screen, mInfo.cellX, mInfo.cellY);
Adam Cohenafb01ee2011-06-23 15:38:03 -0700957 }
Adam Cohen716b51e2011-06-30 12:09:54 -0700958 LauncherModel.deleteItemFromDatabase(mLauncher, mInfo);
Adam Cohenafb01ee2011-06-23 15:38:03 -0700959
960 // Add the last remaining child to the workspace in place of the folder
961 if (finalItem != null) {
962 View child = mLauncher.createShortcut(R.layout.application, cellLayout,
963 (ShortcutInfo) finalItem);
964
Winson Chung3d503fb2011-07-13 17:25:49 -0700965 mLauncher.getWorkspace().addInScreen(child, mInfo.container, mInfo.screen, mInfo.cellX,
966 mInfo.cellY, mInfo.spanX, mInfo.spanY);
Adam Cohenafb01ee2011-06-23 15:38:03 -0700967 }
Adam Cohen2801caf2011-05-13 20:57:39 -0700968 }
969
Adam Cohenac56cff2011-09-28 20:45:37 -0700970 // This method keeps track of the last item in the folder for the purposes
971 // of keyboard focus
972 private void updateTextViewFocus() {
973 View lastChild = getItemAt(getItemCount() - 1);
974 getItemAt(getItemCount() - 1);
975 if (lastChild != null) {
976 mFolderName.setNextFocusDownId(lastChild.getId());
977 mFolderName.setNextFocusRightId(lastChild.getId());
978 mFolderName.setNextFocusLeftId(lastChild.getId());
979 mFolderName.setNextFocusUpId(lastChild.getId());
980 }
981 }
982
Adam Cohenbfbfd262011-06-13 16:55:12 -0700983 public void onDrop(DragObject d) {
984 ShortcutInfo item;
985 if (d.dragInfo instanceof ApplicationInfo) {
986 // Came from all apps -- make a copy
987 item = ((ApplicationInfo) d.dragInfo).makeShortcut();
988 item.spanX = 1;
989 item.spanY = 1;
990 } else {
991 item = (ShortcutInfo) d.dragInfo;
992 }
Adam Cohen05e0f402011-08-01 12:12:49 -0700993 // Dragged from self onto self, currently this is the only path possible, however
994 // we keep this as a distinct code path.
Adam Cohenbfbfd262011-06-13 16:55:12 -0700995 if (item == mCurrentDragInfo) {
996 ShortcutInfo si = (ShortcutInfo) mCurrentDragView.getTag();
997 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) mCurrentDragView.getLayoutParams();
998 si.cellX = lp.cellX = mEmptyCell[0];
999 si.cellX = lp.cellY = mEmptyCell[1];
1000 mContent.addViewToCellLayout(mCurrentDragView, -1, (int)item.id, lp, true);
Adam Cohenfc53cd22011-07-20 15:45:11 -07001001 if (d.dragView.hasDrawn()) {
1002 mLauncher.getDragLayer().animateViewIntoPosition(d.dragView, mCurrentDragView);
1003 } else {
Winson Chung2c4cf412012-05-08 14:03:21 -07001004 d.deferDragViewCleanupPostAnimation = false;
Adam Cohenfc53cd22011-07-20 15:45:11 -07001005 mCurrentDragView.setVisibility(VISIBLE);
1006 }
Adam Cohene9166b22011-07-08 17:11:11 -07001007 mItemsInvalidated = true;
Adam Cohen4045eb72011-10-06 11:44:26 -07001008 setupContentDimensions(getItemCount());
Adam Cohen716b51e2011-06-30 12:09:54 -07001009 mSuppressOnAdd = true;
Adam Cohenbfbfd262011-06-13 16:55:12 -07001010 }
1011 mInfo.add(item);
1012 }
1013
1014 public void onAdd(ShortcutInfo item) {
1015 mItemsInvalidated = true;
Adam Cohen05e0f402011-08-01 12:12:49 -07001016 // If the item was dropped onto this open folder, we have done the work associated
1017 // with adding the item to the folder, as indicated by mSuppressOnAdd being set
Adam Cohenbfbfd262011-06-13 16:55:12 -07001018 if (mSuppressOnAdd) return;
1019 if (!findAndSetEmptyCells(item)) {
1020 // The current layout is full, can we expand it?
1021 setupContentForNumItems(getItemCount() + 1);
1022 findAndSetEmptyCells(item);
1023 }
1024 createAndAddShortcut(item);
1025 LauncherModel.addOrMoveItemInDatabase(
1026 mLauncher, item, mInfo.id, 0, item.cellX, item.cellY);
1027 }
1028
Adam Cohena9cf38f2011-05-02 15:36:58 -07001029 public void onRemove(ShortcutInfo item) {
Adam Cohen7c693212011-05-18 15:26:57 -07001030 mItemsInvalidated = true;
Adam Cohen05e0f402011-08-01 12:12:49 -07001031 // If this item is being dragged from this open folder, we have already handled
1032 // the work associated with removing the item, so we don't have to do anything here.
Adam Cohenbfbfd262011-06-13 16:55:12 -07001033 if (item == mCurrentDragInfo) return;
Adam Cohendf1e4e82011-06-24 15:57:39 -07001034 View v = getViewForInfo(item);
Adam Cohena9cf38f2011-05-02 15:36:58 -07001035 mContent.removeView(v);
Adam Cohen2801caf2011-05-13 20:57:39 -07001036 if (mState == STATE_ANIMATING) {
1037 mRearrangeOnClose = true;
1038 } else {
1039 setupContentForNumItems(getItemCount());
1040 }
Adam Cohenafb01ee2011-06-23 15:38:03 -07001041 if (getItemCount() <= 1) {
1042 replaceFolderWithFinalItem();
1043 }
Adam Cohena9cf38f2011-05-02 15:36:58 -07001044 }
Adam Cohen7c693212011-05-18 15:26:57 -07001045
Adam Cohendf1e4e82011-06-24 15:57:39 -07001046 private View getViewForInfo(ShortcutInfo item) {
1047 for (int j = 0; j < mContent.getCountY(); j++) {
1048 for (int i = 0; i < mContent.getCountX(); i++) {
1049 View v = mContent.getChildAt(i, j);
1050 if (v.getTag() == item) {
1051 return v;
1052 }
1053 }
1054 }
1055 return null;
1056 }
1057
Adam Cohen76078c42011-06-09 15:06:52 -07001058 public void onItemsChanged() {
Adam Cohenac56cff2011-09-28 20:45:37 -07001059 updateTextViewFocus();
Adam Cohen76078c42011-06-09 15:06:52 -07001060 }
Adam Cohenac56cff2011-09-28 20:45:37 -07001061
Adam Cohen76fc0852011-06-17 13:26:23 -07001062 public void onTitleChanged(CharSequence title) {
1063 }
Adam Cohen76078c42011-06-09 15:06:52 -07001064
Adam Cohen7c693212011-05-18 15:26:57 -07001065 public ArrayList<View> getItemsInReadingOrder() {
Adam Cohen76078c42011-06-09 15:06:52 -07001066 return getItemsInReadingOrder(true);
1067 }
1068
1069 public ArrayList<View> getItemsInReadingOrder(boolean includeCurrentDragItem) {
Adam Cohen7c693212011-05-18 15:26:57 -07001070 if (mItemsInvalidated) {
1071 mItemsInReadingOrder.clear();
1072 for (int j = 0; j < mContent.getCountY(); j++) {
1073 for (int i = 0; i < mContent.getCountX(); i++) {
1074 View v = mContent.getChildAt(i, j);
1075 if (v != null) {
Adam Cohen76078c42011-06-09 15:06:52 -07001076 ShortcutInfo info = (ShortcutInfo) v.getTag();
1077 if (info != mCurrentDragInfo || includeCurrentDragItem) {
1078 mItemsInReadingOrder.add(v);
1079 }
Adam Cohen7c693212011-05-18 15:26:57 -07001080 }
1081 }
1082 }
1083 mItemsInvalidated = false;
1084 }
1085 return mItemsInReadingOrder;
1086 }
Adam Cohen8dfcba42011-07-07 16:38:18 -07001087
1088 public void getLocationInDragLayer(int[] loc) {
1089 mLauncher.getDragLayer().getLocationInDragLayer(this, loc);
1090 }
Adam Cohenea0818d2011-09-30 20:06:58 -07001091
1092 public void onFocusChange(View v, boolean hasFocus) {
1093 if (v == mFolderName && hasFocus) {
1094 startEditingFolderName();
1095 }
1096 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001097}