blob: e8f1ac96ba6d309cf90a0b5de352fc4de6be704e [file] [log] [blame]
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Joe Onoratoa5902522009-07-30 13:37:37 -070017package com.android.launcher2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080018
Adam Cohendf2cc412011-04-27 16:56:57 -070019import android.animation.Animator;
20import android.animation.AnimatorListenerAdapter;
Adam Cohendf2cc412011-04-27 16:56:57 -070021import android.animation.ObjectAnimator;
22import android.animation.PropertyValuesHolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080023import android.content.Context;
Adam Cohen76fc0852011-06-17 13:26:23 -070024import android.content.res.Resources;
Winson Chung043f2af2012-03-01 16:09:54 -080025import android.graphics.PointF;
Romain Guyfb5411e2010-02-24 10:04:17 -080026import android.graphics.Rect;
Adam Cohenbadf71e2011-05-26 19:08:29 -070027import android.graphics.drawable.Drawable;
Adam Cohen7a14d0b2011-06-24 11:36:35 -070028import android.text.InputType;
Adam Cohene601a432011-07-26 21:51:30 -070029import android.text.Selection;
30import android.text.Spannable;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080031import android.util.AttributeSet;
Adam Cohen3bf84d32012-05-07 20:17:14 -070032import android.util.Log;
Adam Cohen76fc0852011-06-17 13:26:23 -070033import android.view.ActionMode;
34import android.view.KeyEvent;
Adam Cohendf2cc412011-04-27 16:56:57 -070035import android.view.LayoutInflater;
Adam Cohen76fc0852011-06-17 13:26:23 -070036import android.view.Menu;
37import android.view.MenuItem;
Adam Cohen0c872ba2011-05-05 10:34:16 -070038import android.view.MotionEvent;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080039import android.view.View;
Adam Cohen3371da02011-10-25 21:38:29 -070040import android.view.accessibility.AccessibilityEvent;
41import android.view.accessibility.AccessibilityManager;
Adam Cohen76fc0852011-06-17 13:26:23 -070042import android.view.inputmethod.EditorInfo;
43import android.view.inputmethod.InputMethodManager;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080044import android.widget.LinearLayout;
Adam Cohendf2cc412011-04-27 16:56:57 -070045import android.widget.TextView;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080046
Romain Guyedcce092010-03-04 13:03:17 -080047import com.android.launcher.R;
Adam Cohena9cf38f2011-05-02 15:36:58 -070048import com.android.launcher2.FolderInfo.FolderListener;
Romain Guyedcce092010-03-04 13:03:17 -080049
Adam Cohenc0dcf592011-06-01 15:30:43 -070050import java.util.ArrayList;
Adam Cohen3bf84d32012-05-07 20:17:14 -070051import java.util.Collections;
52import java.util.Comparator;
Adam Cohenc0dcf592011-06-01 15:30:43 -070053
The Android Open Source Project31dd5032009-03-03 19:32:27 -080054/**
55 * Represents a set of icons chosen by the user or generated by the system.
56 */
Adam Cohen8dfcba42011-07-07 16:38:18 -070057public class Folder extends LinearLayout implements DragSource, View.OnClickListener,
Adam Cohenea0818d2011-09-30 20:06:58 -070058 View.OnLongClickListener, DropTarget, FolderListener, TextView.OnEditorActionListener,
59 View.OnFocusChangeListener {
The Android Open Source Project31dd5032009-03-03 19:32:27 -080060
Michael Jurka3a9fced2012-04-13 14:44:29 -070061 @SuppressWarnings("unused")
Adam Cohendf2cc412011-04-27 16:56:57 -070062 private static final String TAG = "Launcher.Folder";
63
Adam Cohen4eac29a2011-07-11 17:53:37 -070064 protected DragController mDragController;
65 protected Launcher mLauncher;
66 protected FolderInfo mInfo;
67
Adam Cohendf2cc412011-04-27 16:56:57 -070068 static final int STATE_NONE = -1;
69 static final int STATE_SMALL = 0;
70 static final int STATE_ANIMATING = 1;
71 static final int STATE_OPEN = 2;
72
73 private int mExpandDuration;
74 protected CellLayout mContent;
75 private final LayoutInflater mInflater;
76 private final IconCache mIconCache;
77 private int mState = STATE_NONE;
Adam Cohenbfbfd262011-06-13 16:55:12 -070078 private static final int REORDER_ANIMATION_DURATION = 230;
79 private static final int ON_EXIT_CLOSE_DELAY = 800;
Adam Cohen2801caf2011-05-13 20:57:39 -070080 private boolean mRearrangeOnClose = false;
81 private FolderIcon mFolderIcon;
82 private int mMaxCountX;
83 private int mMaxCountY;
Adam Cohen78dc83e2011-11-15 17:10:00 -080084 private int mMaxNumItems;
Adam Cohen7c693212011-05-18 15:26:57 -070085 private ArrayList<View> mItemsInReadingOrder = new ArrayList<View>();
Adam Cohenbadf71e2011-05-26 19:08:29 -070086 private Drawable mIconDrawable;
Adam Cohen7c693212011-05-18 15:26:57 -070087 boolean mItemsInvalidated = false;
Adam Cohenbfbfd262011-06-13 16:55:12 -070088 private ShortcutInfo mCurrentDragInfo;
89 private View mCurrentDragView;
90 boolean mSuppressOnAdd = false;
91 private int[] mTargetCell = new int[2];
92 private int[] mPreviousTargetCell = new int[2];
93 private int[] mEmptyCell = new int[2];
94 private Alarm mReorderAlarm = new Alarm();
95 private Alarm mOnExitAlarm = new Alarm();
Adam Cohen76fc0852011-06-17 13:26:23 -070096 private int mFolderNameHeight;
Adam Cohen8e776a62011-06-28 18:10:06 -070097 private Rect mTempRect = new Rect();
Adam Cohen67bd9cc2011-07-29 14:07:04 -070098 private boolean mDragInProgress = false;
99 private boolean mDeleteFolderOnDropCompleted = false;
100 private boolean mSuppressFolderDeletion = false;
Adam Cohen05e0f402011-08-01 12:12:49 -0700101 private boolean mItemAddedBackToSelfViaIcon = false;
Adam Cohenac56cff2011-09-28 20:45:37 -0700102 FolderEditText mFolderName;
Adam Cohen228da5a2011-07-27 22:23:47 -0700103
Adam Cohen76fc0852011-06-17 13:26:23 -0700104 private boolean mIsEditingName = false;
105 private InputMethodManager mInputMethodManager;
Adam Cohendf2cc412011-04-27 16:56:57 -0700106
Adam Cohena65beee2011-06-27 21:32:23 -0700107 private static String sDefaultFolderName;
108 private static String sHintText;
109
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800110 /**
111 * Used to inflate the Workspace from XML.
112 *
113 * @param context The application's context.
114 * @param attrs The attribtues set containing the Workspace's customization values.
115 */
116 public Folder(Context context, AttributeSet attrs) {
117 super(context, attrs);
118 setAlwaysDrawnWithCacheEnabled(false);
Adam Cohendf2cc412011-04-27 16:56:57 -0700119 mInflater = LayoutInflater.from(context);
120 mIconCache = ((LauncherApplication)context.getApplicationContext()).getIconCache();
Adam Cohen78dc83e2011-11-15 17:10:00 -0800121
122 Resources res = getResources();
123 mMaxCountX = res.getInteger(R.integer.folder_max_count_x);
124 mMaxCountY = res.getInteger(R.integer.folder_max_count_y);
125 mMaxNumItems = res.getInteger(R.integer.folder_max_num_items);
126 if (mMaxCountX < 0 || mMaxCountY < 0 || mMaxNumItems < 0) {
127 mMaxCountX = LauncherModel.getCellCountX();
128 mMaxCountY = LauncherModel.getCellCountY();
129 mMaxNumItems = mMaxCountX * mMaxCountY;
130 }
Adam Cohen76fc0852011-06-17 13:26:23 -0700131
132 mInputMethodManager = (InputMethodManager)
Michael Jurka8b805b12012-04-18 14:23:14 -0700133 getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
Adam Cohen76fc0852011-06-17 13:26:23 -0700134
Adam Cohen76fc0852011-06-17 13:26:23 -0700135 mExpandDuration = res.getInteger(R.integer.config_folderAnimDuration);
Adam Cohen4ef610f2011-06-21 22:37:36 -0700136
137 if (sDefaultFolderName == null) {
138 sDefaultFolderName = res.getString(R.string.folder_name);
139 }
Adam Cohena65beee2011-06-27 21:32:23 -0700140 if (sHintText == null) {
141 sHintText = res.getString(R.string.folder_hint_text);
142 }
Adam Cohen4eac29a2011-07-11 17:53:37 -0700143 mLauncher = (Launcher) context;
Adam Cohenac56cff2011-09-28 20:45:37 -0700144 // We need this view to be focusable in touch mode so that when text editing of the folder
145 // name is complete, we have something to focus on, thus hiding the cursor and giving
146 // reliable behvior when clicking the text field (since it will always gain focus on click).
147 setFocusableInTouchMode(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800148 }
149
150 @Override
151 protected void onFinishInflate() {
152 super.onFinishInflate();
Adam Cohendf2cc412011-04-27 16:56:57 -0700153 mContent = (CellLayout) findViewById(R.id.folder_content);
Adam Cohen2801caf2011-05-13 20:57:39 -0700154 mContent.setGridSize(0, 0);
Michael Jurkaa52570f2012-03-20 03:18:20 -0700155 mContent.getShortcutsAndWidgets().setMotionEventSplittingEnabled(false);
Adam Cohenac56cff2011-09-28 20:45:37 -0700156 mFolderName = (FolderEditText) findViewById(R.id.folder_name);
157 mFolderName.setFolder(this);
Adam Cohenea0818d2011-09-30 20:06:58 -0700158 mFolderName.setOnFocusChangeListener(this);
Adam Cohen76fc0852011-06-17 13:26:23 -0700159
160 // We find out how tall the text view wants to be (it is set to wrap_content), so that
161 // we can allocate the appropriate amount of space for it.
162 int measureSpec = MeasureSpec.UNSPECIFIED;
163 mFolderName.measure(measureSpec, measureSpec);
164 mFolderNameHeight = mFolderName.getMeasuredHeight();
165
166 // We disable action mode for now since it messes up the view on phones
167 mFolderName.setCustomSelectionActionModeCallback(mActionModeCallback);
Adam Cohen76fc0852011-06-17 13:26:23 -0700168 mFolderName.setOnEditorActionListener(this);
Adam Cohen4ef610f2011-06-21 22:37:36 -0700169 mFolderName.setSelectAllOnFocus(true);
Adam Cohen7a14d0b2011-06-24 11:36:35 -0700170 mFolderName.setInputType(mFolderName.getInputType() |
171 InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS | InputType.TYPE_TEXT_FLAG_CAP_WORDS);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800172 }
Adam Cohen2801caf2011-05-13 20:57:39 -0700173
Adam Cohen76fc0852011-06-17 13:26:23 -0700174 private ActionMode.Callback mActionModeCallback = new ActionMode.Callback() {
175 public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
176 return false;
177 }
178
179 public boolean onCreateActionMode(ActionMode mode, Menu menu) {
180 return false;
181 }
182
183 public void onDestroyActionMode(ActionMode mode) {
184 }
185
186 public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
187 return false;
188 }
189 };
190
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800191 public void onClick(View v) {
Adam Cohendf2cc412011-04-27 16:56:57 -0700192 Object tag = v.getTag();
193 if (tag instanceof ShortcutInfo) {
194 // refactor this code from Folder
195 ShortcutInfo item = (ShortcutInfo) tag;
196 int[] pos = new int[2];
197 v.getLocationOnScreen(pos);
198 item.intent.setSourceBounds(new Rect(pos[0], pos[1],
199 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Winson Chungc7450e32012-04-17 17:34:08 -0700200
201 mLauncher.startActivitySafely(v, item.intent, item);
Adam Cohendf2cc412011-04-27 16:56:57 -0700202 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800203 }
204
205 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -0700206 // Return if global dragging is not enabled
207 if (!mLauncher.isDraggingEnabled()) return true;
208
Adam Cohendf2cc412011-04-27 16:56:57 -0700209 Object tag = v.getTag();
210 if (tag instanceof ShortcutInfo) {
Adam Cohendf2cc412011-04-27 16:56:57 -0700211 ShortcutInfo item = (ShortcutInfo) tag;
212 if (!v.isInTouchMode()) {
213 return false;
214 }
215
Winson Chung7d7541e2011-09-16 20:14:36 -0700216 mLauncher.dismissFolderCling(null);
217
Adam Cohendf2cc412011-04-27 16:56:57 -0700218 mLauncher.getWorkspace().onDragStartedWithItem(v);
Adam Cohenac8c8762011-07-13 11:15:27 -0700219 mLauncher.getWorkspace().beginDragShared(v, this);
Adam Cohenbadf71e2011-05-26 19:08:29 -0700220 mIconDrawable = ((TextView) v).getCompoundDrawables()[1];
Adam Cohen76078c42011-06-09 15:06:52 -0700221
222 mCurrentDragInfo = item;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700223 mEmptyCell[0] = item.cellX;
224 mEmptyCell[1] = item.cellY;
225 mCurrentDragView = v;
Adam Cohenfc53cd22011-07-20 15:45:11 -0700226
227 mContent.removeView(mCurrentDragView);
228 mInfo.remove(mCurrentDragInfo);
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700229 mDragInProgress = true;
Adam Cohen05e0f402011-08-01 12:12:49 -0700230 mItemAddedBackToSelfViaIcon = false;
Adam Cohendf2cc412011-04-27 16:56:57 -0700231 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800232 return true;
233 }
234
Adam Cohen76fc0852011-06-17 13:26:23 -0700235 public boolean isEditingName() {
236 return mIsEditingName;
237 }
238
239 public void startEditingFolderName() {
Adam Cohena65beee2011-06-27 21:32:23 -0700240 mFolderName.setHint("");
Adam Cohen76fc0852011-06-17 13:26:23 -0700241 mIsEditingName = true;
242 }
243
244 public void dismissEditingName() {
245 mInputMethodManager.hideSoftInputFromWindow(getWindowToken(), 0);
246 doneEditingFolderName(true);
247 }
248
249 public void doneEditingFolderName(boolean commit) {
Adam Cohena65beee2011-06-27 21:32:23 -0700250 mFolderName.setHint(sHintText);
Adam Cohen1df26a32011-08-12 16:15:23 -0700251 // Convert to a string here to ensure that no other state associated with the text field
252 // gets saved.
Adam Cohen3371da02011-10-25 21:38:29 -0700253 String newTitle = mFolderName.getText().toString();
254 mInfo.setTitle(newTitle);
Adam Cohen76fc0852011-06-17 13:26:23 -0700255 LauncherModel.updateItemInDatabase(mLauncher, mInfo);
Adam Cohenac56cff2011-09-28 20:45:37 -0700256
Adam Cohen3371da02011-10-25 21:38:29 -0700257 if (commit) {
258 sendCustomAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED,
Michael Jurka8b805b12012-04-18 14:23:14 -0700259 String.format(getContext().getString(R.string.folder_renamed), newTitle));
Adam Cohen3371da02011-10-25 21:38:29 -0700260 }
Adam Cohenac56cff2011-09-28 20:45:37 -0700261 // In order to clear the focus from the text field, we set the focus on ourself. This
262 // ensures that every time the field is clicked, focus is gained, giving reliable behavior.
263 requestFocus();
264
Adam Cohene601a432011-07-26 21:51:30 -0700265 Selection.setSelection((Spannable) mFolderName.getText(), 0, 0);
Adam Cohen76fc0852011-06-17 13:26:23 -0700266 mIsEditingName = false;
267 }
268
269 public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
270 if (actionId == EditorInfo.IME_ACTION_DONE) {
271 dismissEditingName();
272 return true;
273 }
274 return false;
275 }
276
277 public View getEditTextRegion() {
278 return mFolderName;
279 }
280
Adam Cohenbadf71e2011-05-26 19:08:29 -0700281 public Drawable getDragDrawable() {
282 return mIconDrawable;
283 }
284
Adam Cohen0c872ba2011-05-05 10:34:16 -0700285 /**
286 * We need to handle touch events to prevent them from falling through to the workspace below.
287 */
288 @Override
289 public boolean onTouchEvent(MotionEvent ev) {
290 return true;
291 }
292
Joe Onorato00acb122009-08-04 16:04:30 -0400293 public void setDragController(DragController dragController) {
294 mDragController = dragController;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800295 }
296
Adam Cohen2801caf2011-05-13 20:57:39 -0700297 void setFolderIcon(FolderIcon icon) {
298 mFolderIcon = icon;
299 }
300
Adam Cohen3371da02011-10-25 21:38:29 -0700301 @Override
302 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
303 // When the folder gets focus, we don't want to announce the list of items.
304 return true;
305 }
306
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800307 /**
308 * @return the FolderInfo object associated with this folder
309 */
310 FolderInfo getInfo() {
311 return mInfo;
312 }
313
Adam Cohen3bf84d32012-05-07 20:17:14 -0700314 private class GridComparator implements Comparator<ShortcutInfo> {
315 int mNumCols;
316 public GridComparator(int numCols) {
317 mNumCols = numCols;
318 }
319
320 @Override
321 public int compare(ShortcutInfo lhs, ShortcutInfo rhs) {
322 int lhIndex = lhs.cellY * mNumCols + lhs.cellX;
323 int rhIndex = rhs.cellY * mNumCols + rhs.cellX;
324 return (lhIndex - rhIndex);
325 }
326
327 }
328
329 private void placeInReadingOrder(ArrayList<ShortcutInfo> items) {
330 int maxX = 0;
331 int count = items.size();
332 for (int i = 0; i < count; i++) {
333 ShortcutInfo item = items.get(i);
334 if (item.cellX > maxX) {
335 maxX = item.cellX;
336 }
337 }
338 GridComparator gridComparator = new GridComparator(maxX);
339 Collections.sort(items, gridComparator);
340 final int countX = mContent.getCountX();
341 for (int i = 0; i < count; i++) {
342 int x = i % countX;
343 int y = i / countX;
344 ShortcutInfo item = items.get(i);
345 item.cellX = x;
346 item.cellY = y;
347 }
348 }
349
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800350 void bind(FolderInfo info) {
351 mInfo = info;
Adam Cohendf2cc412011-04-27 16:56:57 -0700352 ArrayList<ShortcutInfo> children = info.contents;
Adam Cohenc508b2d2011-06-28 14:41:44 -0700353 ArrayList<ShortcutInfo> overflow = new ArrayList<ShortcutInfo>();
Adam Cohen7c693212011-05-18 15:26:57 -0700354 setupContentForNumItems(children.size());
Adam Cohen3bf84d32012-05-07 20:17:14 -0700355 placeInReadingOrder(children);
Adam Cohen0057bbc2011-08-12 18:30:51 -0700356 int count = 0;
Adam Cohendf2cc412011-04-27 16:56:57 -0700357 for (int i = 0; i < children.size(); i++) {
358 ShortcutInfo child = (ShortcutInfo) children.get(i);
Adam Cohenc508b2d2011-06-28 14:41:44 -0700359 if (!createAndAddShortcut(child)) {
360 overflow.add(child);
Adam Cohen0057bbc2011-08-12 18:30:51 -0700361 } else {
362 count++;
Adam Cohenc508b2d2011-06-28 14:41:44 -0700363 }
Adam Cohendf2cc412011-04-27 16:56:57 -0700364 }
Adam Cohenc508b2d2011-06-28 14:41:44 -0700365
Adam Cohen0057bbc2011-08-12 18:30:51 -0700366 // We rearrange the items in case there are any empty gaps
367 setupContentForNumItems(count);
368
Adam Cohenc508b2d2011-06-28 14:41:44 -0700369 // If our folder has too many items we prune them from the list. This is an issue
370 // when upgrading from the old Folders implementation which could contain an unlimited
371 // number of items.
372 for (ShortcutInfo item: overflow) {
373 mInfo.remove(item);
374 LauncherModel.deleteItemFromDatabase(mLauncher, item);
375 }
376
Adam Cohen4dbe6d92011-05-18 17:14:15 -0700377 mItemsInvalidated = true;
Adam Cohenac56cff2011-09-28 20:45:37 -0700378 updateTextViewFocus();
Adam Cohena9cf38f2011-05-02 15:36:58 -0700379 mInfo.addListener(this);
Adam Cohen4ef610f2011-06-21 22:37:36 -0700380
Adam Cohenafb01ee2011-06-23 15:38:03 -0700381 if (!sDefaultFolderName.contentEquals(mInfo.title)) {
Adam Cohen4ef610f2011-06-21 22:37:36 -0700382 mFolderName.setText(mInfo.title);
383 } else {
384 mFolderName.setText("");
385 }
Adam Cohendf2cc412011-04-27 16:56:57 -0700386 }
387
388 /**
389 * Creates a new UserFolder, inflated from R.layout.user_folder.
390 *
391 * @param context The application's context.
392 *
393 * @return A new UserFolder.
394 */
395 static Folder fromXml(Context context) {
396 return (Folder) LayoutInflater.from(context).inflate(R.layout.user_folder, null);
397 }
398
399 /**
400 * This method is intended to make the UserFolder to be visually identical in size and position
401 * to its associated FolderIcon. This allows for a seamless transition into the expanded state.
402 */
403 private void positionAndSizeAsIcon() {
Adam Cohen3e8f8112011-07-02 18:03:00 -0700404 if (!(getParent() instanceof DragLayer)) return;
Adam Cohen662b5982011-12-13 17:45:21 -0800405 setScaleX(0.8f);
406 setScaleY(0.8f);
407 setAlpha(0f);
Adam Cohendf2cc412011-04-27 16:56:57 -0700408 mState = STATE_SMALL;
409 }
410
411 public void animateOpen() {
Adam Cohen3e8f8112011-07-02 18:03:00 -0700412 positionAndSizeAsIcon();
413
Adam Cohen8e776a62011-06-28 18:10:06 -0700414 if (!(getParent() instanceof DragLayer)) return;
Adam Cohen2801caf2011-05-13 20:57:39 -0700415 centerAboutIcon();
Adam Cohen662b5982011-12-13 17:45:21 -0800416 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1);
417 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.0f);
418 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.0f);
419 ObjectAnimator oa = ObjectAnimator.ofPropertyValuesHolder(this, alpha, scaleX, scaleY);
Adam Cohen6441de02011-12-14 14:25:32 -0800420
Adam Cohen2801caf2011-05-13 20:57:39 -0700421 oa.addListener(new AnimatorListenerAdapter() {
Adam Cohendf2cc412011-04-27 16:56:57 -0700422 @Override
423 public void onAnimationStart(Animator animation) {
Adam Cohen3371da02011-10-25 21:38:29 -0700424 sendCustomAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED,
Michael Jurka8b805b12012-04-18 14:23:14 -0700425 String.format(getContext().getString(R.string.folder_opened),
Adam Cohen3371da02011-10-25 21:38:29 -0700426 mContent.getCountX(), mContent.getCountY()));
Adam Cohendf2cc412011-04-27 16:56:57 -0700427 mState = STATE_ANIMATING;
428 }
429 @Override
430 public void onAnimationEnd(Animator animation) {
Adam Cohen2801caf2011-05-13 20:57:39 -0700431 mState = STATE_OPEN;
Winson Chung7a74ac92011-09-20 17:43:51 -0700432
433 Cling cling = mLauncher.showFirstRunFoldersCling();
434 if (cling != null) {
435 cling.bringToFront();
436 }
Adam Cohenac56cff2011-09-28 20:45:37 -0700437 setFocusOnFirstChild();
Adam Cohendf2cc412011-04-27 16:56:57 -0700438 }
439 });
Adam Cohen2801caf2011-05-13 20:57:39 -0700440 oa.setDuration(mExpandDuration);
441 oa.start();
Adam Cohendf2cc412011-04-27 16:56:57 -0700442 }
443
Adam Cohen3371da02011-10-25 21:38:29 -0700444 private void sendCustomAccessibilityEvent(int type, String text) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700445 AccessibilityManager accessibilityManager = (AccessibilityManager)
446 getContext().getSystemService(Context.ACCESSIBILITY_SERVICE);
447 if (accessibilityManager.isEnabled()) {
Adam Cohen3371da02011-10-25 21:38:29 -0700448 AccessibilityEvent event = AccessibilityEvent.obtain(type);
449 onInitializeAccessibilityEvent(event);
450 event.getText().add(text);
Michael Jurka8b805b12012-04-18 14:23:14 -0700451 accessibilityManager.sendAccessibilityEvent(event);
Adam Cohen3371da02011-10-25 21:38:29 -0700452 }
453 }
454
Adam Cohenac56cff2011-09-28 20:45:37 -0700455 private void setFocusOnFirstChild() {
456 View firstChild = mContent.getChildAt(0, 0);
457 if (firstChild != null) {
458 firstChild.requestFocus();
459 }
460 }
461
Adam Cohendf2cc412011-04-27 16:56:57 -0700462 public void animateClosed() {
Adam Cohen8e776a62011-06-28 18:10:06 -0700463 if (!(getParent() instanceof DragLayer)) return;
Adam Cohen662b5982011-12-13 17:45:21 -0800464 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0);
465 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 0.9f);
466 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 0.9f);
467 ObjectAnimator oa = ObjectAnimator.ofPropertyValuesHolder(this, alpha, scaleX, scaleY);
Adam Cohendf2cc412011-04-27 16:56:57 -0700468
Adam Cohen2801caf2011-05-13 20:57:39 -0700469 oa.addListener(new AnimatorListenerAdapter() {
Adam Cohendf2cc412011-04-27 16:56:57 -0700470 @Override
471 public void onAnimationEnd(Animator animation) {
Adam Cohen2801caf2011-05-13 20:57:39 -0700472 onCloseComplete();
Adam Cohen2801caf2011-05-13 20:57:39 -0700473 mState = STATE_SMALL;
Adam Cohendf2cc412011-04-27 16:56:57 -0700474 }
475 @Override
476 public void onAnimationStart(Animator animation) {
Adam Cohen3371da02011-10-25 21:38:29 -0700477 sendCustomAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED,
Michael Jurka8b805b12012-04-18 14:23:14 -0700478 getContext().getString(R.string.folder_closed));
Adam Cohendf2cc412011-04-27 16:56:57 -0700479 mState = STATE_ANIMATING;
480 }
481 });
Adam Cohen2801caf2011-05-13 20:57:39 -0700482 oa.setDuration(mExpandDuration);
483 oa.start();
Adam Cohendf2cc412011-04-27 16:56:57 -0700484 }
485
486 void notifyDataSetChanged() {
487 // recreate all the children if the data set changes under us. We may want to do this more
488 // intelligently (ie just removing the views that should no longer exist)
489 mContent.removeAllViewsInLayout();
490 bind(mInfo);
491 }
492
Adam Cohencb3382b2011-05-24 14:07:08 -0700493 public boolean acceptDrop(DragObject d) {
494 final ItemInfo item = (ItemInfo) d.dragInfo;
Adam Cohendf2cc412011-04-27 16:56:57 -0700495 final int itemType = item.itemType;
Adam Cohen2801caf2011-05-13 20:57:39 -0700496 return ((itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION ||
497 itemType == LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT) &&
498 !isFull());
Adam Cohendf2cc412011-04-27 16:56:57 -0700499 }
500
Adam Cohendf2cc412011-04-27 16:56:57 -0700501 protected boolean findAndSetEmptyCells(ShortcutInfo item) {
502 int[] emptyCell = new int[2];
503 if (mContent.findCellForSpan(emptyCell, item.spanX, item.spanY)) {
504 item.cellX = emptyCell[0];
505 item.cellY = emptyCell[1];
Adam Cohendf2cc412011-04-27 16:56:57 -0700506 return true;
507 } else {
508 return false;
509 }
510 }
511
Adam Cohenc508b2d2011-06-28 14:41:44 -0700512 protected boolean createAndAddShortcut(ShortcutInfo item) {
Adam Cohendf2cc412011-04-27 16:56:57 -0700513 final TextView textView =
Adam Cohene87b9242011-06-29 14:01:26 -0700514 (TextView) mInflater.inflate(R.layout.application, this, false);
Adam Cohendf2cc412011-04-27 16:56:57 -0700515 textView.setCompoundDrawablesWithIntrinsicBounds(null,
516 new FastBitmapDrawable(item.getIcon(mIconCache)), null, null);
517 textView.setText(item.title);
518 textView.setTag(item);
519
520 textView.setOnClickListener(this);
521 textView.setOnLongClickListener(this);
522
Adam Cohenc508b2d2011-06-28 14:41:44 -0700523 // We need to check here to verify that the given item's location isn't already occupied
Adam Cohen3bf84d32012-05-07 20:17:14 -0700524 // by another item.
Adam Cohen0057bbc2011-08-12 18:30:51 -0700525 if (mContent.getChildAt(item.cellX, item.cellY) != null || item.cellX < 0 || item.cellY < 0
526 || item.cellX >= mContent.getCountX() || item.cellY >= mContent.getCountY()) {
Adam Cohen3bf84d32012-05-07 20:17:14 -0700527 // This shouldn't happen, log it.
528 Log.e(TAG, "Folder order not properly persisted during bind");
Adam Cohenc508b2d2011-06-28 14:41:44 -0700529 if (!findAndSetEmptyCells(item)) {
530 return false;
531 }
532 }
533
Adam Cohendf2cc412011-04-27 16:56:57 -0700534 CellLayout.LayoutParams lp =
535 new CellLayout.LayoutParams(item.cellX, item.cellY, item.spanX, item.spanY);
536 boolean insert = false;
Adam Cohenac56cff2011-09-28 20:45:37 -0700537 textView.setOnKeyListener(new FolderKeyEventListener());
Adam Cohendf2cc412011-04-27 16:56:57 -0700538 mContent.addViewToCellLayout(textView, insert ? 0 : -1, (int)item.id, lp, true);
Adam Cohenc508b2d2011-06-28 14:41:44 -0700539 return true;
Adam Cohendf2cc412011-04-27 16:56:57 -0700540 }
541
Adam Cohencb3382b2011-05-24 14:07:08 -0700542 public void onDragEnter(DragObject d) {
Adam Cohenbfbfd262011-06-13 16:55:12 -0700543 mPreviousTargetCell[0] = -1;
544 mPreviousTargetCell[1] = -1;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700545 mOnExitAlarm.cancelAlarm();
546 }
547
548 OnAlarmListener mReorderAlarmListener = new OnAlarmListener() {
549 public void onAlarm(Alarm alarm) {
550 realTimeReorder(mEmptyCell, mTargetCell);
551 }
552 };
553
554 boolean readingOrderGreaterThan(int[] v1, int[] v2) {
555 if (v1[1] > v2[1] || (v1[1] == v2[1] && v1[0] > v2[0])) {
556 return true;
557 } else {
558 return false;
559 }
560 }
561
562 private void realTimeReorder(int[] empty, int[] target) {
563 boolean wrap;
564 int startX;
565 int endX;
566 int startY;
Adam Cohen76fc0852011-06-17 13:26:23 -0700567 int delay = 0;
568 float delayAmount = 30;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700569 if (readingOrderGreaterThan(target, empty)) {
570 wrap = empty[0] >= mContent.getCountX() - 1;
571 startY = wrap ? empty[1] + 1 : empty[1];
572 for (int y = startY; y <= target[1]; y++) {
573 startX = y == empty[1] ? empty[0] + 1 : 0;
574 endX = y < target[1] ? mContent.getCountX() - 1 : target[0];
575 for (int x = startX; x <= endX; x++) {
576 View v = mContent.getChildAt(x,y);
577 if (mContent.animateChildToPosition(v, empty[0], empty[1],
Adam Cohen482ed822012-03-02 14:15:13 -0800578 REORDER_ANIMATION_DURATION, delay, true, true)) {
Adam Cohenbfbfd262011-06-13 16:55:12 -0700579 empty[0] = x;
580 empty[1] = y;
Adam Cohen76fc0852011-06-17 13:26:23 -0700581 delay += delayAmount;
582 delayAmount *= 0.9;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700583 }
584 }
585 }
586 } else {
587 wrap = empty[0] == 0;
588 startY = wrap ? empty[1] - 1 : empty[1];
589 for (int y = startY; y >= target[1]; y--) {
590 startX = y == empty[1] ? empty[0] - 1 : mContent.getCountX() - 1;
591 endX = y > target[1] ? 0 : target[0];
592 for (int x = startX; x >= endX; x--) {
593 View v = mContent.getChildAt(x,y);
594 if (mContent.animateChildToPosition(v, empty[0], empty[1],
Adam Cohen482ed822012-03-02 14:15:13 -0800595 REORDER_ANIMATION_DURATION, delay, true, true)) {
Adam Cohenbfbfd262011-06-13 16:55:12 -0700596 empty[0] = x;
597 empty[1] = y;
Adam Cohen76fc0852011-06-17 13:26:23 -0700598 delay += delayAmount;
599 delayAmount *= 0.9;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700600 }
601 }
602 }
603 }
Adam Cohendf2cc412011-04-27 16:56:57 -0700604 }
605
Adam Cohencb3382b2011-05-24 14:07:08 -0700606 public void onDragOver(DragObject d) {
Adam Cohenbfbfd262011-06-13 16:55:12 -0700607 float[] r = getDragViewVisualCenter(d.x, d.y, d.xOffset, d.yOffset, d.dragView, null);
608 mTargetCell = mContent.findNearestArea((int) r[0], (int) r[1], 1, 1, mTargetCell);
609
610 if (mTargetCell[0] != mPreviousTargetCell[0] || mTargetCell[1] != mPreviousTargetCell[1]) {
611 mReorderAlarm.cancelAlarm();
612 mReorderAlarm.setOnAlarmListener(mReorderAlarmListener);
613 mReorderAlarm.setAlarm(150);
614 mPreviousTargetCell[0] = mTargetCell[0];
615 mPreviousTargetCell[1] = mTargetCell[1];
616 }
Adam Cohendf2cc412011-04-27 16:56:57 -0700617 }
618
Adam Cohenbfbfd262011-06-13 16:55:12 -0700619 // This is used to compute the visual center of the dragView. The idea is that
620 // the visual center represents the user's interpretation of where the item is, and hence
621 // is the appropriate point to use when determining drop location.
622 private float[] getDragViewVisualCenter(int x, int y, int xOffset, int yOffset,
623 DragView dragView, float[] recycle) {
624 float res[];
625 if (recycle == null) {
626 res = new float[2];
627 } else {
628 res = recycle;
629 }
630
631 // These represent the visual top and left of drag view if a dragRect was provided.
632 // If a dragRect was not provided, then they correspond to the actual view left and
633 // top, as the dragRect is in that case taken to be the entire dragView.
634 // R.dimen.dragViewOffsetY.
635 int left = x - xOffset;
636 int top = y - yOffset;
637
638 // In order to find the visual center, we shift by half the dragRect
639 res[0] = left + dragView.getDragRegion().width() / 2;
640 res[1] = top + dragView.getDragRegion().height() / 2;
641
642 return res;
643 }
644
645 OnAlarmListener mOnExitAlarmListener = new OnAlarmListener() {
646 public void onAlarm(Alarm alarm) {
Adam Cohen3e8f8112011-07-02 18:03:00 -0700647 completeDragExit();
Adam Cohenbfbfd262011-06-13 16:55:12 -0700648 }
649 };
650
Adam Cohen95bb8002011-07-03 23:40:28 -0700651 public void completeDragExit() {
Adam Cohen3e8f8112011-07-02 18:03:00 -0700652 mLauncher.closeFolder();
653 mCurrentDragInfo = null;
654 mCurrentDragView = null;
655 mSuppressOnAdd = false;
656 mRearrangeOnClose = true;
657 }
658
Adam Cohencb3382b2011-05-24 14:07:08 -0700659 public void onDragExit(DragObject d) {
Adam Cohenbfbfd262011-06-13 16:55:12 -0700660 // We only close the folder if this is a true drag exit, ie. not because a drop
661 // has occurred above the folder.
662 if (!d.dragComplete) {
663 mOnExitAlarm.setOnAlarmListener(mOnExitAlarmListener);
664 mOnExitAlarm.setAlarm(ON_EXIT_CLOSE_DELAY);
665 }
666 mReorderAlarm.cancelAlarm();
Adam Cohen2801caf2011-05-13 20:57:39 -0700667 }
668
Winson Chunga48487a2012-03-20 16:19:37 -0700669 public void onDropCompleted(View target, DragObject d, boolean isFlingToDelete,
670 boolean success) {
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700671 if (success) {
Adam Cohen05e0f402011-08-01 12:12:49 -0700672 if (mDeleteFolderOnDropCompleted && !mItemAddedBackToSelfViaIcon) {
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700673 replaceFolderWithFinalItem();
674 }
675 } else {
676 // The drag failed, we need to return the item to the folder
677 mFolderIcon.onDrop(d);
678
679 // We're going to trigger a "closeFolder" which may occur before this item has
680 // been added back to the folder -- this could cause the folder to be deleted
681 if (mOnExitAlarm.alarmPending()) {
682 mSuppressFolderDeletion = true;
683 }
684 }
685
686 if (target != this) {
687 if (mOnExitAlarm.alarmPending()) {
688 mOnExitAlarm.cancelAlarm();
689 completeDragExit();
690 }
691 }
692 mDeleteFolderOnDropCompleted = false;
693 mDragInProgress = false;
Adam Cohen05e0f402011-08-01 12:12:49 -0700694 mItemAddedBackToSelfViaIcon = false;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700695 mCurrentDragInfo = null;
696 mCurrentDragView = null;
697 mSuppressOnAdd = false;
Adam Cohen4045eb72011-10-06 11:44:26 -0700698
699 // Reordering may have occured, and we need to save the new item locations. We do this once
700 // at the end to prevent unnecessary database operations.
701 updateItemLocationsInDatabase();
702 }
703
Winson Chunga48487a2012-03-20 16:19:37 -0700704 @Override
Winson Chung043f2af2012-03-01 16:09:54 -0800705 public boolean supportsFlingToDelete() {
706 return true;
707 }
708
Winson Chunga48487a2012-03-20 16:19:37 -0700709 public void onFlingToDelete(DragObject d, int x, int y, PointF vec) {
710 // Do nothing
711 }
712
713 @Override
714 public void onFlingToDeleteCompleted() {
715 // Do nothing
716 }
717
Adam Cohen4045eb72011-10-06 11:44:26 -0700718 private void updateItemLocationsInDatabase() {
719 ArrayList<View> list = getItemsInReadingOrder();
720 for (int i = 0; i < list.size(); i++) {
721 View v = list.get(i);
722 ItemInfo info = (ItemInfo) v.getTag();
723 LauncherModel.moveItemInDatabase(mLauncher, info, mInfo.id, 0,
724 info.cellX, info.cellY);
725 }
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700726 }
Adam Cohen228da5a2011-07-27 22:23:47 -0700727
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700728 public void notifyDrop() {
729 if (mDragInProgress) {
Adam Cohen05e0f402011-08-01 12:12:49 -0700730 mItemAddedBackToSelfViaIcon = true;
Adam Cohen76078c42011-06-09 15:06:52 -0700731 }
Adam Cohendf2cc412011-04-27 16:56:57 -0700732 }
733
734 public boolean isDropEnabled() {
735 return true;
736 }
737
Adam Cohencb3382b2011-05-24 14:07:08 -0700738 public DropTarget getDropTargetDelegate(DragObject d) {
Adam Cohendf2cc412011-04-27 16:56:57 -0700739 return null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800740 }
Adam Cohena9cf38f2011-05-02 15:36:58 -0700741
Adam Cohen4045eb72011-10-06 11:44:26 -0700742 private void setupContentDimensions(int count) {
Adam Cohen2801caf2011-05-13 20:57:39 -0700743 ArrayList<View> list = getItemsInReadingOrder();
744
745 int countX = mContent.getCountX();
746 int countY = mContent.getCountY();
Adam Cohen7c693212011-05-18 15:26:57 -0700747 boolean done = false;
Adam Cohen2801caf2011-05-13 20:57:39 -0700748
Adam Cohen7c693212011-05-18 15:26:57 -0700749 while (!done) {
750 int oldCountX = countX;
751 int oldCountY = countY;
752 if (countX * countY < count) {
753 // Current grid is too small, expand it
Adam Cohen78dc83e2011-11-15 17:10:00 -0800754 if ((countX <= countY || countY == mMaxCountY) && countX < mMaxCountX) {
Adam Cohen7c693212011-05-18 15:26:57 -0700755 countX++;
756 } else if (countY < mMaxCountY) {
757 countY++;
758 }
759 if (countY == 0) countY++;
760 } else if ((countY - 1) * countX >= count && countY >= countX) {
761 countY = Math.max(0, countY - 1);
762 } else if ((countX - 1) * countY >= count) {
763 countX = Math.max(0, countX - 1);
Adam Cohen2801caf2011-05-13 20:57:39 -0700764 }
Adam Cohen7c693212011-05-18 15:26:57 -0700765 done = countX == oldCountX && countY == oldCountY;
Adam Cohen2801caf2011-05-13 20:57:39 -0700766 }
Adam Cohen7c693212011-05-18 15:26:57 -0700767 mContent.setGridSize(countX, countY);
Adam Cohen2801caf2011-05-13 20:57:39 -0700768 arrangeChildren(list);
769 }
770
771 public boolean isFull() {
Adam Cohen78dc83e2011-11-15 17:10:00 -0800772 return getItemCount() >= mMaxNumItems;
Adam Cohen2801caf2011-05-13 20:57:39 -0700773 }
774
775 private void centerAboutIcon() {
Adam Cohen8e776a62011-06-28 18:10:06 -0700776 DragLayer.LayoutParams lp = (DragLayer.LayoutParams) getLayoutParams();
Adam Cohen2801caf2011-05-13 20:57:39 -0700777
778 int width = getPaddingLeft() + getPaddingRight() + mContent.getDesiredWidth();
Adam Cohenf4bb1cd2011-07-22 14:36:03 -0700779 int height = getPaddingTop() + getPaddingBottom() + mContent.getDesiredHeight()
780 + mFolderNameHeight;
Adam Cohen8e776a62011-06-28 18:10:06 -0700781 DragLayer parent = (DragLayer) mLauncher.findViewById(R.id.drag_layer);
Adam Cohen2801caf2011-05-13 20:57:39 -0700782
Adam Cohen8e776a62011-06-28 18:10:06 -0700783 parent.getDescendantRectRelativeToSelf(mFolderIcon, mTempRect);
784
785 int centerX = mTempRect.centerX();
786 int centerY = mTempRect.centerY();
Adam Cohen2801caf2011-05-13 20:57:39 -0700787 int centeredLeft = centerX - width / 2;
788 int centeredTop = centerY - height / 2;
789
Adam Cohen7cc1bc42011-12-13 21:28:43 -0800790 int currentPage = mLauncher.getWorkspace().getCurrentPage();
791 // In case the workspace is scrolling, we need to use the final scroll to compute
792 // the folders bounds.
793 mLauncher.getWorkspace().setFinalScrollForPageChange(currentPage);
Adam Cohen35e7e642011-07-17 14:47:18 -0700794 // We first fetch the currently visible CellLayoutChildren
Adam Cohen7cc1bc42011-12-13 21:28:43 -0800795 CellLayout currentLayout = (CellLayout) mLauncher.getWorkspace().getChildAt(currentPage);
Michael Jurkaa52570f2012-03-20 03:18:20 -0700796 ShortcutAndWidgetContainer boundingLayout = currentLayout.getShortcutsAndWidgets();
Adam Cohen35e7e642011-07-17 14:47:18 -0700797 Rect bounds = new Rect();
798 parent.getDescendantRectRelativeToSelf(boundingLayout, bounds);
Adam Cohen7cc1bc42011-12-13 21:28:43 -0800799 // We reset the workspaces scroll
800 mLauncher.getWorkspace().resetFinalScrollForPageChange(currentPage);
Adam Cohen2801caf2011-05-13 20:57:39 -0700801
Adam Cohen35e7e642011-07-17 14:47:18 -0700802 // We need to bound the folder to the currently visible CellLayoutChildren
803 int left = Math.min(Math.max(bounds.left, centeredLeft),
804 bounds.left + bounds.width() - width);
805 int top = Math.min(Math.max(bounds.top, centeredTop),
806 bounds.top + bounds.height() - height);
807 // If the folder doesn't fit within the bounds, center it about the desired bounds
808 if (width >= bounds.width()) {
809 left = bounds.left + (bounds.width() - width) / 2;
Adam Cohen0e4857c2011-06-30 17:05:14 -0700810 }
Adam Cohen35e7e642011-07-17 14:47:18 -0700811 if (height >= bounds.height()) {
812 top = bounds.top + (bounds.height() - height) / 2;
Adam Cohen0e4857c2011-06-30 17:05:14 -0700813 }
Adam Cohen2801caf2011-05-13 20:57:39 -0700814
815 int folderPivotX = width / 2 + (centeredLeft - left);
816 int folderPivotY = height / 2 + (centeredTop - top);
817 setPivotX(folderPivotX);
818 setPivotY(folderPivotY);
819 int folderIconPivotX = (int) (mFolderIcon.getMeasuredWidth() *
820 (1.0f * folderPivotX / width));
821 int folderIconPivotY = (int) (mFolderIcon.getMeasuredHeight() *
822 (1.0f * folderPivotY / height));
Adam Cohen3bf84d32012-05-07 20:17:14 -0700823
Adam Cohen2801caf2011-05-13 20:57:39 -0700824 mFolderIcon.setPivotX(folderIconPivotX);
825 mFolderIcon.setPivotY(folderIconPivotY);
826
Adam Cohen662b5982011-12-13 17:45:21 -0800827 lp.width = width;
828 lp.height = height;
829 lp.x = left;
830 lp.y = top;
Adam Cohen2801caf2011-05-13 20:57:39 -0700831 }
832
833 private void setupContentForNumItems(int count) {
Adam Cohen4045eb72011-10-06 11:44:26 -0700834 setupContentDimensions(count);
Adam Cohen2801caf2011-05-13 20:57:39 -0700835
Adam Cohen8e776a62011-06-28 18:10:06 -0700836 DragLayer.LayoutParams lp = (DragLayer.LayoutParams) getLayoutParams();
Adam Cohen2801caf2011-05-13 20:57:39 -0700837 if (lp == null) {
Adam Cohen8e776a62011-06-28 18:10:06 -0700838 lp = new DragLayer.LayoutParams(0, 0);
839 lp.customPosition = true;
Adam Cohen2801caf2011-05-13 20:57:39 -0700840 setLayoutParams(lp);
841 }
842 centerAboutIcon();
843 }
844
Adam Cohen234c4cd2011-07-17 21:03:04 -0700845 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
846 int width = getPaddingLeft() + getPaddingRight() + mContent.getDesiredWidth();
Adam Cohenf4bb1cd2011-07-22 14:36:03 -0700847 int height = getPaddingTop() + getPaddingBottom() + mContent.getDesiredHeight()
848 + mFolderNameHeight;
Adam Cohen234c4cd2011-07-17 21:03:04 -0700849
850 int contentWidthSpec = MeasureSpec.makeMeasureSpec(mContent.getDesiredWidth(),
851 MeasureSpec.EXACTLY);
852 int contentHeightSpec = MeasureSpec.makeMeasureSpec(mContent.getDesiredHeight(),
853 MeasureSpec.EXACTLY);
854 mContent.measure(contentWidthSpec, contentHeightSpec);
855
856 mFolderName.measure(contentWidthSpec,
857 MeasureSpec.makeMeasureSpec(mFolderNameHeight, MeasureSpec.EXACTLY));
858 setMeasuredDimension(width, height);
859 }
860
Adam Cohen2801caf2011-05-13 20:57:39 -0700861 private void arrangeChildren(ArrayList<View> list) {
862 int[] vacant = new int[2];
863 if (list == null) {
864 list = getItemsInReadingOrder();
865 }
866 mContent.removeAllViews();
867
868 for (int i = 0; i < list.size(); i++) {
869 View v = list.get(i);
870 mContent.getVacantCell(vacant, 1, 1);
871 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) v.getLayoutParams();
872 lp.cellX = vacant[0];
873 lp.cellY = vacant[1];
874 ItemInfo info = (ItemInfo) v.getTag();
Adam Cohen2792a332011-09-26 21:09:47 -0700875 if (info.cellX != vacant[0] || info.cellY != vacant[1]) {
876 info.cellX = vacant[0];
877 info.cellY = vacant[1];
878 LauncherModel.addOrMoveItemInDatabase(mLauncher, info, mInfo.id, 0,
879 info.cellX, info.cellY);
880 }
Adam Cohen2801caf2011-05-13 20:57:39 -0700881 boolean insert = false;
882 mContent.addViewToCellLayout(v, insert ? 0 : -1, (int)info.id, lp, true);
883 }
Adam Cohen7c693212011-05-18 15:26:57 -0700884 mItemsInvalidated = true;
Adam Cohen2801caf2011-05-13 20:57:39 -0700885 }
886
Adam Cohena9cf38f2011-05-02 15:36:58 -0700887 public int getItemCount() {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700888 return mContent.getShortcutsAndWidgets().getChildCount();
Adam Cohena9cf38f2011-05-02 15:36:58 -0700889 }
890
891 public View getItemAt(int index) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700892 return mContent.getShortcutsAndWidgets().getChildAt(index);
Adam Cohena9cf38f2011-05-02 15:36:58 -0700893 }
894
Adam Cohen2801caf2011-05-13 20:57:39 -0700895 private void onCloseComplete() {
Adam Cohen05e0f402011-08-01 12:12:49 -0700896 DragLayer parent = (DragLayer) getParent();
Adam Cohen4554ee12011-08-03 16:13:21 -0700897 parent.removeView(this);
898 mDragController.removeDropTarget((DropTarget) this);
Adam Cohen05e0f402011-08-01 12:12:49 -0700899 clearFocus();
Adam Cohenac56cff2011-09-28 20:45:37 -0700900 mFolderIcon.requestFocus();
Adam Cohen05e0f402011-08-01 12:12:49 -0700901
Adam Cohen2801caf2011-05-13 20:57:39 -0700902 if (mRearrangeOnClose) {
903 setupContentForNumItems(getItemCount());
904 mRearrangeOnClose = false;
905 }
Adam Cohenafb01ee2011-06-23 15:38:03 -0700906 if (getItemCount() <= 1) {
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700907 if (!mDragInProgress && !mSuppressFolderDeletion) {
908 replaceFolderWithFinalItem();
909 } else if (mDragInProgress) {
910 mDeleteFolderOnDropCompleted = true;
911 }
Adam Cohenafb01ee2011-06-23 15:38:03 -0700912 }
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700913 mSuppressFolderDeletion = false;
Adam Cohenafb01ee2011-06-23 15:38:03 -0700914 }
915
916 private void replaceFolderWithFinalItem() {
917 ItemInfo finalItem = null;
918
919 if (getItemCount() == 1) {
920 finalItem = mInfo.contents.get(0);
921 }
922
923 // Remove the folder completely
Winson Chung3d503fb2011-07-13 17:25:49 -0700924 CellLayout cellLayout = mLauncher.getCellLayout(mInfo.container, mInfo.screen);
Adam Cohenafb01ee2011-06-23 15:38:03 -0700925 cellLayout.removeView(mFolderIcon);
926 if (mFolderIcon instanceof DropTarget) {
927 mDragController.removeDropTarget((DropTarget) mFolderIcon);
928 }
929 mLauncher.removeFolder(mInfo);
930
931 if (finalItem != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -0700932 LauncherModel.addOrMoveItemInDatabase(mLauncher, finalItem, mInfo.container,
933 mInfo.screen, mInfo.cellX, mInfo.cellY);
Adam Cohenafb01ee2011-06-23 15:38:03 -0700934 }
Adam Cohen716b51e2011-06-30 12:09:54 -0700935 LauncherModel.deleteItemFromDatabase(mLauncher, mInfo);
Adam Cohenafb01ee2011-06-23 15:38:03 -0700936
937 // Add the last remaining child to the workspace in place of the folder
938 if (finalItem != null) {
939 View child = mLauncher.createShortcut(R.layout.application, cellLayout,
940 (ShortcutInfo) finalItem);
941
Winson Chung3d503fb2011-07-13 17:25:49 -0700942 mLauncher.getWorkspace().addInScreen(child, mInfo.container, mInfo.screen, mInfo.cellX,
943 mInfo.cellY, mInfo.spanX, mInfo.spanY);
Adam Cohenafb01ee2011-06-23 15:38:03 -0700944 }
Adam Cohen2801caf2011-05-13 20:57:39 -0700945 }
946
Adam Cohenac56cff2011-09-28 20:45:37 -0700947 // This method keeps track of the last item in the folder for the purposes
948 // of keyboard focus
949 private void updateTextViewFocus() {
950 View lastChild = getItemAt(getItemCount() - 1);
951 getItemAt(getItemCount() - 1);
952 if (lastChild != null) {
953 mFolderName.setNextFocusDownId(lastChild.getId());
954 mFolderName.setNextFocusRightId(lastChild.getId());
955 mFolderName.setNextFocusLeftId(lastChild.getId());
956 mFolderName.setNextFocusUpId(lastChild.getId());
957 }
958 }
959
Adam Cohenbfbfd262011-06-13 16:55:12 -0700960 public void onDrop(DragObject d) {
961 ShortcutInfo item;
962 if (d.dragInfo instanceof ApplicationInfo) {
963 // Came from all apps -- make a copy
964 item = ((ApplicationInfo) d.dragInfo).makeShortcut();
965 item.spanX = 1;
966 item.spanY = 1;
967 } else {
968 item = (ShortcutInfo) d.dragInfo;
969 }
Adam Cohen05e0f402011-08-01 12:12:49 -0700970 // Dragged from self onto self, currently this is the only path possible, however
971 // we keep this as a distinct code path.
Adam Cohenbfbfd262011-06-13 16:55:12 -0700972 if (item == mCurrentDragInfo) {
973 ShortcutInfo si = (ShortcutInfo) mCurrentDragView.getTag();
974 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) mCurrentDragView.getLayoutParams();
975 si.cellX = lp.cellX = mEmptyCell[0];
976 si.cellX = lp.cellY = mEmptyCell[1];
977 mContent.addViewToCellLayout(mCurrentDragView, -1, (int)item.id, lp, true);
Adam Cohenfc53cd22011-07-20 15:45:11 -0700978 if (d.dragView.hasDrawn()) {
979 mLauncher.getDragLayer().animateViewIntoPosition(d.dragView, mCurrentDragView);
980 } else {
Winson Chung2c4cf412012-05-08 14:03:21 -0700981 d.deferDragViewCleanupPostAnimation = false;
Adam Cohenfc53cd22011-07-20 15:45:11 -0700982 mCurrentDragView.setVisibility(VISIBLE);
983 }
Adam Cohene9166b22011-07-08 17:11:11 -0700984 mItemsInvalidated = true;
Adam Cohen4045eb72011-10-06 11:44:26 -0700985 setupContentDimensions(getItemCount());
Adam Cohen716b51e2011-06-30 12:09:54 -0700986 mSuppressOnAdd = true;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700987 }
988 mInfo.add(item);
989 }
990
991 public void onAdd(ShortcutInfo item) {
992 mItemsInvalidated = true;
Adam Cohen05e0f402011-08-01 12:12:49 -0700993 // If the item was dropped onto this open folder, we have done the work associated
994 // with adding the item to the folder, as indicated by mSuppressOnAdd being set
Adam Cohenbfbfd262011-06-13 16:55:12 -0700995 if (mSuppressOnAdd) return;
996 if (!findAndSetEmptyCells(item)) {
997 // The current layout is full, can we expand it?
998 setupContentForNumItems(getItemCount() + 1);
999 findAndSetEmptyCells(item);
1000 }
1001 createAndAddShortcut(item);
1002 LauncherModel.addOrMoveItemInDatabase(
1003 mLauncher, item, mInfo.id, 0, item.cellX, item.cellY);
1004 }
1005
Adam Cohena9cf38f2011-05-02 15:36:58 -07001006 public void onRemove(ShortcutInfo item) {
Adam Cohen7c693212011-05-18 15:26:57 -07001007 mItemsInvalidated = true;
Adam Cohen05e0f402011-08-01 12:12:49 -07001008 // If this item is being dragged from this open folder, we have already handled
1009 // the work associated with removing the item, so we don't have to do anything here.
Adam Cohenbfbfd262011-06-13 16:55:12 -07001010 if (item == mCurrentDragInfo) return;
Adam Cohendf1e4e82011-06-24 15:57:39 -07001011 View v = getViewForInfo(item);
Adam Cohena9cf38f2011-05-02 15:36:58 -07001012 mContent.removeView(v);
Adam Cohen2801caf2011-05-13 20:57:39 -07001013 if (mState == STATE_ANIMATING) {
1014 mRearrangeOnClose = true;
1015 } else {
1016 setupContentForNumItems(getItemCount());
1017 }
Adam Cohenafb01ee2011-06-23 15:38:03 -07001018 if (getItemCount() <= 1) {
1019 replaceFolderWithFinalItem();
1020 }
Adam Cohena9cf38f2011-05-02 15:36:58 -07001021 }
Adam Cohen7c693212011-05-18 15:26:57 -07001022
Adam Cohendf1e4e82011-06-24 15:57:39 -07001023 private View getViewForInfo(ShortcutInfo item) {
1024 for (int j = 0; j < mContent.getCountY(); j++) {
1025 for (int i = 0; i < mContent.getCountX(); i++) {
1026 View v = mContent.getChildAt(i, j);
1027 if (v.getTag() == item) {
1028 return v;
1029 }
1030 }
1031 }
1032 return null;
1033 }
1034
Adam Cohen76078c42011-06-09 15:06:52 -07001035 public void onItemsChanged() {
Adam Cohenac56cff2011-09-28 20:45:37 -07001036 updateTextViewFocus();
Adam Cohen76078c42011-06-09 15:06:52 -07001037 }
Adam Cohenac56cff2011-09-28 20:45:37 -07001038
Adam Cohen76fc0852011-06-17 13:26:23 -07001039 public void onTitleChanged(CharSequence title) {
1040 }
Adam Cohen76078c42011-06-09 15:06:52 -07001041
Adam Cohen7c693212011-05-18 15:26:57 -07001042 public ArrayList<View> getItemsInReadingOrder() {
Adam Cohen76078c42011-06-09 15:06:52 -07001043 return getItemsInReadingOrder(true);
1044 }
1045
1046 public ArrayList<View> getItemsInReadingOrder(boolean includeCurrentDragItem) {
Adam Cohen7c693212011-05-18 15:26:57 -07001047 if (mItemsInvalidated) {
1048 mItemsInReadingOrder.clear();
1049 for (int j = 0; j < mContent.getCountY(); j++) {
1050 for (int i = 0; i < mContent.getCountX(); i++) {
1051 View v = mContent.getChildAt(i, j);
1052 if (v != null) {
Adam Cohen76078c42011-06-09 15:06:52 -07001053 ShortcutInfo info = (ShortcutInfo) v.getTag();
1054 if (info != mCurrentDragInfo || includeCurrentDragItem) {
1055 mItemsInReadingOrder.add(v);
1056 }
Adam Cohen7c693212011-05-18 15:26:57 -07001057 }
1058 }
1059 }
1060 mItemsInvalidated = false;
1061 }
1062 return mItemsInReadingOrder;
1063 }
Adam Cohen8dfcba42011-07-07 16:38:18 -07001064
1065 public void getLocationInDragLayer(int[] loc) {
1066 mLauncher.getDragLayer().getLocationInDragLayer(this, loc);
1067 }
Adam Cohenea0818d2011-09-30 20:06:58 -07001068
1069 public void onFocusChange(View v, boolean hasFocus) {
1070 if (v == mFolderName && hasFocus) {
1071 startEditingFolderName();
1072 }
1073 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001074}