blob: 5520e23f3b71ca22459bf8070f2824b3e9915fb8 [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 {
Adam Cohendf2cc412011-04-27 16:56:57 -070060 private static final String TAG = "Launcher.Folder";
61
Adam Cohen4eac29a2011-07-11 17:53:37 -070062 protected DragController mDragController;
63 protected Launcher mLauncher;
64 protected FolderInfo mInfo;
65
Adam Cohendf2cc412011-04-27 16:56:57 -070066 static final int STATE_NONE = -1;
67 static final int STATE_SMALL = 0;
68 static final int STATE_ANIMATING = 1;
69 static final int STATE_OPEN = 2;
70
71 private int mExpandDuration;
72 protected CellLayout mContent;
73 private final LayoutInflater mInflater;
74 private final IconCache mIconCache;
75 private int mState = STATE_NONE;
Adam Cohenbfbfd262011-06-13 16:55:12 -070076 private static final int REORDER_ANIMATION_DURATION = 230;
77 private static final int ON_EXIT_CLOSE_DELAY = 800;
Adam Cohen2801caf2011-05-13 20:57:39 -070078 private boolean mRearrangeOnClose = false;
79 private FolderIcon mFolderIcon;
80 private int mMaxCountX;
81 private int mMaxCountY;
Adam Cohen78dc83e2011-11-15 17:10:00 -080082 private int mMaxNumItems;
Adam Cohen7c693212011-05-18 15:26:57 -070083 private ArrayList<View> mItemsInReadingOrder = new ArrayList<View>();
Adam Cohenbadf71e2011-05-26 19:08:29 -070084 private Drawable mIconDrawable;
Adam Cohen7c693212011-05-18 15:26:57 -070085 boolean mItemsInvalidated = false;
Adam Cohenbfbfd262011-06-13 16:55:12 -070086 private ShortcutInfo mCurrentDragInfo;
87 private View mCurrentDragView;
88 boolean mSuppressOnAdd = false;
89 private int[] mTargetCell = new int[2];
90 private int[] mPreviousTargetCell = new int[2];
91 private int[] mEmptyCell = new int[2];
92 private Alarm mReorderAlarm = new Alarm();
93 private Alarm mOnExitAlarm = new Alarm();
Adam Cohen76fc0852011-06-17 13:26:23 -070094 private int mFolderNameHeight;
Adam Cohen8e776a62011-06-28 18:10:06 -070095 private Rect mTempRect = new Rect();
Adam Cohen67bd9cc2011-07-29 14:07:04 -070096 private boolean mDragInProgress = false;
97 private boolean mDeleteFolderOnDropCompleted = false;
98 private boolean mSuppressFolderDeletion = false;
Adam Cohen05e0f402011-08-01 12:12:49 -070099 private boolean mItemAddedBackToSelfViaIcon = false;
Adam Cohenac56cff2011-09-28 20:45:37 -0700100 FolderEditText mFolderName;
Adam Cohen268c4752012-06-06 17:47:33 -0700101 private float mFolderIconPivotX;
102 private float mFolderIconPivotY;
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
Adam Cohenfb91f302012-06-11 15:45:18 -0700111 private boolean mDestroyed;
112
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800113 /**
114 * Used to inflate the Workspace from XML.
115 *
116 * @param context The application's context.
117 * @param attrs The attribtues set containing the Workspace's customization values.
118 */
119 public Folder(Context context, AttributeSet attrs) {
120 super(context, attrs);
121 setAlwaysDrawnWithCacheEnabled(false);
Adam Cohendf2cc412011-04-27 16:56:57 -0700122 mInflater = LayoutInflater.from(context);
123 mIconCache = ((LauncherApplication)context.getApplicationContext()).getIconCache();
Adam Cohen78dc83e2011-11-15 17:10:00 -0800124
125 Resources res = getResources();
126 mMaxCountX = res.getInteger(R.integer.folder_max_count_x);
127 mMaxCountY = res.getInteger(R.integer.folder_max_count_y);
128 mMaxNumItems = res.getInteger(R.integer.folder_max_num_items);
129 if (mMaxCountX < 0 || mMaxCountY < 0 || mMaxNumItems < 0) {
130 mMaxCountX = LauncherModel.getCellCountX();
131 mMaxCountY = LauncherModel.getCellCountY();
132 mMaxNumItems = mMaxCountX * mMaxCountY;
133 }
Adam Cohen76fc0852011-06-17 13:26:23 -0700134
135 mInputMethodManager = (InputMethodManager)
Michael Jurka8b805b12012-04-18 14:23:14 -0700136 getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
Adam Cohen76fc0852011-06-17 13:26:23 -0700137
Adam Cohen76fc0852011-06-17 13:26:23 -0700138 mExpandDuration = res.getInteger(R.integer.config_folderAnimDuration);
Adam Cohen4ef610f2011-06-21 22:37:36 -0700139
140 if (sDefaultFolderName == null) {
141 sDefaultFolderName = res.getString(R.string.folder_name);
142 }
Adam Cohena65beee2011-06-27 21:32:23 -0700143 if (sHintText == null) {
144 sHintText = res.getString(R.string.folder_hint_text);
145 }
Adam Cohen4eac29a2011-07-11 17:53:37 -0700146 mLauncher = (Launcher) context;
Adam Cohenac56cff2011-09-28 20:45:37 -0700147 // We need this view to be focusable in touch mode so that when text editing of the folder
148 // name is complete, we have something to focus on, thus hiding the cursor and giving
149 // reliable behvior when clicking the text field (since it will always gain focus on click).
150 setFocusableInTouchMode(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800151 }
152
153 @Override
154 protected void onFinishInflate() {
155 super.onFinishInflate();
Adam Cohendf2cc412011-04-27 16:56:57 -0700156 mContent = (CellLayout) findViewById(R.id.folder_content);
Adam Cohen2801caf2011-05-13 20:57:39 -0700157 mContent.setGridSize(0, 0);
Michael Jurkaa52570f2012-03-20 03:18:20 -0700158 mContent.getShortcutsAndWidgets().setMotionEventSplittingEnabled(false);
Adam Cohenac56cff2011-09-28 20:45:37 -0700159 mFolderName = (FolderEditText) findViewById(R.id.folder_name);
160 mFolderName.setFolder(this);
Adam Cohenea0818d2011-09-30 20:06:58 -0700161 mFolderName.setOnFocusChangeListener(this);
Adam Cohen76fc0852011-06-17 13:26:23 -0700162
163 // We find out how tall the text view wants to be (it is set to wrap_content), so that
164 // we can allocate the appropriate amount of space for it.
165 int measureSpec = MeasureSpec.UNSPECIFIED;
166 mFolderName.measure(measureSpec, measureSpec);
167 mFolderNameHeight = mFolderName.getMeasuredHeight();
168
169 // We disable action mode for now since it messes up the view on phones
170 mFolderName.setCustomSelectionActionModeCallback(mActionModeCallback);
Adam Cohen76fc0852011-06-17 13:26:23 -0700171 mFolderName.setOnEditorActionListener(this);
Adam Cohen4ef610f2011-06-21 22:37:36 -0700172 mFolderName.setSelectAllOnFocus(true);
Adam Cohen7a14d0b2011-06-24 11:36:35 -0700173 mFolderName.setInputType(mFolderName.getInputType() |
174 InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS | InputType.TYPE_TEXT_FLAG_CAP_WORDS);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800175 }
Adam Cohen2801caf2011-05-13 20:57:39 -0700176
Adam Cohen76fc0852011-06-17 13:26:23 -0700177 private ActionMode.Callback mActionModeCallback = new ActionMode.Callback() {
178 public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
179 return false;
180 }
181
182 public boolean onCreateActionMode(ActionMode mode, Menu menu) {
183 return false;
184 }
185
186 public void onDestroyActionMode(ActionMode mode) {
187 }
188
189 public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
190 return false;
191 }
192 };
193
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800194 public void onClick(View v) {
Adam Cohendf2cc412011-04-27 16:56:57 -0700195 Object tag = v.getTag();
196 if (tag instanceof ShortcutInfo) {
197 // refactor this code from Folder
198 ShortcutInfo item = (ShortcutInfo) tag;
199 int[] pos = new int[2];
200 v.getLocationOnScreen(pos);
201 item.intent.setSourceBounds(new Rect(pos[0], pos[1],
202 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Winson Chungc7450e32012-04-17 17:34:08 -0700203
204 mLauncher.startActivitySafely(v, item.intent, item);
Adam Cohendf2cc412011-04-27 16:56:57 -0700205 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800206 }
207
208 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -0700209 // Return if global dragging is not enabled
210 if (!mLauncher.isDraggingEnabled()) return true;
211
Adam Cohendf2cc412011-04-27 16:56:57 -0700212 Object tag = v.getTag();
213 if (tag instanceof ShortcutInfo) {
Adam Cohendf2cc412011-04-27 16:56:57 -0700214 ShortcutInfo item = (ShortcutInfo) tag;
215 if (!v.isInTouchMode()) {
216 return false;
217 }
218
Winson Chung7d7541e2011-09-16 20:14:36 -0700219 mLauncher.dismissFolderCling(null);
220
Adam Cohendf2cc412011-04-27 16:56:57 -0700221 mLauncher.getWorkspace().onDragStartedWithItem(v);
Adam Cohenac8c8762011-07-13 11:15:27 -0700222 mLauncher.getWorkspace().beginDragShared(v, this);
Adam Cohenbadf71e2011-05-26 19:08:29 -0700223 mIconDrawable = ((TextView) v).getCompoundDrawables()[1];
Adam Cohen76078c42011-06-09 15:06:52 -0700224
225 mCurrentDragInfo = item;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700226 mEmptyCell[0] = item.cellX;
227 mEmptyCell[1] = item.cellY;
228 mCurrentDragView = v;
Adam Cohenfc53cd22011-07-20 15:45:11 -0700229
230 mContent.removeView(mCurrentDragView);
231 mInfo.remove(mCurrentDragInfo);
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700232 mDragInProgress = true;
Adam Cohen05e0f402011-08-01 12:12:49 -0700233 mItemAddedBackToSelfViaIcon = false;
Adam Cohendf2cc412011-04-27 16:56:57 -0700234 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800235 return true;
236 }
237
Adam Cohen76fc0852011-06-17 13:26:23 -0700238 public boolean isEditingName() {
239 return mIsEditingName;
240 }
241
242 public void startEditingFolderName() {
Adam Cohena65beee2011-06-27 21:32:23 -0700243 mFolderName.setHint("");
Adam Cohen76fc0852011-06-17 13:26:23 -0700244 mIsEditingName = true;
245 }
246
247 public void dismissEditingName() {
248 mInputMethodManager.hideSoftInputFromWindow(getWindowToken(), 0);
249 doneEditingFolderName(true);
250 }
251
252 public void doneEditingFolderName(boolean commit) {
Adam Cohena65beee2011-06-27 21:32:23 -0700253 mFolderName.setHint(sHintText);
Adam Cohen1df26a32011-08-12 16:15:23 -0700254 // Convert to a string here to ensure that no other state associated with the text field
255 // gets saved.
Adam Cohen3371da02011-10-25 21:38:29 -0700256 String newTitle = mFolderName.getText().toString();
257 mInfo.setTitle(newTitle);
Adam Cohen76fc0852011-06-17 13:26:23 -0700258 LauncherModel.updateItemInDatabase(mLauncher, mInfo);
Adam Cohenac56cff2011-09-28 20:45:37 -0700259
Adam Cohen3371da02011-10-25 21:38:29 -0700260 if (commit) {
261 sendCustomAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED,
Michael Jurka8b805b12012-04-18 14:23:14 -0700262 String.format(getContext().getString(R.string.folder_renamed), newTitle));
Adam Cohen3371da02011-10-25 21:38:29 -0700263 }
Adam Cohenac56cff2011-09-28 20:45:37 -0700264 // In order to clear the focus from the text field, we set the focus on ourself. This
265 // ensures that every time the field is clicked, focus is gained, giving reliable behavior.
266 requestFocus();
267
Adam Cohene601a432011-07-26 21:51:30 -0700268 Selection.setSelection((Spannable) mFolderName.getText(), 0, 0);
Adam Cohen76fc0852011-06-17 13:26:23 -0700269 mIsEditingName = false;
270 }
271
272 public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
273 if (actionId == EditorInfo.IME_ACTION_DONE) {
274 dismissEditingName();
275 return true;
276 }
277 return false;
278 }
279
280 public View getEditTextRegion() {
281 return mFolderName;
282 }
283
Adam Cohenbadf71e2011-05-26 19:08:29 -0700284 public Drawable getDragDrawable() {
285 return mIconDrawable;
286 }
287
Adam Cohen0c872ba2011-05-05 10:34:16 -0700288 /**
289 * We need to handle touch events to prevent them from falling through to the workspace below.
290 */
291 @Override
292 public boolean onTouchEvent(MotionEvent ev) {
293 return true;
294 }
295
Joe Onorato00acb122009-08-04 16:04:30 -0400296 public void setDragController(DragController dragController) {
297 mDragController = dragController;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800298 }
299
Adam Cohen2801caf2011-05-13 20:57:39 -0700300 void setFolderIcon(FolderIcon icon) {
301 mFolderIcon = icon;
302 }
303
Adam Cohen3371da02011-10-25 21:38:29 -0700304 @Override
305 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
306 // When the folder gets focus, we don't want to announce the list of items.
307 return true;
308 }
309
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800310 /**
311 * @return the FolderInfo object associated with this folder
312 */
313 FolderInfo getInfo() {
314 return mInfo;
315 }
316
Adam Cohen3bf84d32012-05-07 20:17:14 -0700317 private class GridComparator implements Comparator<ShortcutInfo> {
318 int mNumCols;
319 public GridComparator(int numCols) {
320 mNumCols = numCols;
321 }
322
323 @Override
324 public int compare(ShortcutInfo lhs, ShortcutInfo rhs) {
325 int lhIndex = lhs.cellY * mNumCols + lhs.cellX;
326 int rhIndex = rhs.cellY * mNumCols + rhs.cellX;
327 return (lhIndex - rhIndex);
328 }
Adam Cohen3bf84d32012-05-07 20:17:14 -0700329 }
330
331 private void placeInReadingOrder(ArrayList<ShortcutInfo> items) {
332 int maxX = 0;
333 int count = items.size();
334 for (int i = 0; i < count; i++) {
335 ShortcutInfo item = items.get(i);
336 if (item.cellX > maxX) {
337 maxX = item.cellX;
338 }
339 }
Adam Cohen691a5792012-05-11 14:27:30 -0700340
341 GridComparator gridComparator = new GridComparator(maxX + 1);
Adam Cohen3bf84d32012-05-07 20:17:14 -0700342 Collections.sort(items, gridComparator);
343 final int countX = mContent.getCountX();
344 for (int i = 0; i < count; i++) {
345 int x = i % countX;
346 int y = i / countX;
347 ShortcutInfo item = items.get(i);
348 item.cellX = x;
349 item.cellY = y;
350 }
351 }
352
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800353 void bind(FolderInfo info) {
354 mInfo = info;
Adam Cohendf2cc412011-04-27 16:56:57 -0700355 ArrayList<ShortcutInfo> children = info.contents;
Adam Cohenc508b2d2011-06-28 14:41:44 -0700356 ArrayList<ShortcutInfo> overflow = new ArrayList<ShortcutInfo>();
Adam Cohen7c693212011-05-18 15:26:57 -0700357 setupContentForNumItems(children.size());
Adam Cohen3bf84d32012-05-07 20:17:14 -0700358 placeInReadingOrder(children);
Adam Cohen0057bbc2011-08-12 18:30:51 -0700359 int count = 0;
Adam Cohendf2cc412011-04-27 16:56:57 -0700360 for (int i = 0; i < children.size(); i++) {
361 ShortcutInfo child = (ShortcutInfo) children.get(i);
Adam Cohenc508b2d2011-06-28 14:41:44 -0700362 if (!createAndAddShortcut(child)) {
363 overflow.add(child);
Adam Cohen0057bbc2011-08-12 18:30:51 -0700364 } else {
365 count++;
Adam Cohenc508b2d2011-06-28 14:41:44 -0700366 }
Adam Cohendf2cc412011-04-27 16:56:57 -0700367 }
Adam Cohenc508b2d2011-06-28 14:41:44 -0700368
Adam Cohen0057bbc2011-08-12 18:30:51 -0700369 // We rearrange the items in case there are any empty gaps
370 setupContentForNumItems(count);
371
Adam Cohenc508b2d2011-06-28 14:41:44 -0700372 // If our folder has too many items we prune them from the list. This is an issue
373 // when upgrading from the old Folders implementation which could contain an unlimited
374 // number of items.
375 for (ShortcutInfo item: overflow) {
376 mInfo.remove(item);
377 LauncherModel.deleteItemFromDatabase(mLauncher, item);
378 }
379
Adam Cohen4dbe6d92011-05-18 17:14:15 -0700380 mItemsInvalidated = true;
Adam Cohenac56cff2011-09-28 20:45:37 -0700381 updateTextViewFocus();
Adam Cohena9cf38f2011-05-02 15:36:58 -0700382 mInfo.addListener(this);
Adam Cohen4ef610f2011-06-21 22:37:36 -0700383
Adam Cohenafb01ee2011-06-23 15:38:03 -0700384 if (!sDefaultFolderName.contentEquals(mInfo.title)) {
Adam Cohen4ef610f2011-06-21 22:37:36 -0700385 mFolderName.setText(mInfo.title);
386 } else {
387 mFolderName.setText("");
388 }
Adam Cohen691a5792012-05-11 14:27:30 -0700389 updateItemLocationsInDatabase();
Adam Cohendf2cc412011-04-27 16:56:57 -0700390 }
391
392 /**
393 * Creates a new UserFolder, inflated from R.layout.user_folder.
394 *
395 * @param context The application's context.
396 *
397 * @return A new UserFolder.
398 */
399 static Folder fromXml(Context context) {
400 return (Folder) LayoutInflater.from(context).inflate(R.layout.user_folder, null);
401 }
402
403 /**
404 * This method is intended to make the UserFolder to be visually identical in size and position
405 * to its associated FolderIcon. This allows for a seamless transition into the expanded state.
406 */
407 private void positionAndSizeAsIcon() {
Adam Cohen3e8f8112011-07-02 18:03:00 -0700408 if (!(getParent() instanceof DragLayer)) return;
Adam Cohen662b5982011-12-13 17:45:21 -0800409 setScaleX(0.8f);
410 setScaleY(0.8f);
411 setAlpha(0f);
Adam Cohendf2cc412011-04-27 16:56:57 -0700412 mState = STATE_SMALL;
413 }
414
415 public void animateOpen() {
Adam Cohen3e8f8112011-07-02 18:03:00 -0700416 positionAndSizeAsIcon();
417
Adam Cohen8e776a62011-06-28 18:10:06 -0700418 if (!(getParent() instanceof DragLayer)) return;
Adam Cohen2801caf2011-05-13 20:57:39 -0700419 centerAboutIcon();
Adam Cohen662b5982011-12-13 17:45:21 -0800420 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1);
421 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.0f);
422 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.0f);
Michael Jurka0121c3e2012-05-31 08:36:04 -0700423 final ObjectAnimator oa = mOpenCloseAnimator =
Michael Jurka2ecf9952012-06-18 12:52:28 -0700424 LauncherAnimUtils.ofPropertyValuesHolder(this, alpha, scaleX, scaleY);
Adam Cohen6441de02011-12-14 14:25:32 -0800425
Adam Cohen2801caf2011-05-13 20:57:39 -0700426 oa.addListener(new AnimatorListenerAdapter() {
Adam Cohendf2cc412011-04-27 16:56:57 -0700427 @Override
428 public void onAnimationStart(Animator animation) {
Adam Cohen3371da02011-10-25 21:38:29 -0700429 sendCustomAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED,
Michael Jurka8b805b12012-04-18 14:23:14 -0700430 String.format(getContext().getString(R.string.folder_opened),
Adam Cohen3371da02011-10-25 21:38:29 -0700431 mContent.getCountX(), mContent.getCountY()));
Adam Cohendf2cc412011-04-27 16:56:57 -0700432 mState = STATE_ANIMATING;
433 }
434 @Override
435 public void onAnimationEnd(Animator animation) {
Adam Cohen2801caf2011-05-13 20:57:39 -0700436 mState = STATE_OPEN;
Michael Jurka0121c3e2012-05-31 08:36:04 -0700437 setLayerType(LAYER_TYPE_NONE, null);
Winson Chung7a74ac92011-09-20 17:43:51 -0700438 Cling cling = mLauncher.showFirstRunFoldersCling();
439 if (cling != null) {
440 cling.bringToFront();
441 }
Adam Cohenac56cff2011-09-28 20:45:37 -0700442 setFocusOnFirstChild();
Adam Cohendf2cc412011-04-27 16:56:57 -0700443 }
444 });
Adam Cohen2801caf2011-05-13 20:57:39 -0700445 oa.setDuration(mExpandDuration);
Michael Jurka0121c3e2012-05-31 08:36:04 -0700446 setLayerType(LAYER_TYPE_HARDWARE, null);
Michael Jurkaf1ad6082013-03-13 12:55:46 +0100447 oa.start();
Adam Cohendf2cc412011-04-27 16:56:57 -0700448 }
449
Adam Cohen3371da02011-10-25 21:38:29 -0700450 private void sendCustomAccessibilityEvent(int type, String text) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700451 AccessibilityManager accessibilityManager = (AccessibilityManager)
452 getContext().getSystemService(Context.ACCESSIBILITY_SERVICE);
453 if (accessibilityManager.isEnabled()) {
Adam Cohen3371da02011-10-25 21:38:29 -0700454 AccessibilityEvent event = AccessibilityEvent.obtain(type);
455 onInitializeAccessibilityEvent(event);
456 event.getText().add(text);
Michael Jurka8b805b12012-04-18 14:23:14 -0700457 accessibilityManager.sendAccessibilityEvent(event);
Adam Cohen3371da02011-10-25 21:38:29 -0700458 }
459 }
460
Adam Cohenac56cff2011-09-28 20:45:37 -0700461 private void setFocusOnFirstChild() {
462 View firstChild = mContent.getChildAt(0, 0);
463 if (firstChild != null) {
464 firstChild.requestFocus();
465 }
466 }
467
Adam Cohendf2cc412011-04-27 16:56:57 -0700468 public void animateClosed() {
Adam Cohen8e776a62011-06-28 18:10:06 -0700469 if (!(getParent() instanceof DragLayer)) return;
Adam Cohen662b5982011-12-13 17:45:21 -0800470 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0);
471 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 0.9f);
472 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 0.9f);
Michael Jurka0121c3e2012-05-31 08:36:04 -0700473 final ObjectAnimator oa = mOpenCloseAnimator =
Michael Jurka2ecf9952012-06-18 12:52:28 -0700474 LauncherAnimUtils.ofPropertyValuesHolder(this, alpha, scaleX, scaleY);
Adam Cohendf2cc412011-04-27 16:56:57 -0700475
Adam Cohen2801caf2011-05-13 20:57:39 -0700476 oa.addListener(new AnimatorListenerAdapter() {
Adam Cohendf2cc412011-04-27 16:56:57 -0700477 @Override
478 public void onAnimationEnd(Animator animation) {
Adam Cohen2801caf2011-05-13 20:57:39 -0700479 onCloseComplete();
Michael Jurka0121c3e2012-05-31 08:36:04 -0700480 setLayerType(LAYER_TYPE_NONE, null);
Adam Cohen2801caf2011-05-13 20:57:39 -0700481 mState = STATE_SMALL;
Adam Cohendf2cc412011-04-27 16:56:57 -0700482 }
483 @Override
484 public void onAnimationStart(Animator animation) {
Adam Cohen3371da02011-10-25 21:38:29 -0700485 sendCustomAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED,
Michael Jurka8b805b12012-04-18 14:23:14 -0700486 getContext().getString(R.string.folder_closed));
Adam Cohendf2cc412011-04-27 16:56:57 -0700487 mState = STATE_ANIMATING;
488 }
489 });
Adam Cohen2801caf2011-05-13 20:57:39 -0700490 oa.setDuration(mExpandDuration);
Michael Jurka0121c3e2012-05-31 08:36:04 -0700491 setLayerType(LAYER_TYPE_HARDWARE, null);
Michael Jurkaf1ad6082013-03-13 12:55:46 +0100492 oa.start();
Adam Cohendf2cc412011-04-27 16:56:57 -0700493 }
494
495 void notifyDataSetChanged() {
496 // recreate all the children if the data set changes under us. We may want to do this more
497 // intelligently (ie just removing the views that should no longer exist)
498 mContent.removeAllViewsInLayout();
499 bind(mInfo);
500 }
501
Adam Cohencb3382b2011-05-24 14:07:08 -0700502 public boolean acceptDrop(DragObject d) {
503 final ItemInfo item = (ItemInfo) d.dragInfo;
Adam Cohendf2cc412011-04-27 16:56:57 -0700504 final int itemType = item.itemType;
Adam Cohen2801caf2011-05-13 20:57:39 -0700505 return ((itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION ||
506 itemType == LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT) &&
507 !isFull());
Adam Cohendf2cc412011-04-27 16:56:57 -0700508 }
509
Adam Cohendf2cc412011-04-27 16:56:57 -0700510 protected boolean findAndSetEmptyCells(ShortcutInfo item) {
511 int[] emptyCell = new int[2];
512 if (mContent.findCellForSpan(emptyCell, item.spanX, item.spanY)) {
513 item.cellX = emptyCell[0];
514 item.cellY = emptyCell[1];
Adam Cohendf2cc412011-04-27 16:56:57 -0700515 return true;
516 } else {
517 return false;
518 }
519 }
520
Adam Cohenc508b2d2011-06-28 14:41:44 -0700521 protected boolean createAndAddShortcut(ShortcutInfo item) {
Adam Cohendf2cc412011-04-27 16:56:57 -0700522 final TextView textView =
Adam Cohene87b9242011-06-29 14:01:26 -0700523 (TextView) mInflater.inflate(R.layout.application, this, false);
Adam Cohendf2cc412011-04-27 16:56:57 -0700524 textView.setCompoundDrawablesWithIntrinsicBounds(null,
525 new FastBitmapDrawable(item.getIcon(mIconCache)), null, null);
526 textView.setText(item.title);
527 textView.setTag(item);
528
529 textView.setOnClickListener(this);
530 textView.setOnLongClickListener(this);
531
Adam Cohenc508b2d2011-06-28 14:41:44 -0700532 // We need to check here to verify that the given item's location isn't already occupied
Adam Cohen3bf84d32012-05-07 20:17:14 -0700533 // by another item.
Adam Cohen0057bbc2011-08-12 18:30:51 -0700534 if (mContent.getChildAt(item.cellX, item.cellY) != null || item.cellX < 0 || item.cellY < 0
535 || item.cellX >= mContent.getCountX() || item.cellY >= mContent.getCountY()) {
Adam Cohen3bf84d32012-05-07 20:17:14 -0700536 // This shouldn't happen, log it.
537 Log.e(TAG, "Folder order not properly persisted during bind");
Adam Cohenc508b2d2011-06-28 14:41:44 -0700538 if (!findAndSetEmptyCells(item)) {
539 return false;
540 }
541 }
542
Adam Cohendf2cc412011-04-27 16:56:57 -0700543 CellLayout.LayoutParams lp =
544 new CellLayout.LayoutParams(item.cellX, item.cellY, item.spanX, item.spanY);
545 boolean insert = false;
Adam Cohenac56cff2011-09-28 20:45:37 -0700546 textView.setOnKeyListener(new FolderKeyEventListener());
Adam Cohendf2cc412011-04-27 16:56:57 -0700547 mContent.addViewToCellLayout(textView, insert ? 0 : -1, (int)item.id, lp, true);
Adam Cohenc508b2d2011-06-28 14:41:44 -0700548 return true;
Adam Cohendf2cc412011-04-27 16:56:57 -0700549 }
550
Adam Cohencb3382b2011-05-24 14:07:08 -0700551 public void onDragEnter(DragObject d) {
Adam Cohenbfbfd262011-06-13 16:55:12 -0700552 mPreviousTargetCell[0] = -1;
553 mPreviousTargetCell[1] = -1;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700554 mOnExitAlarm.cancelAlarm();
555 }
556
557 OnAlarmListener mReorderAlarmListener = new OnAlarmListener() {
558 public void onAlarm(Alarm alarm) {
559 realTimeReorder(mEmptyCell, mTargetCell);
560 }
561 };
562
563 boolean readingOrderGreaterThan(int[] v1, int[] v2) {
564 if (v1[1] > v2[1] || (v1[1] == v2[1] && v1[0] > v2[0])) {
565 return true;
566 } else {
567 return false;
568 }
569 }
570
571 private void realTimeReorder(int[] empty, int[] target) {
572 boolean wrap;
573 int startX;
574 int endX;
575 int startY;
Adam Cohen76fc0852011-06-17 13:26:23 -0700576 int delay = 0;
577 float delayAmount = 30;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700578 if (readingOrderGreaterThan(target, empty)) {
579 wrap = empty[0] >= mContent.getCountX() - 1;
580 startY = wrap ? empty[1] + 1 : empty[1];
581 for (int y = startY; y <= target[1]; y++) {
582 startX = y == empty[1] ? empty[0] + 1 : 0;
583 endX = y < target[1] ? mContent.getCountX() - 1 : target[0];
584 for (int x = startX; x <= endX; x++) {
585 View v = mContent.getChildAt(x,y);
586 if (mContent.animateChildToPosition(v, empty[0], empty[1],
Adam Cohen482ed822012-03-02 14:15:13 -0800587 REORDER_ANIMATION_DURATION, delay, true, true)) {
Adam Cohenbfbfd262011-06-13 16:55:12 -0700588 empty[0] = x;
589 empty[1] = y;
Adam Cohen76fc0852011-06-17 13:26:23 -0700590 delay += delayAmount;
591 delayAmount *= 0.9;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700592 }
593 }
594 }
595 } else {
596 wrap = empty[0] == 0;
597 startY = wrap ? empty[1] - 1 : empty[1];
598 for (int y = startY; y >= target[1]; y--) {
599 startX = y == empty[1] ? empty[0] - 1 : mContent.getCountX() - 1;
600 endX = y > target[1] ? 0 : target[0];
601 for (int x = startX; x >= endX; x--) {
602 View v = mContent.getChildAt(x,y);
603 if (mContent.animateChildToPosition(v, empty[0], empty[1],
Adam Cohen482ed822012-03-02 14:15:13 -0800604 REORDER_ANIMATION_DURATION, delay, true, true)) {
Adam Cohenbfbfd262011-06-13 16:55:12 -0700605 empty[0] = x;
606 empty[1] = y;
Adam Cohen76fc0852011-06-17 13:26:23 -0700607 delay += delayAmount;
608 delayAmount *= 0.9;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700609 }
610 }
611 }
612 }
Adam Cohendf2cc412011-04-27 16:56:57 -0700613 }
614
Adam Cohencb3382b2011-05-24 14:07:08 -0700615 public void onDragOver(DragObject d) {
Adam Cohenbfbfd262011-06-13 16:55:12 -0700616 float[] r = getDragViewVisualCenter(d.x, d.y, d.xOffset, d.yOffset, d.dragView, null);
617 mTargetCell = mContent.findNearestArea((int) r[0], (int) r[1], 1, 1, mTargetCell);
618
619 if (mTargetCell[0] != mPreviousTargetCell[0] || mTargetCell[1] != mPreviousTargetCell[1]) {
620 mReorderAlarm.cancelAlarm();
621 mReorderAlarm.setOnAlarmListener(mReorderAlarmListener);
622 mReorderAlarm.setAlarm(150);
623 mPreviousTargetCell[0] = mTargetCell[0];
624 mPreviousTargetCell[1] = mTargetCell[1];
625 }
Adam Cohendf2cc412011-04-27 16:56:57 -0700626 }
627
Adam Cohenbfbfd262011-06-13 16:55:12 -0700628 // This is used to compute the visual center of the dragView. The idea is that
629 // the visual center represents the user's interpretation of where the item is, and hence
630 // is the appropriate point to use when determining drop location.
631 private float[] getDragViewVisualCenter(int x, int y, int xOffset, int yOffset,
632 DragView dragView, float[] recycle) {
633 float res[];
634 if (recycle == null) {
635 res = new float[2];
636 } else {
637 res = recycle;
638 }
639
640 // These represent the visual top and left of drag view if a dragRect was provided.
641 // If a dragRect was not provided, then they correspond to the actual view left and
642 // top, as the dragRect is in that case taken to be the entire dragView.
643 // R.dimen.dragViewOffsetY.
644 int left = x - xOffset;
645 int top = y - yOffset;
646
647 // In order to find the visual center, we shift by half the dragRect
648 res[0] = left + dragView.getDragRegion().width() / 2;
649 res[1] = top + dragView.getDragRegion().height() / 2;
650
651 return res;
652 }
653
654 OnAlarmListener mOnExitAlarmListener = new OnAlarmListener() {
655 public void onAlarm(Alarm alarm) {
Adam Cohen3e8f8112011-07-02 18:03:00 -0700656 completeDragExit();
Adam Cohenbfbfd262011-06-13 16:55:12 -0700657 }
658 };
659
Adam Cohen95bb8002011-07-03 23:40:28 -0700660 public void completeDragExit() {
Adam Cohen3e8f8112011-07-02 18:03:00 -0700661 mLauncher.closeFolder();
662 mCurrentDragInfo = null;
663 mCurrentDragView = null;
664 mSuppressOnAdd = false;
665 mRearrangeOnClose = true;
666 }
667
Adam Cohencb3382b2011-05-24 14:07:08 -0700668 public void onDragExit(DragObject d) {
Adam Cohenbfbfd262011-06-13 16:55:12 -0700669 // We only close the folder if this is a true drag exit, ie. not because a drop
670 // has occurred above the folder.
671 if (!d.dragComplete) {
672 mOnExitAlarm.setOnAlarmListener(mOnExitAlarmListener);
673 mOnExitAlarm.setAlarm(ON_EXIT_CLOSE_DELAY);
674 }
675 mReorderAlarm.cancelAlarm();
Adam Cohen2801caf2011-05-13 20:57:39 -0700676 }
677
Winson Chunga48487a2012-03-20 16:19:37 -0700678 public void onDropCompleted(View target, DragObject d, boolean isFlingToDelete,
679 boolean success) {
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700680 if (success) {
Adam Cohen05e0f402011-08-01 12:12:49 -0700681 if (mDeleteFolderOnDropCompleted && !mItemAddedBackToSelfViaIcon) {
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700682 replaceFolderWithFinalItem();
683 }
684 } else {
685 // The drag failed, we need to return the item to the folder
686 mFolderIcon.onDrop(d);
687
688 // We're going to trigger a "closeFolder" which may occur before this item has
689 // been added back to the folder -- this could cause the folder to be deleted
690 if (mOnExitAlarm.alarmPending()) {
691 mSuppressFolderDeletion = true;
692 }
693 }
694
695 if (target != this) {
696 if (mOnExitAlarm.alarmPending()) {
697 mOnExitAlarm.cancelAlarm();
698 completeDragExit();
699 }
700 }
701 mDeleteFolderOnDropCompleted = false;
702 mDragInProgress = false;
Adam Cohen05e0f402011-08-01 12:12:49 -0700703 mItemAddedBackToSelfViaIcon = false;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700704 mCurrentDragInfo = null;
705 mCurrentDragView = null;
706 mSuppressOnAdd = false;
Adam Cohen4045eb72011-10-06 11:44:26 -0700707
708 // Reordering may have occured, and we need to save the new item locations. We do this once
709 // at the end to prevent unnecessary database operations.
710 updateItemLocationsInDatabase();
711 }
712
Winson Chunga48487a2012-03-20 16:19:37 -0700713 @Override
Winson Chung043f2af2012-03-01 16:09:54 -0800714 public boolean supportsFlingToDelete() {
715 return true;
716 }
717
Winson Chunga48487a2012-03-20 16:19:37 -0700718 public void onFlingToDelete(DragObject d, int x, int y, PointF vec) {
719 // Do nothing
720 }
721
722 @Override
723 public void onFlingToDeleteCompleted() {
724 // Do nothing
725 }
726
Adam Cohen4045eb72011-10-06 11:44:26 -0700727 private void updateItemLocationsInDatabase() {
728 ArrayList<View> list = getItemsInReadingOrder();
729 for (int i = 0; i < list.size(); i++) {
730 View v = list.get(i);
731 ItemInfo info = (ItemInfo) v.getTag();
732 LauncherModel.moveItemInDatabase(mLauncher, info, mInfo.id, 0,
733 info.cellX, info.cellY);
734 }
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700735 }
Adam Cohen228da5a2011-07-27 22:23:47 -0700736
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700737 public void notifyDrop() {
738 if (mDragInProgress) {
Adam Cohen05e0f402011-08-01 12:12:49 -0700739 mItemAddedBackToSelfViaIcon = true;
Adam Cohen76078c42011-06-09 15:06:52 -0700740 }
Adam Cohendf2cc412011-04-27 16:56:57 -0700741 }
742
743 public boolean isDropEnabled() {
744 return true;
745 }
746
Adam Cohencb3382b2011-05-24 14:07:08 -0700747 public DropTarget getDropTargetDelegate(DragObject d) {
Adam Cohendf2cc412011-04-27 16:56:57 -0700748 return null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800749 }
Adam Cohena9cf38f2011-05-02 15:36:58 -0700750
Adam Cohen4045eb72011-10-06 11:44:26 -0700751 private void setupContentDimensions(int count) {
Adam Cohen2801caf2011-05-13 20:57:39 -0700752 ArrayList<View> list = getItemsInReadingOrder();
753
754 int countX = mContent.getCountX();
755 int countY = mContent.getCountY();
Adam Cohen7c693212011-05-18 15:26:57 -0700756 boolean done = false;
Adam Cohen2801caf2011-05-13 20:57:39 -0700757
Adam Cohen7c693212011-05-18 15:26:57 -0700758 while (!done) {
759 int oldCountX = countX;
760 int oldCountY = countY;
761 if (countX * countY < count) {
762 // Current grid is too small, expand it
Adam Cohen78dc83e2011-11-15 17:10:00 -0800763 if ((countX <= countY || countY == mMaxCountY) && countX < mMaxCountX) {
Adam Cohen7c693212011-05-18 15:26:57 -0700764 countX++;
765 } else if (countY < mMaxCountY) {
766 countY++;
767 }
768 if (countY == 0) countY++;
769 } else if ((countY - 1) * countX >= count && countY >= countX) {
770 countY = Math.max(0, countY - 1);
771 } else if ((countX - 1) * countY >= count) {
772 countX = Math.max(0, countX - 1);
Adam Cohen2801caf2011-05-13 20:57:39 -0700773 }
Adam Cohen7c693212011-05-18 15:26:57 -0700774 done = countX == oldCountX && countY == oldCountY;
Adam Cohen2801caf2011-05-13 20:57:39 -0700775 }
Adam Cohen7c693212011-05-18 15:26:57 -0700776 mContent.setGridSize(countX, countY);
Adam Cohen2801caf2011-05-13 20:57:39 -0700777 arrangeChildren(list);
778 }
779
780 public boolean isFull() {
Adam Cohen78dc83e2011-11-15 17:10:00 -0800781 return getItemCount() >= mMaxNumItems;
Adam Cohen2801caf2011-05-13 20:57:39 -0700782 }
783
784 private void centerAboutIcon() {
Adam Cohen8e776a62011-06-28 18:10:06 -0700785 DragLayer.LayoutParams lp = (DragLayer.LayoutParams) getLayoutParams();
Adam Cohen2801caf2011-05-13 20:57:39 -0700786
787 int width = getPaddingLeft() + getPaddingRight() + mContent.getDesiredWidth();
Adam Cohenf4bb1cd2011-07-22 14:36:03 -0700788 int height = getPaddingTop() + getPaddingBottom() + mContent.getDesiredHeight()
789 + mFolderNameHeight;
Adam Cohen8e776a62011-06-28 18:10:06 -0700790 DragLayer parent = (DragLayer) mLauncher.findViewById(R.id.drag_layer);
Adam Cohen2801caf2011-05-13 20:57:39 -0700791
Adam Cohen307fe232012-08-16 17:55:58 -0700792 float scale = parent.getDescendantRectRelativeToSelf(mFolderIcon, mTempRect);
Adam Cohen8e776a62011-06-28 18:10:06 -0700793
Adam Cohen307fe232012-08-16 17:55:58 -0700794 int centerX = (int) (mTempRect.left + mTempRect.width() * scale / 2);
795 int centerY = (int) (mTempRect.top + mTempRect.height() * scale / 2);
Adam Cohen2801caf2011-05-13 20:57:39 -0700796 int centeredLeft = centerX - width / 2;
797 int centeredTop = centerY - height / 2;
798
Adam Cohen7cc1bc42011-12-13 21:28:43 -0800799 int currentPage = mLauncher.getWorkspace().getCurrentPage();
800 // In case the workspace is scrolling, we need to use the final scroll to compute
801 // the folders bounds.
802 mLauncher.getWorkspace().setFinalScrollForPageChange(currentPage);
Adam Cohen35e7e642011-07-17 14:47:18 -0700803 // We first fetch the currently visible CellLayoutChildren
Adam Cohen7cc1bc42011-12-13 21:28:43 -0800804 CellLayout currentLayout = (CellLayout) mLauncher.getWorkspace().getChildAt(currentPage);
Michael Jurkaa52570f2012-03-20 03:18:20 -0700805 ShortcutAndWidgetContainer boundingLayout = currentLayout.getShortcutsAndWidgets();
Adam Cohen35e7e642011-07-17 14:47:18 -0700806 Rect bounds = new Rect();
807 parent.getDescendantRectRelativeToSelf(boundingLayout, bounds);
Adam Cohen7cc1bc42011-12-13 21:28:43 -0800808 // We reset the workspaces scroll
809 mLauncher.getWorkspace().resetFinalScrollForPageChange(currentPage);
Adam Cohen2801caf2011-05-13 20:57:39 -0700810
Adam Cohen35e7e642011-07-17 14:47:18 -0700811 // We need to bound the folder to the currently visible CellLayoutChildren
812 int left = Math.min(Math.max(bounds.left, centeredLeft),
813 bounds.left + bounds.width() - width);
814 int top = Math.min(Math.max(bounds.top, centeredTop),
815 bounds.top + bounds.height() - height);
816 // If the folder doesn't fit within the bounds, center it about the desired bounds
817 if (width >= bounds.width()) {
818 left = bounds.left + (bounds.width() - width) / 2;
Adam Cohen0e4857c2011-06-30 17:05:14 -0700819 }
Adam Cohen35e7e642011-07-17 14:47:18 -0700820 if (height >= bounds.height()) {
821 top = bounds.top + (bounds.height() - height) / 2;
Adam Cohen0e4857c2011-06-30 17:05:14 -0700822 }
Adam Cohen2801caf2011-05-13 20:57:39 -0700823
824 int folderPivotX = width / 2 + (centeredLeft - left);
825 int folderPivotY = height / 2 + (centeredTop - top);
826 setPivotX(folderPivotX);
827 setPivotY(folderPivotY);
Adam Cohen268c4752012-06-06 17:47:33 -0700828 mFolderIconPivotX = (int) (mFolderIcon.getMeasuredWidth() *
Adam Cohen2801caf2011-05-13 20:57:39 -0700829 (1.0f * folderPivotX / width));
Adam Cohen268c4752012-06-06 17:47:33 -0700830 mFolderIconPivotY = (int) (mFolderIcon.getMeasuredHeight() *
Adam Cohen2801caf2011-05-13 20:57:39 -0700831 (1.0f * folderPivotY / height));
Adam Cohen3bf84d32012-05-07 20:17:14 -0700832
Adam Cohen662b5982011-12-13 17:45:21 -0800833 lp.width = width;
834 lp.height = height;
835 lp.x = left;
836 lp.y = top;
Adam Cohen2801caf2011-05-13 20:57:39 -0700837 }
838
Adam Cohen268c4752012-06-06 17:47:33 -0700839 float getPivotXForIconAnimation() {
840 return mFolderIconPivotX;
841 }
842 float getPivotYForIconAnimation() {
843 return mFolderIconPivotY;
844 }
845
Adam Cohen2801caf2011-05-13 20:57:39 -0700846 private void setupContentForNumItems(int count) {
Adam Cohen4045eb72011-10-06 11:44:26 -0700847 setupContentDimensions(count);
Adam Cohen2801caf2011-05-13 20:57:39 -0700848
Adam Cohen8e776a62011-06-28 18:10:06 -0700849 DragLayer.LayoutParams lp = (DragLayer.LayoutParams) getLayoutParams();
Adam Cohen2801caf2011-05-13 20:57:39 -0700850 if (lp == null) {
Adam Cohen8e776a62011-06-28 18:10:06 -0700851 lp = new DragLayer.LayoutParams(0, 0);
852 lp.customPosition = true;
Adam Cohen2801caf2011-05-13 20:57:39 -0700853 setLayoutParams(lp);
854 }
855 centerAboutIcon();
856 }
857
Adam Cohen234c4cd2011-07-17 21:03:04 -0700858 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
859 int width = getPaddingLeft() + getPaddingRight() + mContent.getDesiredWidth();
Adam Cohenf4bb1cd2011-07-22 14:36:03 -0700860 int height = getPaddingTop() + getPaddingBottom() + mContent.getDesiredHeight()
861 + mFolderNameHeight;
Adam Cohen234c4cd2011-07-17 21:03:04 -0700862
863 int contentWidthSpec = MeasureSpec.makeMeasureSpec(mContent.getDesiredWidth(),
864 MeasureSpec.EXACTLY);
865 int contentHeightSpec = MeasureSpec.makeMeasureSpec(mContent.getDesiredHeight(),
866 MeasureSpec.EXACTLY);
867 mContent.measure(contentWidthSpec, contentHeightSpec);
868
869 mFolderName.measure(contentWidthSpec,
870 MeasureSpec.makeMeasureSpec(mFolderNameHeight, MeasureSpec.EXACTLY));
871 setMeasuredDimension(width, height);
872 }
873
Adam Cohen2801caf2011-05-13 20:57:39 -0700874 private void arrangeChildren(ArrayList<View> list) {
875 int[] vacant = new int[2];
876 if (list == null) {
877 list = getItemsInReadingOrder();
878 }
879 mContent.removeAllViews();
880
881 for (int i = 0; i < list.size(); i++) {
882 View v = list.get(i);
883 mContent.getVacantCell(vacant, 1, 1);
884 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) v.getLayoutParams();
885 lp.cellX = vacant[0];
886 lp.cellY = vacant[1];
887 ItemInfo info = (ItemInfo) v.getTag();
Adam Cohen2792a332011-09-26 21:09:47 -0700888 if (info.cellX != vacant[0] || info.cellY != vacant[1]) {
889 info.cellX = vacant[0];
890 info.cellY = vacant[1];
891 LauncherModel.addOrMoveItemInDatabase(mLauncher, info, mInfo.id, 0,
892 info.cellX, info.cellY);
893 }
Adam Cohen2801caf2011-05-13 20:57:39 -0700894 boolean insert = false;
895 mContent.addViewToCellLayout(v, insert ? 0 : -1, (int)info.id, lp, true);
896 }
Adam Cohen7c693212011-05-18 15:26:57 -0700897 mItemsInvalidated = true;
Adam Cohen2801caf2011-05-13 20:57:39 -0700898 }
899
Adam Cohena9cf38f2011-05-02 15:36:58 -0700900 public int getItemCount() {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700901 return mContent.getShortcutsAndWidgets().getChildCount();
Adam Cohena9cf38f2011-05-02 15:36:58 -0700902 }
903
904 public View getItemAt(int index) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700905 return mContent.getShortcutsAndWidgets().getChildAt(index);
Adam Cohena9cf38f2011-05-02 15:36:58 -0700906 }
907
Adam Cohen2801caf2011-05-13 20:57:39 -0700908 private void onCloseComplete() {
Adam Cohen05e0f402011-08-01 12:12:49 -0700909 DragLayer parent = (DragLayer) getParent();
Michael Jurka5649c282012-06-18 10:33:21 -0700910 if (parent != null) {
911 parent.removeView(this);
912 }
Adam Cohen4554ee12011-08-03 16:13:21 -0700913 mDragController.removeDropTarget((DropTarget) this);
Adam Cohen05e0f402011-08-01 12:12:49 -0700914 clearFocus();
Adam Cohenac56cff2011-09-28 20:45:37 -0700915 mFolderIcon.requestFocus();
Adam Cohen05e0f402011-08-01 12:12:49 -0700916
Adam Cohen2801caf2011-05-13 20:57:39 -0700917 if (mRearrangeOnClose) {
918 setupContentForNumItems(getItemCount());
919 mRearrangeOnClose = false;
920 }
Adam Cohenafb01ee2011-06-23 15:38:03 -0700921 if (getItemCount() <= 1) {
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700922 if (!mDragInProgress && !mSuppressFolderDeletion) {
923 replaceFolderWithFinalItem();
924 } else if (mDragInProgress) {
925 mDeleteFolderOnDropCompleted = true;
926 }
Adam Cohenafb01ee2011-06-23 15:38:03 -0700927 }
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700928 mSuppressFolderDeletion = false;
Adam Cohenafb01ee2011-06-23 15:38:03 -0700929 }
930
931 private void replaceFolderWithFinalItem() {
Adam Cohenafb01ee2011-06-23 15:38:03 -0700932 // Add the last remaining child to the workspace in place of the folder
Adam Cohenfb91f302012-06-11 15:45:18 -0700933 Runnable onCompleteRunnable = new Runnable() {
934 @Override
935 public void run() {
936 CellLayout cellLayout = mLauncher.getCellLayout(mInfo.container, mInfo.screen);
Adam Cohenafb01ee2011-06-23 15:38:03 -0700937
Adam Cohenc5e63f32012-07-12 16:16:57 -0700938 View child = null;
Adam Cohenfb91f302012-06-11 15:45:18 -0700939 // Move the item from the folder to the workspace, in the position of the folder
940 if (getItemCount() == 1) {
941 ShortcutInfo finalItem = mInfo.contents.get(0);
Adam Cohenc5e63f32012-07-12 16:16:57 -0700942 child = mLauncher.createShortcut(R.layout.application, cellLayout,
Adam Cohenfb91f302012-06-11 15:45:18 -0700943 finalItem);
944 LauncherModel.addOrMoveItemInDatabase(mLauncher, finalItem, mInfo.container,
945 mInfo.screen, mInfo.cellX, mInfo.cellY);
Adam Cohenfb91f302012-06-11 15:45:18 -0700946 }
Adam Cohen487f7dd2012-06-28 18:12:10 -0700947 if (getItemCount() <= 1) {
948 // Remove the folder
949 LauncherModel.deleteItemFromDatabase(mLauncher, mInfo);
950 cellLayout.removeView(mFolderIcon);
951 if (mFolderIcon instanceof DropTarget) {
952 mDragController.removeDropTarget((DropTarget) mFolderIcon);
953 }
954 mLauncher.removeFolder(mInfo);
955 }
Adam Cohenc5e63f32012-07-12 16:16:57 -0700956 // We add the child after removing the folder to prevent both from existing at
957 // the same time in the CellLayout.
958 if (child != null) {
959 mLauncher.getWorkspace().addInScreen(child, mInfo.container, mInfo.screen,
960 mInfo.cellX, mInfo.cellY, mInfo.spanX, mInfo.spanY);
961 }
Adam Cohenfb91f302012-06-11 15:45:18 -0700962 }
963 };
964 View finalChild = getItemAt(0);
965 if (finalChild != null) {
966 mFolderIcon.performDestroyAnimation(finalChild, onCompleteRunnable);
Adam Cohenafb01ee2011-06-23 15:38:03 -0700967 }
Adam Cohenfb91f302012-06-11 15:45:18 -0700968 mDestroyed = true;
969 }
970
971 boolean isDestroyed() {
972 return mDestroyed;
Adam Cohen2801caf2011-05-13 20:57:39 -0700973 }
974
Adam Cohenac56cff2011-09-28 20:45:37 -0700975 // This method keeps track of the last item in the folder for the purposes
976 // of keyboard focus
977 private void updateTextViewFocus() {
978 View lastChild = getItemAt(getItemCount() - 1);
979 getItemAt(getItemCount() - 1);
980 if (lastChild != null) {
981 mFolderName.setNextFocusDownId(lastChild.getId());
982 mFolderName.setNextFocusRightId(lastChild.getId());
983 mFolderName.setNextFocusLeftId(lastChild.getId());
984 mFolderName.setNextFocusUpId(lastChild.getId());
985 }
986 }
987
Adam Cohenbfbfd262011-06-13 16:55:12 -0700988 public void onDrop(DragObject d) {
989 ShortcutInfo item;
990 if (d.dragInfo instanceof ApplicationInfo) {
991 // Came from all apps -- make a copy
992 item = ((ApplicationInfo) d.dragInfo).makeShortcut();
993 item.spanX = 1;
994 item.spanY = 1;
995 } else {
996 item = (ShortcutInfo) d.dragInfo;
997 }
Adam Cohen05e0f402011-08-01 12:12:49 -0700998 // Dragged from self onto self, currently this is the only path possible, however
999 // we keep this as a distinct code path.
Adam Cohenbfbfd262011-06-13 16:55:12 -07001000 if (item == mCurrentDragInfo) {
1001 ShortcutInfo si = (ShortcutInfo) mCurrentDragView.getTag();
1002 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) mCurrentDragView.getLayoutParams();
1003 si.cellX = lp.cellX = mEmptyCell[0];
1004 si.cellX = lp.cellY = mEmptyCell[1];
1005 mContent.addViewToCellLayout(mCurrentDragView, -1, (int)item.id, lp, true);
Adam Cohenfc53cd22011-07-20 15:45:11 -07001006 if (d.dragView.hasDrawn()) {
1007 mLauncher.getDragLayer().animateViewIntoPosition(d.dragView, mCurrentDragView);
1008 } else {
Winson Chung2c4cf412012-05-08 14:03:21 -07001009 d.deferDragViewCleanupPostAnimation = false;
Adam Cohenfc53cd22011-07-20 15:45:11 -07001010 mCurrentDragView.setVisibility(VISIBLE);
1011 }
Adam Cohene9166b22011-07-08 17:11:11 -07001012 mItemsInvalidated = true;
Adam Cohen4045eb72011-10-06 11:44:26 -07001013 setupContentDimensions(getItemCount());
Adam Cohen716b51e2011-06-30 12:09:54 -07001014 mSuppressOnAdd = true;
Adam Cohenbfbfd262011-06-13 16:55:12 -07001015 }
1016 mInfo.add(item);
1017 }
1018
1019 public void onAdd(ShortcutInfo item) {
1020 mItemsInvalidated = true;
Adam Cohen05e0f402011-08-01 12:12:49 -07001021 // If the item was dropped onto this open folder, we have done the work associated
1022 // with adding the item to the folder, as indicated by mSuppressOnAdd being set
Adam Cohenbfbfd262011-06-13 16:55:12 -07001023 if (mSuppressOnAdd) return;
1024 if (!findAndSetEmptyCells(item)) {
1025 // The current layout is full, can we expand it?
1026 setupContentForNumItems(getItemCount() + 1);
1027 findAndSetEmptyCells(item);
1028 }
1029 createAndAddShortcut(item);
1030 LauncherModel.addOrMoveItemInDatabase(
1031 mLauncher, item, mInfo.id, 0, item.cellX, item.cellY);
1032 }
1033
Adam Cohena9cf38f2011-05-02 15:36:58 -07001034 public void onRemove(ShortcutInfo item) {
Adam Cohen7c693212011-05-18 15:26:57 -07001035 mItemsInvalidated = true;
Adam Cohen05e0f402011-08-01 12:12:49 -07001036 // If this item is being dragged from this open folder, we have already handled
1037 // the work associated with removing the item, so we don't have to do anything here.
Adam Cohenbfbfd262011-06-13 16:55:12 -07001038 if (item == mCurrentDragInfo) return;
Adam Cohendf1e4e82011-06-24 15:57:39 -07001039 View v = getViewForInfo(item);
Adam Cohena9cf38f2011-05-02 15:36:58 -07001040 mContent.removeView(v);
Adam Cohen2801caf2011-05-13 20:57:39 -07001041 if (mState == STATE_ANIMATING) {
1042 mRearrangeOnClose = true;
1043 } else {
1044 setupContentForNumItems(getItemCount());
1045 }
Adam Cohenafb01ee2011-06-23 15:38:03 -07001046 if (getItemCount() <= 1) {
1047 replaceFolderWithFinalItem();
1048 }
Adam Cohena9cf38f2011-05-02 15:36:58 -07001049 }
Adam Cohen7c693212011-05-18 15:26:57 -07001050
Adam Cohendf1e4e82011-06-24 15:57:39 -07001051 private View getViewForInfo(ShortcutInfo item) {
1052 for (int j = 0; j < mContent.getCountY(); j++) {
1053 for (int i = 0; i < mContent.getCountX(); i++) {
1054 View v = mContent.getChildAt(i, j);
1055 if (v.getTag() == item) {
1056 return v;
1057 }
1058 }
1059 }
1060 return null;
1061 }
1062
Adam Cohen76078c42011-06-09 15:06:52 -07001063 public void onItemsChanged() {
Adam Cohenac56cff2011-09-28 20:45:37 -07001064 updateTextViewFocus();
Adam Cohen76078c42011-06-09 15:06:52 -07001065 }
Adam Cohenac56cff2011-09-28 20:45:37 -07001066
Adam Cohen76fc0852011-06-17 13:26:23 -07001067 public void onTitleChanged(CharSequence title) {
1068 }
Adam Cohen76078c42011-06-09 15:06:52 -07001069
Adam Cohen7c693212011-05-18 15:26:57 -07001070 public ArrayList<View> getItemsInReadingOrder() {
Adam Cohen76078c42011-06-09 15:06:52 -07001071 return getItemsInReadingOrder(true);
1072 }
1073
1074 public ArrayList<View> getItemsInReadingOrder(boolean includeCurrentDragItem) {
Adam Cohen7c693212011-05-18 15:26:57 -07001075 if (mItemsInvalidated) {
1076 mItemsInReadingOrder.clear();
1077 for (int j = 0; j < mContent.getCountY(); j++) {
1078 for (int i = 0; i < mContent.getCountX(); i++) {
1079 View v = mContent.getChildAt(i, j);
1080 if (v != null) {
Adam Cohen76078c42011-06-09 15:06:52 -07001081 ShortcutInfo info = (ShortcutInfo) v.getTag();
1082 if (info != mCurrentDragInfo || includeCurrentDragItem) {
1083 mItemsInReadingOrder.add(v);
1084 }
Adam Cohen7c693212011-05-18 15:26:57 -07001085 }
1086 }
1087 }
1088 mItemsInvalidated = false;
1089 }
1090 return mItemsInReadingOrder;
1091 }
Adam Cohen8dfcba42011-07-07 16:38:18 -07001092
1093 public void getLocationInDragLayer(int[] loc) {
1094 mLauncher.getDragLayer().getLocationInDragLayer(this, loc);
1095 }
Adam Cohenea0818d2011-09-30 20:06:58 -07001096
1097 public void onFocusChange(View v, boolean hasFocus) {
1098 if (v == mFolderName && hasFocus) {
1099 startEditingFolderName();
1100 }
1101 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001102}