blob: 4669918fff0425b619d9fe38970a5e35218e60ee [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 Cohen268c4752012-06-06 17:47:33 -0700103 private float mFolderIconPivotX;
104 private float mFolderIconPivotY;
Adam Cohen228da5a2011-07-27 22:23:47 -0700105
Adam Cohen76fc0852011-06-17 13:26:23 -0700106 private boolean mIsEditingName = false;
107 private InputMethodManager mInputMethodManager;
Adam Cohendf2cc412011-04-27 16:56:57 -0700108
Adam Cohena65beee2011-06-27 21:32:23 -0700109 private static String sDefaultFolderName;
110 private static String sHintText;
Michael Jurka0121c3e2012-05-31 08:36:04 -0700111 private ObjectAnimator mOpenCloseAnimator;
Adam Cohena65beee2011-06-27 21:32:23 -0700112
Adam Cohenfb91f302012-06-11 15:45:18 -0700113 private boolean mDestroyed;
114
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800115 /**
116 * Used to inflate the Workspace from XML.
117 *
118 * @param context The application's context.
119 * @param attrs The attribtues set containing the Workspace's customization values.
120 */
121 public Folder(Context context, AttributeSet attrs) {
122 super(context, attrs);
123 setAlwaysDrawnWithCacheEnabled(false);
Adam Cohendf2cc412011-04-27 16:56:57 -0700124 mInflater = LayoutInflater.from(context);
125 mIconCache = ((LauncherApplication)context.getApplicationContext()).getIconCache();
Adam Cohen78dc83e2011-11-15 17:10:00 -0800126
127 Resources res = getResources();
128 mMaxCountX = res.getInteger(R.integer.folder_max_count_x);
129 mMaxCountY = res.getInteger(R.integer.folder_max_count_y);
130 mMaxNumItems = res.getInteger(R.integer.folder_max_num_items);
131 if (mMaxCountX < 0 || mMaxCountY < 0 || mMaxNumItems < 0) {
132 mMaxCountX = LauncherModel.getCellCountX();
133 mMaxCountY = LauncherModel.getCellCountY();
134 mMaxNumItems = mMaxCountX * mMaxCountY;
135 }
Adam Cohen76fc0852011-06-17 13:26:23 -0700136
137 mInputMethodManager = (InputMethodManager)
Michael Jurka8b805b12012-04-18 14:23:14 -0700138 getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
Adam Cohen76fc0852011-06-17 13:26:23 -0700139
Adam Cohen76fc0852011-06-17 13:26:23 -0700140 mExpandDuration = res.getInteger(R.integer.config_folderAnimDuration);
Adam Cohen4ef610f2011-06-21 22:37:36 -0700141
142 if (sDefaultFolderName == null) {
143 sDefaultFolderName = res.getString(R.string.folder_name);
144 }
Adam Cohena65beee2011-06-27 21:32:23 -0700145 if (sHintText == null) {
146 sHintText = res.getString(R.string.folder_hint_text);
147 }
Adam Cohen4eac29a2011-07-11 17:53:37 -0700148 mLauncher = (Launcher) context;
Adam Cohenac56cff2011-09-28 20:45:37 -0700149 // We need this view to be focusable in touch mode so that when text editing of the folder
150 // name is complete, we have something to focus on, thus hiding the cursor and giving
151 // reliable behvior when clicking the text field (since it will always gain focus on click).
152 setFocusableInTouchMode(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800153 }
154
155 @Override
156 protected void onFinishInflate() {
157 super.onFinishInflate();
Adam Cohendf2cc412011-04-27 16:56:57 -0700158 mContent = (CellLayout) findViewById(R.id.folder_content);
Adam Cohen2801caf2011-05-13 20:57:39 -0700159 mContent.setGridSize(0, 0);
Michael Jurkaa52570f2012-03-20 03:18:20 -0700160 mContent.getShortcutsAndWidgets().setMotionEventSplittingEnabled(false);
Adam Cohenac56cff2011-09-28 20:45:37 -0700161 mFolderName = (FolderEditText) findViewById(R.id.folder_name);
162 mFolderName.setFolder(this);
Adam Cohenea0818d2011-09-30 20:06:58 -0700163 mFolderName.setOnFocusChangeListener(this);
Adam Cohen76fc0852011-06-17 13:26:23 -0700164
165 // We find out how tall the text view wants to be (it is set to wrap_content), so that
166 // we can allocate the appropriate amount of space for it.
167 int measureSpec = MeasureSpec.UNSPECIFIED;
168 mFolderName.measure(measureSpec, measureSpec);
169 mFolderNameHeight = mFolderName.getMeasuredHeight();
170
171 // We disable action mode for now since it messes up the view on phones
172 mFolderName.setCustomSelectionActionModeCallback(mActionModeCallback);
Adam Cohen76fc0852011-06-17 13:26:23 -0700173 mFolderName.setOnEditorActionListener(this);
Adam Cohen4ef610f2011-06-21 22:37:36 -0700174 mFolderName.setSelectAllOnFocus(true);
Adam Cohen7a14d0b2011-06-24 11:36:35 -0700175 mFolderName.setInputType(mFolderName.getInputType() |
176 InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS | InputType.TYPE_TEXT_FLAG_CAP_WORDS);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800177 }
Adam Cohen2801caf2011-05-13 20:57:39 -0700178
Adam Cohen76fc0852011-06-17 13:26:23 -0700179 private ActionMode.Callback mActionModeCallback = new ActionMode.Callback() {
180 public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
181 return false;
182 }
183
184 public boolean onCreateActionMode(ActionMode mode, Menu menu) {
185 return false;
186 }
187
188 public void onDestroyActionMode(ActionMode mode) {
189 }
190
191 public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
192 return false;
193 }
194 };
195
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800196 public void onClick(View v) {
Adam Cohendf2cc412011-04-27 16:56:57 -0700197 Object tag = v.getTag();
198 if (tag instanceof ShortcutInfo) {
199 // refactor this code from Folder
200 ShortcutInfo item = (ShortcutInfo) tag;
201 int[] pos = new int[2];
202 v.getLocationOnScreen(pos);
203 item.intent.setSourceBounds(new Rect(pos[0], pos[1],
204 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Winson Chungc7450e32012-04-17 17:34:08 -0700205
206 mLauncher.startActivitySafely(v, item.intent, item);
Adam Cohendf2cc412011-04-27 16:56:57 -0700207 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800208 }
209
210 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -0700211 // Return if global dragging is not enabled
212 if (!mLauncher.isDraggingEnabled()) return true;
213
Adam Cohendf2cc412011-04-27 16:56:57 -0700214 Object tag = v.getTag();
215 if (tag instanceof ShortcutInfo) {
Adam Cohendf2cc412011-04-27 16:56:57 -0700216 ShortcutInfo item = (ShortcutInfo) tag;
217 if (!v.isInTouchMode()) {
218 return false;
219 }
220
Winson Chung7d7541e2011-09-16 20:14:36 -0700221 mLauncher.dismissFolderCling(null);
222
Adam Cohendf2cc412011-04-27 16:56:57 -0700223 mLauncher.getWorkspace().onDragStartedWithItem(v);
Adam Cohenac8c8762011-07-13 11:15:27 -0700224 mLauncher.getWorkspace().beginDragShared(v, this);
Adam Cohenbadf71e2011-05-26 19:08:29 -0700225 mIconDrawable = ((TextView) v).getCompoundDrawables()[1];
Adam Cohen76078c42011-06-09 15:06:52 -0700226
227 mCurrentDragInfo = item;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700228 mEmptyCell[0] = item.cellX;
229 mEmptyCell[1] = item.cellY;
230 mCurrentDragView = v;
Adam Cohenfc53cd22011-07-20 15:45:11 -0700231
232 mContent.removeView(mCurrentDragView);
233 mInfo.remove(mCurrentDragInfo);
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700234 mDragInProgress = true;
Adam Cohen05e0f402011-08-01 12:12:49 -0700235 mItemAddedBackToSelfViaIcon = false;
Adam Cohendf2cc412011-04-27 16:56:57 -0700236 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800237 return true;
238 }
239
Adam Cohen76fc0852011-06-17 13:26:23 -0700240 public boolean isEditingName() {
241 return mIsEditingName;
242 }
243
244 public void startEditingFolderName() {
Adam Cohena65beee2011-06-27 21:32:23 -0700245 mFolderName.setHint("");
Adam Cohen76fc0852011-06-17 13:26:23 -0700246 mIsEditingName = true;
247 }
248
249 public void dismissEditingName() {
250 mInputMethodManager.hideSoftInputFromWindow(getWindowToken(), 0);
251 doneEditingFolderName(true);
252 }
253
254 public void doneEditingFolderName(boolean commit) {
Adam Cohena65beee2011-06-27 21:32:23 -0700255 mFolderName.setHint(sHintText);
Adam Cohen1df26a32011-08-12 16:15:23 -0700256 // Convert to a string here to ensure that no other state associated with the text field
257 // gets saved.
Adam Cohen3371da02011-10-25 21:38:29 -0700258 String newTitle = mFolderName.getText().toString();
259 mInfo.setTitle(newTitle);
Adam Cohen76fc0852011-06-17 13:26:23 -0700260 LauncherModel.updateItemInDatabase(mLauncher, mInfo);
Adam Cohenac56cff2011-09-28 20:45:37 -0700261
Adam Cohen3371da02011-10-25 21:38:29 -0700262 if (commit) {
263 sendCustomAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED,
Michael Jurka8b805b12012-04-18 14:23:14 -0700264 String.format(getContext().getString(R.string.folder_renamed), newTitle));
Adam Cohen3371da02011-10-25 21:38:29 -0700265 }
Adam Cohenac56cff2011-09-28 20:45:37 -0700266 // In order to clear the focus from the text field, we set the focus on ourself. This
267 // ensures that every time the field is clicked, focus is gained, giving reliable behavior.
268 requestFocus();
269
Adam Cohene601a432011-07-26 21:51:30 -0700270 Selection.setSelection((Spannable) mFolderName.getText(), 0, 0);
Adam Cohen76fc0852011-06-17 13:26:23 -0700271 mIsEditingName = false;
272 }
273
274 public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
275 if (actionId == EditorInfo.IME_ACTION_DONE) {
276 dismissEditingName();
277 return true;
278 }
279 return false;
280 }
281
282 public View getEditTextRegion() {
283 return mFolderName;
284 }
285
Adam Cohenbadf71e2011-05-26 19:08:29 -0700286 public Drawable getDragDrawable() {
287 return mIconDrawable;
288 }
289
Adam Cohen0c872ba2011-05-05 10:34:16 -0700290 /**
291 * We need to handle touch events to prevent them from falling through to the workspace below.
292 */
293 @Override
294 public boolean onTouchEvent(MotionEvent ev) {
295 return true;
296 }
297
Joe Onorato00acb122009-08-04 16:04:30 -0400298 public void setDragController(DragController dragController) {
299 mDragController = dragController;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800300 }
301
Adam Cohen2801caf2011-05-13 20:57:39 -0700302 void setFolderIcon(FolderIcon icon) {
303 mFolderIcon = icon;
304 }
305
Adam Cohen3371da02011-10-25 21:38:29 -0700306 @Override
307 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
308 // When the folder gets focus, we don't want to announce the list of items.
309 return true;
310 }
311
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800312 /**
313 * @return the FolderInfo object associated with this folder
314 */
315 FolderInfo getInfo() {
316 return mInfo;
317 }
318
Adam Cohen3bf84d32012-05-07 20:17:14 -0700319 private class GridComparator implements Comparator<ShortcutInfo> {
320 int mNumCols;
321 public GridComparator(int numCols) {
322 mNumCols = numCols;
323 }
324
325 @Override
326 public int compare(ShortcutInfo lhs, ShortcutInfo rhs) {
327 int lhIndex = lhs.cellY * mNumCols + lhs.cellX;
328 int rhIndex = rhs.cellY * mNumCols + rhs.cellX;
329 return (lhIndex - rhIndex);
330 }
Adam Cohen3bf84d32012-05-07 20:17:14 -0700331 }
332
333 private void placeInReadingOrder(ArrayList<ShortcutInfo> items) {
334 int maxX = 0;
335 int count = items.size();
336 for (int i = 0; i < count; i++) {
337 ShortcutInfo item = items.get(i);
338 if (item.cellX > maxX) {
339 maxX = item.cellX;
340 }
341 }
Adam Cohen691a5792012-05-11 14:27:30 -0700342
343 GridComparator gridComparator = new GridComparator(maxX + 1);
Adam Cohen3bf84d32012-05-07 20:17:14 -0700344 Collections.sort(items, gridComparator);
345 final int countX = mContent.getCountX();
346 for (int i = 0; i < count; i++) {
347 int x = i % countX;
348 int y = i / countX;
349 ShortcutInfo item = items.get(i);
350 item.cellX = x;
351 item.cellY = y;
352 }
353 }
354
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800355 void bind(FolderInfo info) {
356 mInfo = info;
Adam Cohendf2cc412011-04-27 16:56:57 -0700357 ArrayList<ShortcutInfo> children = info.contents;
Adam Cohenc508b2d2011-06-28 14:41:44 -0700358 ArrayList<ShortcutInfo> overflow = new ArrayList<ShortcutInfo>();
Adam Cohen7c693212011-05-18 15:26:57 -0700359 setupContentForNumItems(children.size());
Adam Cohen3bf84d32012-05-07 20:17:14 -0700360 placeInReadingOrder(children);
Adam Cohen0057bbc2011-08-12 18:30:51 -0700361 int count = 0;
Adam Cohendf2cc412011-04-27 16:56:57 -0700362 for (int i = 0; i < children.size(); i++) {
363 ShortcutInfo child = (ShortcutInfo) children.get(i);
Adam Cohenc508b2d2011-06-28 14:41:44 -0700364 if (!createAndAddShortcut(child)) {
365 overflow.add(child);
Adam Cohen0057bbc2011-08-12 18:30:51 -0700366 } else {
367 count++;
Adam Cohenc508b2d2011-06-28 14:41:44 -0700368 }
Adam Cohendf2cc412011-04-27 16:56:57 -0700369 }
Adam Cohenc508b2d2011-06-28 14:41:44 -0700370
Adam Cohen0057bbc2011-08-12 18:30:51 -0700371 // We rearrange the items in case there are any empty gaps
372 setupContentForNumItems(count);
373
Adam Cohenc508b2d2011-06-28 14:41:44 -0700374 // If our folder has too many items we prune them from the list. This is an issue
375 // when upgrading from the old Folders implementation which could contain an unlimited
376 // number of items.
377 for (ShortcutInfo item: overflow) {
378 mInfo.remove(item);
379 LauncherModel.deleteItemFromDatabase(mLauncher, item);
380 }
381
Adam Cohen4dbe6d92011-05-18 17:14:15 -0700382 mItemsInvalidated = true;
Adam Cohenac56cff2011-09-28 20:45:37 -0700383 updateTextViewFocus();
Adam Cohena9cf38f2011-05-02 15:36:58 -0700384 mInfo.addListener(this);
Adam Cohen4ef610f2011-06-21 22:37:36 -0700385
Adam Cohenafb01ee2011-06-23 15:38:03 -0700386 if (!sDefaultFolderName.contentEquals(mInfo.title)) {
Adam Cohen4ef610f2011-06-21 22:37:36 -0700387 mFolderName.setText(mInfo.title);
388 } else {
389 mFolderName.setText("");
390 }
Adam Cohen691a5792012-05-11 14:27:30 -0700391 updateItemLocationsInDatabase();
Adam Cohendf2cc412011-04-27 16:56:57 -0700392 }
393
394 /**
395 * Creates a new UserFolder, inflated from R.layout.user_folder.
396 *
397 * @param context The application's context.
398 *
399 * @return A new UserFolder.
400 */
401 static Folder fromXml(Context context) {
402 return (Folder) LayoutInflater.from(context).inflate(R.layout.user_folder, null);
403 }
404
405 /**
406 * This method is intended to make the UserFolder to be visually identical in size and position
407 * to its associated FolderIcon. This allows for a seamless transition into the expanded state.
408 */
409 private void positionAndSizeAsIcon() {
Adam Cohen3e8f8112011-07-02 18:03:00 -0700410 if (!(getParent() instanceof DragLayer)) return;
Adam Cohen662b5982011-12-13 17:45:21 -0800411 setScaleX(0.8f);
412 setScaleY(0.8f);
413 setAlpha(0f);
Adam Cohendf2cc412011-04-27 16:56:57 -0700414 mState = STATE_SMALL;
415 }
416
417 public void animateOpen() {
Adam Cohen3e8f8112011-07-02 18:03:00 -0700418 positionAndSizeAsIcon();
419
Adam Cohen8e776a62011-06-28 18:10:06 -0700420 if (!(getParent() instanceof DragLayer)) return;
Adam Cohen2801caf2011-05-13 20:57:39 -0700421 centerAboutIcon();
Adam Cohen662b5982011-12-13 17:45:21 -0800422 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1);
423 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.0f);
424 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.0f);
Michael Jurka0121c3e2012-05-31 08:36:04 -0700425 final ObjectAnimator oa = mOpenCloseAnimator =
426 ObjectAnimator.ofPropertyValuesHolder(this, alpha, scaleX, scaleY);
Adam Cohen6441de02011-12-14 14:25:32 -0800427
Adam Cohen2801caf2011-05-13 20:57:39 -0700428 oa.addListener(new AnimatorListenerAdapter() {
Adam Cohendf2cc412011-04-27 16:56:57 -0700429 @Override
430 public void onAnimationStart(Animator animation) {
Adam Cohen3371da02011-10-25 21:38:29 -0700431 sendCustomAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED,
Michael Jurka8b805b12012-04-18 14:23:14 -0700432 String.format(getContext().getString(R.string.folder_opened),
Adam Cohen3371da02011-10-25 21:38:29 -0700433 mContent.getCountX(), mContent.getCountY()));
Adam Cohendf2cc412011-04-27 16:56:57 -0700434 mState = STATE_ANIMATING;
435 }
436 @Override
437 public void onAnimationEnd(Animator animation) {
Adam Cohen2801caf2011-05-13 20:57:39 -0700438 mState = STATE_OPEN;
Michael Jurka0121c3e2012-05-31 08:36:04 -0700439 setLayerType(LAYER_TYPE_NONE, null);
Winson Chung7a74ac92011-09-20 17:43:51 -0700440 Cling cling = mLauncher.showFirstRunFoldersCling();
441 if (cling != null) {
442 cling.bringToFront();
443 }
Adam Cohenac56cff2011-09-28 20:45:37 -0700444 setFocusOnFirstChild();
Adam Cohendf2cc412011-04-27 16:56:57 -0700445 }
446 });
Adam Cohen2801caf2011-05-13 20:57:39 -0700447 oa.setDuration(mExpandDuration);
Michael Jurka0121c3e2012-05-31 08:36:04 -0700448 setLayerType(LAYER_TYPE_HARDWARE, null);
449 buildLayer();
450 post(new Runnable() {
451 public void run() {
452 // Check if the animator changed in the meantime
453 if (oa != mOpenCloseAnimator)
454 return;
455 oa.start();
456 }
457 });
Adam Cohendf2cc412011-04-27 16:56:57 -0700458 }
459
Adam Cohen3371da02011-10-25 21:38:29 -0700460 private void sendCustomAccessibilityEvent(int type, String text) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700461 AccessibilityManager accessibilityManager = (AccessibilityManager)
462 getContext().getSystemService(Context.ACCESSIBILITY_SERVICE);
463 if (accessibilityManager.isEnabled()) {
Adam Cohen3371da02011-10-25 21:38:29 -0700464 AccessibilityEvent event = AccessibilityEvent.obtain(type);
465 onInitializeAccessibilityEvent(event);
466 event.getText().add(text);
Michael Jurka8b805b12012-04-18 14:23:14 -0700467 accessibilityManager.sendAccessibilityEvent(event);
Adam Cohen3371da02011-10-25 21:38:29 -0700468 }
469 }
470
Adam Cohenac56cff2011-09-28 20:45:37 -0700471 private void setFocusOnFirstChild() {
472 View firstChild = mContent.getChildAt(0, 0);
473 if (firstChild != null) {
474 firstChild.requestFocus();
475 }
476 }
477
Adam Cohendf2cc412011-04-27 16:56:57 -0700478 public void animateClosed() {
Adam Cohen8e776a62011-06-28 18:10:06 -0700479 if (!(getParent() instanceof DragLayer)) return;
Adam Cohen662b5982011-12-13 17:45:21 -0800480 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0);
481 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 0.9f);
482 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 0.9f);
Michael Jurka0121c3e2012-05-31 08:36:04 -0700483 final ObjectAnimator oa = mOpenCloseAnimator =
484 ObjectAnimator.ofPropertyValuesHolder(this, alpha, scaleX, scaleY);
Adam Cohendf2cc412011-04-27 16:56:57 -0700485
Adam Cohen2801caf2011-05-13 20:57:39 -0700486 oa.addListener(new AnimatorListenerAdapter() {
Adam Cohendf2cc412011-04-27 16:56:57 -0700487 @Override
488 public void onAnimationEnd(Animator animation) {
Adam Cohen2801caf2011-05-13 20:57:39 -0700489 onCloseComplete();
Michael Jurka0121c3e2012-05-31 08:36:04 -0700490 setLayerType(LAYER_TYPE_NONE, null);
Adam Cohen2801caf2011-05-13 20:57:39 -0700491 mState = STATE_SMALL;
Adam Cohendf2cc412011-04-27 16:56:57 -0700492 }
493 @Override
494 public void onAnimationStart(Animator animation) {
Adam Cohen3371da02011-10-25 21:38:29 -0700495 sendCustomAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED,
Michael Jurka8b805b12012-04-18 14:23:14 -0700496 getContext().getString(R.string.folder_closed));
Adam Cohendf2cc412011-04-27 16:56:57 -0700497 mState = STATE_ANIMATING;
498 }
499 });
Adam Cohen2801caf2011-05-13 20:57:39 -0700500 oa.setDuration(mExpandDuration);
Michael Jurka0121c3e2012-05-31 08:36:04 -0700501 setLayerType(LAYER_TYPE_HARDWARE, null);
502 buildLayer();
503 post(new Runnable() {
504 public void run() {
505 // Check if the animator changed in the meantime
506 if (oa != mOpenCloseAnimator)
507 return;
508 oa.start();
509 }
510 });
Adam Cohendf2cc412011-04-27 16:56:57 -0700511 }
512
513 void notifyDataSetChanged() {
514 // recreate all the children if the data set changes under us. We may want to do this more
515 // intelligently (ie just removing the views that should no longer exist)
516 mContent.removeAllViewsInLayout();
517 bind(mInfo);
518 }
519
Adam Cohencb3382b2011-05-24 14:07:08 -0700520 public boolean acceptDrop(DragObject d) {
521 final ItemInfo item = (ItemInfo) d.dragInfo;
Adam Cohendf2cc412011-04-27 16:56:57 -0700522 final int itemType = item.itemType;
Adam Cohen2801caf2011-05-13 20:57:39 -0700523 return ((itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION ||
524 itemType == LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT) &&
525 !isFull());
Adam Cohendf2cc412011-04-27 16:56:57 -0700526 }
527
Adam Cohendf2cc412011-04-27 16:56:57 -0700528 protected boolean findAndSetEmptyCells(ShortcutInfo item) {
529 int[] emptyCell = new int[2];
530 if (mContent.findCellForSpan(emptyCell, item.spanX, item.spanY)) {
531 item.cellX = emptyCell[0];
532 item.cellY = emptyCell[1];
Adam Cohendf2cc412011-04-27 16:56:57 -0700533 return true;
534 } else {
535 return false;
536 }
537 }
538
Adam Cohenc508b2d2011-06-28 14:41:44 -0700539 protected boolean createAndAddShortcut(ShortcutInfo item) {
Adam Cohendf2cc412011-04-27 16:56:57 -0700540 final TextView textView =
Adam Cohene87b9242011-06-29 14:01:26 -0700541 (TextView) mInflater.inflate(R.layout.application, this, false);
Adam Cohendf2cc412011-04-27 16:56:57 -0700542 textView.setCompoundDrawablesWithIntrinsicBounds(null,
543 new FastBitmapDrawable(item.getIcon(mIconCache)), null, null);
544 textView.setText(item.title);
545 textView.setTag(item);
546
547 textView.setOnClickListener(this);
548 textView.setOnLongClickListener(this);
549
Adam Cohenc508b2d2011-06-28 14:41:44 -0700550 // We need to check here to verify that the given item's location isn't already occupied
Adam Cohen3bf84d32012-05-07 20:17:14 -0700551 // by another item.
Adam Cohen0057bbc2011-08-12 18:30:51 -0700552 if (mContent.getChildAt(item.cellX, item.cellY) != null || item.cellX < 0 || item.cellY < 0
553 || item.cellX >= mContent.getCountX() || item.cellY >= mContent.getCountY()) {
Adam Cohen3bf84d32012-05-07 20:17:14 -0700554 // This shouldn't happen, log it.
555 Log.e(TAG, "Folder order not properly persisted during bind");
Adam Cohenc508b2d2011-06-28 14:41:44 -0700556 if (!findAndSetEmptyCells(item)) {
557 return false;
558 }
559 }
560
Adam Cohendf2cc412011-04-27 16:56:57 -0700561 CellLayout.LayoutParams lp =
562 new CellLayout.LayoutParams(item.cellX, item.cellY, item.spanX, item.spanY);
563 boolean insert = false;
Adam Cohenac56cff2011-09-28 20:45:37 -0700564 textView.setOnKeyListener(new FolderKeyEventListener());
Adam Cohendf2cc412011-04-27 16:56:57 -0700565 mContent.addViewToCellLayout(textView, insert ? 0 : -1, (int)item.id, lp, true);
Adam Cohenc508b2d2011-06-28 14:41:44 -0700566 return true;
Adam Cohendf2cc412011-04-27 16:56:57 -0700567 }
568
Adam Cohencb3382b2011-05-24 14:07:08 -0700569 public void onDragEnter(DragObject d) {
Adam Cohenbfbfd262011-06-13 16:55:12 -0700570 mPreviousTargetCell[0] = -1;
571 mPreviousTargetCell[1] = -1;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700572 mOnExitAlarm.cancelAlarm();
573 }
574
575 OnAlarmListener mReorderAlarmListener = new OnAlarmListener() {
576 public void onAlarm(Alarm alarm) {
577 realTimeReorder(mEmptyCell, mTargetCell);
578 }
579 };
580
581 boolean readingOrderGreaterThan(int[] v1, int[] v2) {
582 if (v1[1] > v2[1] || (v1[1] == v2[1] && v1[0] > v2[0])) {
583 return true;
584 } else {
585 return false;
586 }
587 }
588
589 private void realTimeReorder(int[] empty, int[] target) {
590 boolean wrap;
591 int startX;
592 int endX;
593 int startY;
Adam Cohen76fc0852011-06-17 13:26:23 -0700594 int delay = 0;
595 float delayAmount = 30;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700596 if (readingOrderGreaterThan(target, empty)) {
597 wrap = empty[0] >= mContent.getCountX() - 1;
598 startY = wrap ? empty[1] + 1 : empty[1];
599 for (int y = startY; y <= target[1]; y++) {
600 startX = y == empty[1] ? empty[0] + 1 : 0;
601 endX = y < target[1] ? mContent.getCountX() - 1 : target[0];
602 for (int x = startX; x <= endX; x++) {
603 View v = mContent.getChildAt(x,y);
604 if (mContent.animateChildToPosition(v, empty[0], empty[1],
Adam Cohen482ed822012-03-02 14:15:13 -0800605 REORDER_ANIMATION_DURATION, delay, true, true)) {
Adam Cohenbfbfd262011-06-13 16:55:12 -0700606 empty[0] = x;
607 empty[1] = y;
Adam Cohen76fc0852011-06-17 13:26:23 -0700608 delay += delayAmount;
609 delayAmount *= 0.9;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700610 }
611 }
612 }
613 } else {
614 wrap = empty[0] == 0;
615 startY = wrap ? empty[1] - 1 : empty[1];
616 for (int y = startY; y >= target[1]; y--) {
617 startX = y == empty[1] ? empty[0] - 1 : mContent.getCountX() - 1;
618 endX = y > target[1] ? 0 : target[0];
619 for (int x = startX; x >= endX; x--) {
620 View v = mContent.getChildAt(x,y);
621 if (mContent.animateChildToPosition(v, empty[0], empty[1],
Adam Cohen482ed822012-03-02 14:15:13 -0800622 REORDER_ANIMATION_DURATION, delay, true, true)) {
Adam Cohenbfbfd262011-06-13 16:55:12 -0700623 empty[0] = x;
624 empty[1] = y;
Adam Cohen76fc0852011-06-17 13:26:23 -0700625 delay += delayAmount;
626 delayAmount *= 0.9;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700627 }
628 }
629 }
630 }
Adam Cohendf2cc412011-04-27 16:56:57 -0700631 }
632
Adam Cohencb3382b2011-05-24 14:07:08 -0700633 public void onDragOver(DragObject d) {
Adam Cohenbfbfd262011-06-13 16:55:12 -0700634 float[] r = getDragViewVisualCenter(d.x, d.y, d.xOffset, d.yOffset, d.dragView, null);
635 mTargetCell = mContent.findNearestArea((int) r[0], (int) r[1], 1, 1, mTargetCell);
636
637 if (mTargetCell[0] != mPreviousTargetCell[0] || mTargetCell[1] != mPreviousTargetCell[1]) {
638 mReorderAlarm.cancelAlarm();
639 mReorderAlarm.setOnAlarmListener(mReorderAlarmListener);
640 mReorderAlarm.setAlarm(150);
641 mPreviousTargetCell[0] = mTargetCell[0];
642 mPreviousTargetCell[1] = mTargetCell[1];
643 }
Adam Cohendf2cc412011-04-27 16:56:57 -0700644 }
645
Adam Cohenbfbfd262011-06-13 16:55:12 -0700646 // This is used to compute the visual center of the dragView. The idea is that
647 // the visual center represents the user's interpretation of where the item is, and hence
648 // is the appropriate point to use when determining drop location.
649 private float[] getDragViewVisualCenter(int x, int y, int xOffset, int yOffset,
650 DragView dragView, float[] recycle) {
651 float res[];
652 if (recycle == null) {
653 res = new float[2];
654 } else {
655 res = recycle;
656 }
657
658 // These represent the visual top and left of drag view if a dragRect was provided.
659 // If a dragRect was not provided, then they correspond to the actual view left and
660 // top, as the dragRect is in that case taken to be the entire dragView.
661 // R.dimen.dragViewOffsetY.
662 int left = x - xOffset;
663 int top = y - yOffset;
664
665 // In order to find the visual center, we shift by half the dragRect
666 res[0] = left + dragView.getDragRegion().width() / 2;
667 res[1] = top + dragView.getDragRegion().height() / 2;
668
669 return res;
670 }
671
672 OnAlarmListener mOnExitAlarmListener = new OnAlarmListener() {
673 public void onAlarm(Alarm alarm) {
Adam Cohen3e8f8112011-07-02 18:03:00 -0700674 completeDragExit();
Adam Cohenbfbfd262011-06-13 16:55:12 -0700675 }
676 };
677
Adam Cohen95bb8002011-07-03 23:40:28 -0700678 public void completeDragExit() {
Adam Cohen3e8f8112011-07-02 18:03:00 -0700679 mLauncher.closeFolder();
680 mCurrentDragInfo = null;
681 mCurrentDragView = null;
682 mSuppressOnAdd = false;
683 mRearrangeOnClose = true;
684 }
685
Adam Cohencb3382b2011-05-24 14:07:08 -0700686 public void onDragExit(DragObject d) {
Adam Cohenbfbfd262011-06-13 16:55:12 -0700687 // We only close the folder if this is a true drag exit, ie. not because a drop
688 // has occurred above the folder.
689 if (!d.dragComplete) {
690 mOnExitAlarm.setOnAlarmListener(mOnExitAlarmListener);
691 mOnExitAlarm.setAlarm(ON_EXIT_CLOSE_DELAY);
692 }
693 mReorderAlarm.cancelAlarm();
Adam Cohen2801caf2011-05-13 20:57:39 -0700694 }
695
Winson Chunga48487a2012-03-20 16:19:37 -0700696 public void onDropCompleted(View target, DragObject d, boolean isFlingToDelete,
697 boolean success) {
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700698 if (success) {
Adam Cohen05e0f402011-08-01 12:12:49 -0700699 if (mDeleteFolderOnDropCompleted && !mItemAddedBackToSelfViaIcon) {
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700700 replaceFolderWithFinalItem();
701 }
702 } else {
703 // The drag failed, we need to return the item to the folder
704 mFolderIcon.onDrop(d);
705
706 // We're going to trigger a "closeFolder" which may occur before this item has
707 // been added back to the folder -- this could cause the folder to be deleted
708 if (mOnExitAlarm.alarmPending()) {
709 mSuppressFolderDeletion = true;
710 }
711 }
712
713 if (target != this) {
714 if (mOnExitAlarm.alarmPending()) {
715 mOnExitAlarm.cancelAlarm();
716 completeDragExit();
717 }
718 }
719 mDeleteFolderOnDropCompleted = false;
720 mDragInProgress = false;
Adam Cohen05e0f402011-08-01 12:12:49 -0700721 mItemAddedBackToSelfViaIcon = false;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700722 mCurrentDragInfo = null;
723 mCurrentDragView = null;
724 mSuppressOnAdd = false;
Adam Cohen4045eb72011-10-06 11:44:26 -0700725
726 // Reordering may have occured, and we need to save the new item locations. We do this once
727 // at the end to prevent unnecessary database operations.
728 updateItemLocationsInDatabase();
729 }
730
Winson Chunga48487a2012-03-20 16:19:37 -0700731 @Override
Winson Chung043f2af2012-03-01 16:09:54 -0800732 public boolean supportsFlingToDelete() {
733 return true;
734 }
735
Winson Chunga48487a2012-03-20 16:19:37 -0700736 public void onFlingToDelete(DragObject d, int x, int y, PointF vec) {
737 // Do nothing
738 }
739
740 @Override
741 public void onFlingToDeleteCompleted() {
742 // Do nothing
743 }
744
Adam Cohen4045eb72011-10-06 11:44:26 -0700745 private void updateItemLocationsInDatabase() {
746 ArrayList<View> list = getItemsInReadingOrder();
747 for (int i = 0; i < list.size(); i++) {
748 View v = list.get(i);
749 ItemInfo info = (ItemInfo) v.getTag();
750 LauncherModel.moveItemInDatabase(mLauncher, info, mInfo.id, 0,
751 info.cellX, info.cellY);
752 }
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700753 }
Adam Cohen228da5a2011-07-27 22:23:47 -0700754
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700755 public void notifyDrop() {
756 if (mDragInProgress) {
Adam Cohen05e0f402011-08-01 12:12:49 -0700757 mItemAddedBackToSelfViaIcon = true;
Adam Cohen76078c42011-06-09 15:06:52 -0700758 }
Adam Cohendf2cc412011-04-27 16:56:57 -0700759 }
760
761 public boolean isDropEnabled() {
762 return true;
763 }
764
Adam Cohencb3382b2011-05-24 14:07:08 -0700765 public DropTarget getDropTargetDelegate(DragObject d) {
Adam Cohendf2cc412011-04-27 16:56:57 -0700766 return null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800767 }
Adam Cohena9cf38f2011-05-02 15:36:58 -0700768
Adam Cohen4045eb72011-10-06 11:44:26 -0700769 private void setupContentDimensions(int count) {
Adam Cohen2801caf2011-05-13 20:57:39 -0700770 ArrayList<View> list = getItemsInReadingOrder();
771
772 int countX = mContent.getCountX();
773 int countY = mContent.getCountY();
Adam Cohen7c693212011-05-18 15:26:57 -0700774 boolean done = false;
Adam Cohen2801caf2011-05-13 20:57:39 -0700775
Adam Cohen7c693212011-05-18 15:26:57 -0700776 while (!done) {
777 int oldCountX = countX;
778 int oldCountY = countY;
779 if (countX * countY < count) {
780 // Current grid is too small, expand it
Adam Cohen78dc83e2011-11-15 17:10:00 -0800781 if ((countX <= countY || countY == mMaxCountY) && countX < mMaxCountX) {
Adam Cohen7c693212011-05-18 15:26:57 -0700782 countX++;
783 } else if (countY < mMaxCountY) {
784 countY++;
785 }
786 if (countY == 0) countY++;
787 } else if ((countY - 1) * countX >= count && countY >= countX) {
788 countY = Math.max(0, countY - 1);
789 } else if ((countX - 1) * countY >= count) {
790 countX = Math.max(0, countX - 1);
Adam Cohen2801caf2011-05-13 20:57:39 -0700791 }
Adam Cohen7c693212011-05-18 15:26:57 -0700792 done = countX == oldCountX && countY == oldCountY;
Adam Cohen2801caf2011-05-13 20:57:39 -0700793 }
Adam Cohen7c693212011-05-18 15:26:57 -0700794 mContent.setGridSize(countX, countY);
Adam Cohen2801caf2011-05-13 20:57:39 -0700795 arrangeChildren(list);
796 }
797
798 public boolean isFull() {
Adam Cohen78dc83e2011-11-15 17:10:00 -0800799 return getItemCount() >= mMaxNumItems;
Adam Cohen2801caf2011-05-13 20:57:39 -0700800 }
801
802 private void centerAboutIcon() {
Adam Cohen8e776a62011-06-28 18:10:06 -0700803 DragLayer.LayoutParams lp = (DragLayer.LayoutParams) getLayoutParams();
Adam Cohen2801caf2011-05-13 20:57:39 -0700804
805 int width = getPaddingLeft() + getPaddingRight() + mContent.getDesiredWidth();
Adam Cohenf4bb1cd2011-07-22 14:36:03 -0700806 int height = getPaddingTop() + getPaddingBottom() + mContent.getDesiredHeight()
807 + mFolderNameHeight;
Adam Cohen8e776a62011-06-28 18:10:06 -0700808 DragLayer parent = (DragLayer) mLauncher.findViewById(R.id.drag_layer);
Adam Cohen2801caf2011-05-13 20:57:39 -0700809
Adam Cohen8e776a62011-06-28 18:10:06 -0700810 parent.getDescendantRectRelativeToSelf(mFolderIcon, mTempRect);
811
812 int centerX = mTempRect.centerX();
813 int centerY = mTempRect.centerY();
Adam Cohen2801caf2011-05-13 20:57:39 -0700814 int centeredLeft = centerX - width / 2;
815 int centeredTop = centerY - height / 2;
816
Adam Cohen7cc1bc42011-12-13 21:28:43 -0800817 int currentPage = mLauncher.getWorkspace().getCurrentPage();
818 // In case the workspace is scrolling, we need to use the final scroll to compute
819 // the folders bounds.
820 mLauncher.getWorkspace().setFinalScrollForPageChange(currentPage);
Adam Cohen35e7e642011-07-17 14:47:18 -0700821 // We first fetch the currently visible CellLayoutChildren
Adam Cohen7cc1bc42011-12-13 21:28:43 -0800822 CellLayout currentLayout = (CellLayout) mLauncher.getWorkspace().getChildAt(currentPage);
Michael Jurkaa52570f2012-03-20 03:18:20 -0700823 ShortcutAndWidgetContainer boundingLayout = currentLayout.getShortcutsAndWidgets();
Adam Cohen35e7e642011-07-17 14:47:18 -0700824 Rect bounds = new Rect();
825 parent.getDescendantRectRelativeToSelf(boundingLayout, bounds);
Adam Cohen7cc1bc42011-12-13 21:28:43 -0800826 // We reset the workspaces scroll
827 mLauncher.getWorkspace().resetFinalScrollForPageChange(currentPage);
Adam Cohen2801caf2011-05-13 20:57:39 -0700828
Adam Cohen35e7e642011-07-17 14:47:18 -0700829 // We need to bound the folder to the currently visible CellLayoutChildren
830 int left = Math.min(Math.max(bounds.left, centeredLeft),
831 bounds.left + bounds.width() - width);
832 int top = Math.min(Math.max(bounds.top, centeredTop),
833 bounds.top + bounds.height() - height);
834 // If the folder doesn't fit within the bounds, center it about the desired bounds
835 if (width >= bounds.width()) {
836 left = bounds.left + (bounds.width() - width) / 2;
Adam Cohen0e4857c2011-06-30 17:05:14 -0700837 }
Adam Cohen35e7e642011-07-17 14:47:18 -0700838 if (height >= bounds.height()) {
839 top = bounds.top + (bounds.height() - height) / 2;
Adam Cohen0e4857c2011-06-30 17:05:14 -0700840 }
Adam Cohen2801caf2011-05-13 20:57:39 -0700841
842 int folderPivotX = width / 2 + (centeredLeft - left);
843 int folderPivotY = height / 2 + (centeredTop - top);
844 setPivotX(folderPivotX);
845 setPivotY(folderPivotY);
Adam Cohen268c4752012-06-06 17:47:33 -0700846 mFolderIconPivotX = (int) (mFolderIcon.getMeasuredWidth() *
Adam Cohen2801caf2011-05-13 20:57:39 -0700847 (1.0f * folderPivotX / width));
Adam Cohen268c4752012-06-06 17:47:33 -0700848 mFolderIconPivotY = (int) (mFolderIcon.getMeasuredHeight() *
Adam Cohen2801caf2011-05-13 20:57:39 -0700849 (1.0f * folderPivotY / height));
Adam Cohen3bf84d32012-05-07 20:17:14 -0700850
Adam Cohen662b5982011-12-13 17:45:21 -0800851 lp.width = width;
852 lp.height = height;
853 lp.x = left;
854 lp.y = top;
Adam Cohen2801caf2011-05-13 20:57:39 -0700855 }
856
Adam Cohen268c4752012-06-06 17:47:33 -0700857 float getPivotXForIconAnimation() {
858 return mFolderIconPivotX;
859 }
860 float getPivotYForIconAnimation() {
861 return mFolderIconPivotY;
862 }
863
Adam Cohen2801caf2011-05-13 20:57:39 -0700864 private void setupContentForNumItems(int count) {
Adam Cohen4045eb72011-10-06 11:44:26 -0700865 setupContentDimensions(count);
Adam Cohen2801caf2011-05-13 20:57:39 -0700866
Adam Cohen8e776a62011-06-28 18:10:06 -0700867 DragLayer.LayoutParams lp = (DragLayer.LayoutParams) getLayoutParams();
Adam Cohen2801caf2011-05-13 20:57:39 -0700868 if (lp == null) {
Adam Cohen8e776a62011-06-28 18:10:06 -0700869 lp = new DragLayer.LayoutParams(0, 0);
870 lp.customPosition = true;
Adam Cohen2801caf2011-05-13 20:57:39 -0700871 setLayoutParams(lp);
872 }
873 centerAboutIcon();
874 }
875
Adam Cohen234c4cd2011-07-17 21:03:04 -0700876 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
877 int width = getPaddingLeft() + getPaddingRight() + mContent.getDesiredWidth();
Adam Cohenf4bb1cd2011-07-22 14:36:03 -0700878 int height = getPaddingTop() + getPaddingBottom() + mContent.getDesiredHeight()
879 + mFolderNameHeight;
Adam Cohen234c4cd2011-07-17 21:03:04 -0700880
881 int contentWidthSpec = MeasureSpec.makeMeasureSpec(mContent.getDesiredWidth(),
882 MeasureSpec.EXACTLY);
883 int contentHeightSpec = MeasureSpec.makeMeasureSpec(mContent.getDesiredHeight(),
884 MeasureSpec.EXACTLY);
885 mContent.measure(contentWidthSpec, contentHeightSpec);
886
887 mFolderName.measure(contentWidthSpec,
888 MeasureSpec.makeMeasureSpec(mFolderNameHeight, MeasureSpec.EXACTLY));
889 setMeasuredDimension(width, height);
890 }
891
Adam Cohen2801caf2011-05-13 20:57:39 -0700892 private void arrangeChildren(ArrayList<View> list) {
893 int[] vacant = new int[2];
894 if (list == null) {
895 list = getItemsInReadingOrder();
896 }
897 mContent.removeAllViews();
898
899 for (int i = 0; i < list.size(); i++) {
900 View v = list.get(i);
901 mContent.getVacantCell(vacant, 1, 1);
902 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) v.getLayoutParams();
903 lp.cellX = vacant[0];
904 lp.cellY = vacant[1];
905 ItemInfo info = (ItemInfo) v.getTag();
Adam Cohen2792a332011-09-26 21:09:47 -0700906 if (info.cellX != vacant[0] || info.cellY != vacant[1]) {
907 info.cellX = vacant[0];
908 info.cellY = vacant[1];
909 LauncherModel.addOrMoveItemInDatabase(mLauncher, info, mInfo.id, 0,
910 info.cellX, info.cellY);
911 }
Adam Cohen2801caf2011-05-13 20:57:39 -0700912 boolean insert = false;
913 mContent.addViewToCellLayout(v, insert ? 0 : -1, (int)info.id, lp, true);
914 }
Adam Cohen7c693212011-05-18 15:26:57 -0700915 mItemsInvalidated = true;
Adam Cohen2801caf2011-05-13 20:57:39 -0700916 }
917
Adam Cohena9cf38f2011-05-02 15:36:58 -0700918 public int getItemCount() {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700919 return mContent.getShortcutsAndWidgets().getChildCount();
Adam Cohena9cf38f2011-05-02 15:36:58 -0700920 }
921
922 public View getItemAt(int index) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700923 return mContent.getShortcutsAndWidgets().getChildAt(index);
Adam Cohena9cf38f2011-05-02 15:36:58 -0700924 }
925
Adam Cohen2801caf2011-05-13 20:57:39 -0700926 private void onCloseComplete() {
Adam Cohen05e0f402011-08-01 12:12:49 -0700927 DragLayer parent = (DragLayer) getParent();
Adam Cohen4554ee12011-08-03 16:13:21 -0700928 parent.removeView(this);
929 mDragController.removeDropTarget((DropTarget) this);
Adam Cohen05e0f402011-08-01 12:12:49 -0700930 clearFocus();
Adam Cohenac56cff2011-09-28 20:45:37 -0700931 mFolderIcon.requestFocus();
Adam Cohen05e0f402011-08-01 12:12:49 -0700932
Adam Cohen2801caf2011-05-13 20:57:39 -0700933 if (mRearrangeOnClose) {
934 setupContentForNumItems(getItemCount());
935 mRearrangeOnClose = false;
936 }
Adam Cohenafb01ee2011-06-23 15:38:03 -0700937 if (getItemCount() <= 1) {
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700938 if (!mDragInProgress && !mSuppressFolderDeletion) {
939 replaceFolderWithFinalItem();
940 } else if (mDragInProgress) {
941 mDeleteFolderOnDropCompleted = true;
942 }
Adam Cohenafb01ee2011-06-23 15:38:03 -0700943 }
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700944 mSuppressFolderDeletion = false;
Adam Cohenafb01ee2011-06-23 15:38:03 -0700945 }
946
947 private void replaceFolderWithFinalItem() {
Adam Cohenafb01ee2011-06-23 15:38:03 -0700948 // Add the last remaining child to the workspace in place of the folder
Adam Cohenfb91f302012-06-11 15:45:18 -0700949 Runnable onCompleteRunnable = new Runnable() {
950 @Override
951 public void run() {
952 CellLayout cellLayout = mLauncher.getCellLayout(mInfo.container, mInfo.screen);
Adam Cohenafb01ee2011-06-23 15:38:03 -0700953
Adam Cohenfb91f302012-06-11 15:45:18 -0700954 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 }
963
964 // Move the item from the folder to the workspace, in the position of the folder
965 if (getItemCount() == 1) {
966 ShortcutInfo finalItem = mInfo.contents.get(0);
967
968 final View child = mLauncher.createShortcut(R.layout.application, cellLayout,
969 finalItem);
970 LauncherModel.addOrMoveItemInDatabase(mLauncher, finalItem, mInfo.container,
971 mInfo.screen, mInfo.cellX, mInfo.cellY);
972 mLauncher.getWorkspace().addInScreen(child, mInfo.container, mInfo.screen,
973 mInfo.cellX, mInfo.cellY, mInfo.spanX, mInfo.spanY);
974 }
975
976 }
977 };
978 View finalChild = getItemAt(0);
979 if (finalChild != null) {
980 mFolderIcon.performDestroyAnimation(finalChild, onCompleteRunnable);
Adam Cohenafb01ee2011-06-23 15:38:03 -0700981 }
Adam Cohenfb91f302012-06-11 15:45:18 -0700982 mDestroyed = true;
983 }
984
985 boolean isDestroyed() {
986 return mDestroyed;
Adam Cohen2801caf2011-05-13 20:57:39 -0700987 }
988
Adam Cohenac56cff2011-09-28 20:45:37 -0700989 // This method keeps track of the last item in the folder for the purposes
990 // of keyboard focus
991 private void updateTextViewFocus() {
992 View lastChild = getItemAt(getItemCount() - 1);
993 getItemAt(getItemCount() - 1);
994 if (lastChild != null) {
995 mFolderName.setNextFocusDownId(lastChild.getId());
996 mFolderName.setNextFocusRightId(lastChild.getId());
997 mFolderName.setNextFocusLeftId(lastChild.getId());
998 mFolderName.setNextFocusUpId(lastChild.getId());
999 }
1000 }
1001
Adam Cohenbfbfd262011-06-13 16:55:12 -07001002 public void onDrop(DragObject d) {
1003 ShortcutInfo item;
1004 if (d.dragInfo instanceof ApplicationInfo) {
1005 // Came from all apps -- make a copy
1006 item = ((ApplicationInfo) d.dragInfo).makeShortcut();
1007 item.spanX = 1;
1008 item.spanY = 1;
1009 } else {
1010 item = (ShortcutInfo) d.dragInfo;
1011 }
Adam Cohen05e0f402011-08-01 12:12:49 -07001012 // Dragged from self onto self, currently this is the only path possible, however
1013 // we keep this as a distinct code path.
Adam Cohenbfbfd262011-06-13 16:55:12 -07001014 if (item == mCurrentDragInfo) {
1015 ShortcutInfo si = (ShortcutInfo) mCurrentDragView.getTag();
1016 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) mCurrentDragView.getLayoutParams();
1017 si.cellX = lp.cellX = mEmptyCell[0];
1018 si.cellX = lp.cellY = mEmptyCell[1];
1019 mContent.addViewToCellLayout(mCurrentDragView, -1, (int)item.id, lp, true);
Adam Cohenfc53cd22011-07-20 15:45:11 -07001020 if (d.dragView.hasDrawn()) {
1021 mLauncher.getDragLayer().animateViewIntoPosition(d.dragView, mCurrentDragView);
1022 } else {
Winson Chung2c4cf412012-05-08 14:03:21 -07001023 d.deferDragViewCleanupPostAnimation = false;
Adam Cohenfc53cd22011-07-20 15:45:11 -07001024 mCurrentDragView.setVisibility(VISIBLE);
1025 }
Adam Cohene9166b22011-07-08 17:11:11 -07001026 mItemsInvalidated = true;
Adam Cohen4045eb72011-10-06 11:44:26 -07001027 setupContentDimensions(getItemCount());
Adam Cohen716b51e2011-06-30 12:09:54 -07001028 mSuppressOnAdd = true;
Adam Cohenbfbfd262011-06-13 16:55:12 -07001029 }
1030 mInfo.add(item);
1031 }
1032
1033 public void onAdd(ShortcutInfo item) {
1034 mItemsInvalidated = true;
Adam Cohen05e0f402011-08-01 12:12:49 -07001035 // If the item was dropped onto this open folder, we have done the work associated
1036 // with adding the item to the folder, as indicated by mSuppressOnAdd being set
Adam Cohenbfbfd262011-06-13 16:55:12 -07001037 if (mSuppressOnAdd) return;
1038 if (!findAndSetEmptyCells(item)) {
1039 // The current layout is full, can we expand it?
1040 setupContentForNumItems(getItemCount() + 1);
1041 findAndSetEmptyCells(item);
1042 }
1043 createAndAddShortcut(item);
1044 LauncherModel.addOrMoveItemInDatabase(
1045 mLauncher, item, mInfo.id, 0, item.cellX, item.cellY);
1046 }
1047
Adam Cohena9cf38f2011-05-02 15:36:58 -07001048 public void onRemove(ShortcutInfo item) {
Adam Cohen7c693212011-05-18 15:26:57 -07001049 mItemsInvalidated = true;
Adam Cohen05e0f402011-08-01 12:12:49 -07001050 // If this item is being dragged from this open folder, we have already handled
1051 // the work associated with removing the item, so we don't have to do anything here.
Adam Cohenbfbfd262011-06-13 16:55:12 -07001052 if (item == mCurrentDragInfo) return;
Adam Cohendf1e4e82011-06-24 15:57:39 -07001053 View v = getViewForInfo(item);
Adam Cohena9cf38f2011-05-02 15:36:58 -07001054 mContent.removeView(v);
Adam Cohen2801caf2011-05-13 20:57:39 -07001055 if (mState == STATE_ANIMATING) {
1056 mRearrangeOnClose = true;
1057 } else {
1058 setupContentForNumItems(getItemCount());
1059 }
Adam Cohenafb01ee2011-06-23 15:38:03 -07001060 if (getItemCount() <= 1) {
1061 replaceFolderWithFinalItem();
1062 }
Adam Cohena9cf38f2011-05-02 15:36:58 -07001063 }
Adam Cohen7c693212011-05-18 15:26:57 -07001064
Adam Cohendf1e4e82011-06-24 15:57:39 -07001065 private View getViewForInfo(ShortcutInfo item) {
1066 for (int j = 0; j < mContent.getCountY(); j++) {
1067 for (int i = 0; i < mContent.getCountX(); i++) {
1068 View v = mContent.getChildAt(i, j);
1069 if (v.getTag() == item) {
1070 return v;
1071 }
1072 }
1073 }
1074 return null;
1075 }
1076
Adam Cohen76078c42011-06-09 15:06:52 -07001077 public void onItemsChanged() {
Adam Cohenac56cff2011-09-28 20:45:37 -07001078 updateTextViewFocus();
Adam Cohen76078c42011-06-09 15:06:52 -07001079 }
Adam Cohenac56cff2011-09-28 20:45:37 -07001080
Adam Cohen76fc0852011-06-17 13:26:23 -07001081 public void onTitleChanged(CharSequence title) {
1082 }
Adam Cohen76078c42011-06-09 15:06:52 -07001083
Adam Cohen7c693212011-05-18 15:26:57 -07001084 public ArrayList<View> getItemsInReadingOrder() {
Adam Cohen76078c42011-06-09 15:06:52 -07001085 return getItemsInReadingOrder(true);
1086 }
1087
1088 public ArrayList<View> getItemsInReadingOrder(boolean includeCurrentDragItem) {
Adam Cohen7c693212011-05-18 15:26:57 -07001089 if (mItemsInvalidated) {
1090 mItemsInReadingOrder.clear();
1091 for (int j = 0; j < mContent.getCountY(); j++) {
1092 for (int i = 0; i < mContent.getCountX(); i++) {
1093 View v = mContent.getChildAt(i, j);
1094 if (v != null) {
Adam Cohen76078c42011-06-09 15:06:52 -07001095 ShortcutInfo info = (ShortcutInfo) v.getTag();
1096 if (info != mCurrentDragInfo || includeCurrentDragItem) {
1097 mItemsInReadingOrder.add(v);
1098 }
Adam Cohen7c693212011-05-18 15:26:57 -07001099 }
1100 }
1101 }
1102 mItemsInvalidated = false;
1103 }
1104 return mItemsInReadingOrder;
1105 }
Adam Cohen8dfcba42011-07-07 16:38:18 -07001106
1107 public void getLocationInDragLayer(int[] loc) {
1108 mLauncher.getDragLayer().getLocationInDragLayer(this, loc);
1109 }
Adam Cohenea0818d2011-09-30 20:06:58 -07001110
1111 public void onFocusChange(View v, boolean hasFocus) {
1112 if (v == mFolderName && hasFocus) {
1113 startEditingFolderName();
1114 }
1115 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001116}