blob: 8632370f27ca84fc73578fde9572c130b274707e [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) {
Adam Cohendf2cc412011-04-27 16:56:57 -0700206 Object tag = v.getTag();
207 if (tag instanceof ShortcutInfo) {
Adam Cohendf2cc412011-04-27 16:56:57 -0700208 ShortcutInfo item = (ShortcutInfo) tag;
209 if (!v.isInTouchMode()) {
210 return false;
211 }
212
Winson Chung7d7541e2011-09-16 20:14:36 -0700213 mLauncher.dismissFolderCling(null);
214
Adam Cohendf2cc412011-04-27 16:56:57 -0700215 mLauncher.getWorkspace().onDragStartedWithItem(v);
Adam Cohenac8c8762011-07-13 11:15:27 -0700216 mLauncher.getWorkspace().beginDragShared(v, this);
Adam Cohenbadf71e2011-05-26 19:08:29 -0700217 mIconDrawable = ((TextView) v).getCompoundDrawables()[1];
Adam Cohen76078c42011-06-09 15:06:52 -0700218
219 mCurrentDragInfo = item;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700220 mEmptyCell[0] = item.cellX;
221 mEmptyCell[1] = item.cellY;
222 mCurrentDragView = v;
Adam Cohenfc53cd22011-07-20 15:45:11 -0700223
224 mContent.removeView(mCurrentDragView);
225 mInfo.remove(mCurrentDragInfo);
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700226 mDragInProgress = true;
Adam Cohen05e0f402011-08-01 12:12:49 -0700227 mItemAddedBackToSelfViaIcon = false;
Adam Cohendf2cc412011-04-27 16:56:57 -0700228 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800229 return true;
230 }
231
Adam Cohen76fc0852011-06-17 13:26:23 -0700232 public boolean isEditingName() {
233 return mIsEditingName;
234 }
235
236 public void startEditingFolderName() {
Adam Cohena65beee2011-06-27 21:32:23 -0700237 mFolderName.setHint("");
Adam Cohen76fc0852011-06-17 13:26:23 -0700238 mIsEditingName = true;
239 }
240
241 public void dismissEditingName() {
242 mInputMethodManager.hideSoftInputFromWindow(getWindowToken(), 0);
243 doneEditingFolderName(true);
244 }
245
246 public void doneEditingFolderName(boolean commit) {
Adam Cohena65beee2011-06-27 21:32:23 -0700247 mFolderName.setHint(sHintText);
Adam Cohen1df26a32011-08-12 16:15:23 -0700248 // Convert to a string here to ensure that no other state associated with the text field
249 // gets saved.
Adam Cohen3371da02011-10-25 21:38:29 -0700250 String newTitle = mFolderName.getText().toString();
251 mInfo.setTitle(newTitle);
Adam Cohen76fc0852011-06-17 13:26:23 -0700252 LauncherModel.updateItemInDatabase(mLauncher, mInfo);
Adam Cohenac56cff2011-09-28 20:45:37 -0700253
Adam Cohen3371da02011-10-25 21:38:29 -0700254 if (commit) {
255 sendCustomAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED,
Michael Jurka8b805b12012-04-18 14:23:14 -0700256 String.format(getContext().getString(R.string.folder_renamed), newTitle));
Adam Cohen3371da02011-10-25 21:38:29 -0700257 }
Adam Cohenac56cff2011-09-28 20:45:37 -0700258 // In order to clear the focus from the text field, we set the focus on ourself. This
259 // ensures that every time the field is clicked, focus is gained, giving reliable behavior.
260 requestFocus();
261
Adam Cohene601a432011-07-26 21:51:30 -0700262 Selection.setSelection((Spannable) mFolderName.getText(), 0, 0);
Adam Cohen76fc0852011-06-17 13:26:23 -0700263 mIsEditingName = false;
264 }
265
266 public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
267 if (actionId == EditorInfo.IME_ACTION_DONE) {
268 dismissEditingName();
269 return true;
270 }
271 return false;
272 }
273
274 public View getEditTextRegion() {
275 return mFolderName;
276 }
277
Adam Cohenbadf71e2011-05-26 19:08:29 -0700278 public Drawable getDragDrawable() {
279 return mIconDrawable;
280 }
281
Adam Cohen0c872ba2011-05-05 10:34:16 -0700282 /**
283 * We need to handle touch events to prevent them from falling through to the workspace below.
284 */
285 @Override
286 public boolean onTouchEvent(MotionEvent ev) {
287 return true;
288 }
289
Joe Onorato00acb122009-08-04 16:04:30 -0400290 public void setDragController(DragController dragController) {
291 mDragController = dragController;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800292 }
293
Adam Cohen2801caf2011-05-13 20:57:39 -0700294 void setFolderIcon(FolderIcon icon) {
295 mFolderIcon = icon;
296 }
297
Adam Cohen3371da02011-10-25 21:38:29 -0700298 @Override
299 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
300 // When the folder gets focus, we don't want to announce the list of items.
301 return true;
302 }
303
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800304 /**
305 * @return the FolderInfo object associated with this folder
306 */
307 FolderInfo getInfo() {
308 return mInfo;
309 }
310
Adam Cohen3bf84d32012-05-07 20:17:14 -0700311 private class GridComparator implements Comparator<ShortcutInfo> {
312 int mNumCols;
313 public GridComparator(int numCols) {
314 mNumCols = numCols;
315 }
316
317 @Override
318 public int compare(ShortcutInfo lhs, ShortcutInfo rhs) {
319 int lhIndex = lhs.cellY * mNumCols + lhs.cellX;
320 int rhIndex = rhs.cellY * mNumCols + rhs.cellX;
321 return (lhIndex - rhIndex);
322 }
323
324 }
325
326 private void placeInReadingOrder(ArrayList<ShortcutInfo> items) {
327 int maxX = 0;
328 int count = items.size();
329 for (int i = 0; i < count; i++) {
330 ShortcutInfo item = items.get(i);
331 if (item.cellX > maxX) {
332 maxX = item.cellX;
333 }
334 }
335 GridComparator gridComparator = new GridComparator(maxX);
336 Collections.sort(items, gridComparator);
337 final int countX = mContent.getCountX();
338 for (int i = 0; i < count; i++) {
339 int x = i % countX;
340 int y = i / countX;
341 ShortcutInfo item = items.get(i);
342 item.cellX = x;
343 item.cellY = y;
344 }
345 }
346
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800347 void bind(FolderInfo info) {
348 mInfo = info;
Adam Cohendf2cc412011-04-27 16:56:57 -0700349 ArrayList<ShortcutInfo> children = info.contents;
Adam Cohenc508b2d2011-06-28 14:41:44 -0700350 ArrayList<ShortcutInfo> overflow = new ArrayList<ShortcutInfo>();
Adam Cohen7c693212011-05-18 15:26:57 -0700351 setupContentForNumItems(children.size());
Adam Cohen3bf84d32012-05-07 20:17:14 -0700352 placeInReadingOrder(children);
Adam Cohen0057bbc2011-08-12 18:30:51 -0700353 int count = 0;
Adam Cohendf2cc412011-04-27 16:56:57 -0700354 for (int i = 0; i < children.size(); i++) {
355 ShortcutInfo child = (ShortcutInfo) children.get(i);
Adam Cohenc508b2d2011-06-28 14:41:44 -0700356 if (!createAndAddShortcut(child)) {
357 overflow.add(child);
Adam Cohen0057bbc2011-08-12 18:30:51 -0700358 } else {
359 count++;
Adam Cohenc508b2d2011-06-28 14:41:44 -0700360 }
Adam Cohendf2cc412011-04-27 16:56:57 -0700361 }
Adam Cohenc508b2d2011-06-28 14:41:44 -0700362
Adam Cohen0057bbc2011-08-12 18:30:51 -0700363 // We rearrange the items in case there are any empty gaps
364 setupContentForNumItems(count);
365
Adam Cohenc508b2d2011-06-28 14:41:44 -0700366 // If our folder has too many items we prune them from the list. This is an issue
367 // when upgrading from the old Folders implementation which could contain an unlimited
368 // number of items.
369 for (ShortcutInfo item: overflow) {
370 mInfo.remove(item);
371 LauncherModel.deleteItemFromDatabase(mLauncher, item);
372 }
373
Adam Cohen4dbe6d92011-05-18 17:14:15 -0700374 mItemsInvalidated = true;
Adam Cohenac56cff2011-09-28 20:45:37 -0700375 updateTextViewFocus();
Adam Cohena9cf38f2011-05-02 15:36:58 -0700376 mInfo.addListener(this);
Adam Cohen4ef610f2011-06-21 22:37:36 -0700377
Adam Cohenafb01ee2011-06-23 15:38:03 -0700378 if (!sDefaultFolderName.contentEquals(mInfo.title)) {
Adam Cohen4ef610f2011-06-21 22:37:36 -0700379 mFolderName.setText(mInfo.title);
380 } else {
381 mFolderName.setText("");
382 }
Adam Cohendf2cc412011-04-27 16:56:57 -0700383 }
384
385 /**
386 * Creates a new UserFolder, inflated from R.layout.user_folder.
387 *
388 * @param context The application's context.
389 *
390 * @return A new UserFolder.
391 */
392 static Folder fromXml(Context context) {
393 return (Folder) LayoutInflater.from(context).inflate(R.layout.user_folder, null);
394 }
395
396 /**
397 * This method is intended to make the UserFolder to be visually identical in size and position
398 * to its associated FolderIcon. This allows for a seamless transition into the expanded state.
399 */
400 private void positionAndSizeAsIcon() {
Adam Cohen3e8f8112011-07-02 18:03:00 -0700401 if (!(getParent() instanceof DragLayer)) return;
Adam Cohen662b5982011-12-13 17:45:21 -0800402 setScaleX(0.8f);
403 setScaleY(0.8f);
404 setAlpha(0f);
Adam Cohendf2cc412011-04-27 16:56:57 -0700405 mState = STATE_SMALL;
406 }
407
408 public void animateOpen() {
Adam Cohen3e8f8112011-07-02 18:03:00 -0700409 positionAndSizeAsIcon();
410
Adam Cohen8e776a62011-06-28 18:10:06 -0700411 if (!(getParent() instanceof DragLayer)) return;
Adam Cohen2801caf2011-05-13 20:57:39 -0700412 centerAboutIcon();
Adam Cohen662b5982011-12-13 17:45:21 -0800413 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1);
414 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.0f);
415 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.0f);
416 ObjectAnimator oa = ObjectAnimator.ofPropertyValuesHolder(this, alpha, scaleX, scaleY);
Adam Cohen6441de02011-12-14 14:25:32 -0800417
Adam Cohen2801caf2011-05-13 20:57:39 -0700418 oa.addListener(new AnimatorListenerAdapter() {
Adam Cohendf2cc412011-04-27 16:56:57 -0700419 @Override
420 public void onAnimationStart(Animator animation) {
Adam Cohen3371da02011-10-25 21:38:29 -0700421 sendCustomAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED,
Michael Jurka8b805b12012-04-18 14:23:14 -0700422 String.format(getContext().getString(R.string.folder_opened),
Adam Cohen3371da02011-10-25 21:38:29 -0700423 mContent.getCountX(), mContent.getCountY()));
Adam Cohendf2cc412011-04-27 16:56:57 -0700424 mState = STATE_ANIMATING;
425 }
426 @Override
427 public void onAnimationEnd(Animator animation) {
Adam Cohen2801caf2011-05-13 20:57:39 -0700428 mState = STATE_OPEN;
Winson Chung7a74ac92011-09-20 17:43:51 -0700429
430 Cling cling = mLauncher.showFirstRunFoldersCling();
431 if (cling != null) {
432 cling.bringToFront();
433 }
Adam Cohenac56cff2011-09-28 20:45:37 -0700434 setFocusOnFirstChild();
Adam Cohendf2cc412011-04-27 16:56:57 -0700435 }
436 });
Adam Cohen2801caf2011-05-13 20:57:39 -0700437 oa.setDuration(mExpandDuration);
438 oa.start();
Adam Cohendf2cc412011-04-27 16:56:57 -0700439 }
440
Adam Cohen3371da02011-10-25 21:38:29 -0700441 private void sendCustomAccessibilityEvent(int type, String text) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700442 AccessibilityManager accessibilityManager = (AccessibilityManager)
443 getContext().getSystemService(Context.ACCESSIBILITY_SERVICE);
444 if (accessibilityManager.isEnabled()) {
Adam Cohen3371da02011-10-25 21:38:29 -0700445 AccessibilityEvent event = AccessibilityEvent.obtain(type);
446 onInitializeAccessibilityEvent(event);
447 event.getText().add(text);
Michael Jurka8b805b12012-04-18 14:23:14 -0700448 accessibilityManager.sendAccessibilityEvent(event);
Adam Cohen3371da02011-10-25 21:38:29 -0700449 }
450 }
451
Adam Cohenac56cff2011-09-28 20:45:37 -0700452 private void setFocusOnFirstChild() {
453 View firstChild = mContent.getChildAt(0, 0);
454 if (firstChild != null) {
455 firstChild.requestFocus();
456 }
457 }
458
Adam Cohendf2cc412011-04-27 16:56:57 -0700459 public void animateClosed() {
Adam Cohen8e776a62011-06-28 18:10:06 -0700460 if (!(getParent() instanceof DragLayer)) return;
Adam Cohen662b5982011-12-13 17:45:21 -0800461 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0);
462 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 0.9f);
463 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 0.9f);
464 ObjectAnimator oa = ObjectAnimator.ofPropertyValuesHolder(this, alpha, scaleX, scaleY);
Adam Cohendf2cc412011-04-27 16:56:57 -0700465
Adam Cohen2801caf2011-05-13 20:57:39 -0700466 oa.addListener(new AnimatorListenerAdapter() {
Adam Cohendf2cc412011-04-27 16:56:57 -0700467 @Override
468 public void onAnimationEnd(Animator animation) {
Adam Cohen2801caf2011-05-13 20:57:39 -0700469 onCloseComplete();
Adam Cohen2801caf2011-05-13 20:57:39 -0700470 mState = STATE_SMALL;
Adam Cohendf2cc412011-04-27 16:56:57 -0700471 }
472 @Override
473 public void onAnimationStart(Animator animation) {
Adam Cohen3371da02011-10-25 21:38:29 -0700474 sendCustomAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED,
Michael Jurka8b805b12012-04-18 14:23:14 -0700475 getContext().getString(R.string.folder_closed));
Adam Cohendf2cc412011-04-27 16:56:57 -0700476 mState = STATE_ANIMATING;
477 }
478 });
Adam Cohen2801caf2011-05-13 20:57:39 -0700479 oa.setDuration(mExpandDuration);
480 oa.start();
Adam Cohendf2cc412011-04-27 16:56:57 -0700481 }
482
483 void notifyDataSetChanged() {
484 // recreate all the children if the data set changes under us. We may want to do this more
485 // intelligently (ie just removing the views that should no longer exist)
486 mContent.removeAllViewsInLayout();
487 bind(mInfo);
488 }
489
Adam Cohencb3382b2011-05-24 14:07:08 -0700490 public boolean acceptDrop(DragObject d) {
491 final ItemInfo item = (ItemInfo) d.dragInfo;
Adam Cohendf2cc412011-04-27 16:56:57 -0700492 final int itemType = item.itemType;
Adam Cohen2801caf2011-05-13 20:57:39 -0700493 return ((itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION ||
494 itemType == LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT) &&
495 !isFull());
Adam Cohendf2cc412011-04-27 16:56:57 -0700496 }
497
Adam Cohendf2cc412011-04-27 16:56:57 -0700498 protected boolean findAndSetEmptyCells(ShortcutInfo item) {
499 int[] emptyCell = new int[2];
500 if (mContent.findCellForSpan(emptyCell, item.spanX, item.spanY)) {
501 item.cellX = emptyCell[0];
502 item.cellY = emptyCell[1];
Adam Cohendf2cc412011-04-27 16:56:57 -0700503 return true;
504 } else {
505 return false;
506 }
507 }
508
Adam Cohenc508b2d2011-06-28 14:41:44 -0700509 protected boolean createAndAddShortcut(ShortcutInfo item) {
Adam Cohendf2cc412011-04-27 16:56:57 -0700510 final TextView textView =
Adam Cohene87b9242011-06-29 14:01:26 -0700511 (TextView) mInflater.inflate(R.layout.application, this, false);
Adam Cohendf2cc412011-04-27 16:56:57 -0700512 textView.setCompoundDrawablesWithIntrinsicBounds(null,
513 new FastBitmapDrawable(item.getIcon(mIconCache)), null, null);
514 textView.setText(item.title);
515 textView.setTag(item);
516
517 textView.setOnClickListener(this);
518 textView.setOnLongClickListener(this);
519
Adam Cohenc508b2d2011-06-28 14:41:44 -0700520 // We need to check here to verify that the given item's location isn't already occupied
Adam Cohen3bf84d32012-05-07 20:17:14 -0700521 // by another item.
Adam Cohen0057bbc2011-08-12 18:30:51 -0700522 if (mContent.getChildAt(item.cellX, item.cellY) != null || item.cellX < 0 || item.cellY < 0
523 || item.cellX >= mContent.getCountX() || item.cellY >= mContent.getCountY()) {
Adam Cohen3bf84d32012-05-07 20:17:14 -0700524 // This shouldn't happen, log it.
525 Log.e(TAG, "Folder order not properly persisted during bind");
Adam Cohenc508b2d2011-06-28 14:41:44 -0700526 if (!findAndSetEmptyCells(item)) {
527 return false;
528 }
529 }
530
Adam Cohendf2cc412011-04-27 16:56:57 -0700531 CellLayout.LayoutParams lp =
532 new CellLayout.LayoutParams(item.cellX, item.cellY, item.spanX, item.spanY);
533 boolean insert = false;
Adam Cohenac56cff2011-09-28 20:45:37 -0700534 textView.setOnKeyListener(new FolderKeyEventListener());
Adam Cohendf2cc412011-04-27 16:56:57 -0700535 mContent.addViewToCellLayout(textView, insert ? 0 : -1, (int)item.id, lp, true);
Adam Cohenc508b2d2011-06-28 14:41:44 -0700536 return true;
Adam Cohendf2cc412011-04-27 16:56:57 -0700537 }
538
Adam Cohencb3382b2011-05-24 14:07:08 -0700539 public void onDragEnter(DragObject d) {
Adam Cohenbfbfd262011-06-13 16:55:12 -0700540 mPreviousTargetCell[0] = -1;
541 mPreviousTargetCell[1] = -1;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700542 mOnExitAlarm.cancelAlarm();
543 }
544
545 OnAlarmListener mReorderAlarmListener = new OnAlarmListener() {
546 public void onAlarm(Alarm alarm) {
547 realTimeReorder(mEmptyCell, mTargetCell);
548 }
549 };
550
551 boolean readingOrderGreaterThan(int[] v1, int[] v2) {
552 if (v1[1] > v2[1] || (v1[1] == v2[1] && v1[0] > v2[0])) {
553 return true;
554 } else {
555 return false;
556 }
557 }
558
559 private void realTimeReorder(int[] empty, int[] target) {
560 boolean wrap;
561 int startX;
562 int endX;
563 int startY;
Adam Cohen76fc0852011-06-17 13:26:23 -0700564 int delay = 0;
565 float delayAmount = 30;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700566 if (readingOrderGreaterThan(target, empty)) {
567 wrap = empty[0] >= mContent.getCountX() - 1;
568 startY = wrap ? empty[1] + 1 : empty[1];
569 for (int y = startY; y <= target[1]; y++) {
570 startX = y == empty[1] ? empty[0] + 1 : 0;
571 endX = y < target[1] ? mContent.getCountX() - 1 : target[0];
572 for (int x = startX; x <= endX; x++) {
573 View v = mContent.getChildAt(x,y);
574 if (mContent.animateChildToPosition(v, empty[0], empty[1],
Adam Cohen482ed822012-03-02 14:15:13 -0800575 REORDER_ANIMATION_DURATION, delay, true, true)) {
Adam Cohenbfbfd262011-06-13 16:55:12 -0700576 empty[0] = x;
577 empty[1] = y;
Adam Cohen76fc0852011-06-17 13:26:23 -0700578 delay += delayAmount;
579 delayAmount *= 0.9;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700580 }
581 }
582 }
583 } else {
584 wrap = empty[0] == 0;
585 startY = wrap ? empty[1] - 1 : empty[1];
586 for (int y = startY; y >= target[1]; y--) {
587 startX = y == empty[1] ? empty[0] - 1 : mContent.getCountX() - 1;
588 endX = y > target[1] ? 0 : target[0];
589 for (int x = startX; x >= endX; x--) {
590 View v = mContent.getChildAt(x,y);
591 if (mContent.animateChildToPosition(v, empty[0], empty[1],
Adam Cohen482ed822012-03-02 14:15:13 -0800592 REORDER_ANIMATION_DURATION, delay, true, true)) {
Adam Cohenbfbfd262011-06-13 16:55:12 -0700593 empty[0] = x;
594 empty[1] = y;
Adam Cohen76fc0852011-06-17 13:26:23 -0700595 delay += delayAmount;
596 delayAmount *= 0.9;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700597 }
598 }
599 }
600 }
Adam Cohendf2cc412011-04-27 16:56:57 -0700601 }
602
Adam Cohencb3382b2011-05-24 14:07:08 -0700603 public void onDragOver(DragObject d) {
Adam Cohenbfbfd262011-06-13 16:55:12 -0700604 float[] r = getDragViewVisualCenter(d.x, d.y, d.xOffset, d.yOffset, d.dragView, null);
605 mTargetCell = mContent.findNearestArea((int) r[0], (int) r[1], 1, 1, mTargetCell);
606
607 if (mTargetCell[0] != mPreviousTargetCell[0] || mTargetCell[1] != mPreviousTargetCell[1]) {
608 mReorderAlarm.cancelAlarm();
609 mReorderAlarm.setOnAlarmListener(mReorderAlarmListener);
610 mReorderAlarm.setAlarm(150);
611 mPreviousTargetCell[0] = mTargetCell[0];
612 mPreviousTargetCell[1] = mTargetCell[1];
613 }
Adam Cohendf2cc412011-04-27 16:56:57 -0700614 }
615
Adam Cohenbfbfd262011-06-13 16:55:12 -0700616 // This is used to compute the visual center of the dragView. The idea is that
617 // the visual center represents the user's interpretation of where the item is, and hence
618 // is the appropriate point to use when determining drop location.
619 private float[] getDragViewVisualCenter(int x, int y, int xOffset, int yOffset,
620 DragView dragView, float[] recycle) {
621 float res[];
622 if (recycle == null) {
623 res = new float[2];
624 } else {
625 res = recycle;
626 }
627
628 // These represent the visual top and left of drag view if a dragRect was provided.
629 // If a dragRect was not provided, then they correspond to the actual view left and
630 // top, as the dragRect is in that case taken to be the entire dragView.
631 // R.dimen.dragViewOffsetY.
632 int left = x - xOffset;
633 int top = y - yOffset;
634
635 // In order to find the visual center, we shift by half the dragRect
636 res[0] = left + dragView.getDragRegion().width() / 2;
637 res[1] = top + dragView.getDragRegion().height() / 2;
638
639 return res;
640 }
641
642 OnAlarmListener mOnExitAlarmListener = new OnAlarmListener() {
643 public void onAlarm(Alarm alarm) {
Adam Cohen3e8f8112011-07-02 18:03:00 -0700644 completeDragExit();
Adam Cohenbfbfd262011-06-13 16:55:12 -0700645 }
646 };
647
Adam Cohen95bb8002011-07-03 23:40:28 -0700648 public void completeDragExit() {
Adam Cohen3e8f8112011-07-02 18:03:00 -0700649 mLauncher.closeFolder();
650 mCurrentDragInfo = null;
651 mCurrentDragView = null;
652 mSuppressOnAdd = false;
653 mRearrangeOnClose = true;
654 }
655
Adam Cohencb3382b2011-05-24 14:07:08 -0700656 public void onDragExit(DragObject d) {
Adam Cohenbfbfd262011-06-13 16:55:12 -0700657 // We only close the folder if this is a true drag exit, ie. not because a drop
658 // has occurred above the folder.
659 if (!d.dragComplete) {
660 mOnExitAlarm.setOnAlarmListener(mOnExitAlarmListener);
661 mOnExitAlarm.setAlarm(ON_EXIT_CLOSE_DELAY);
662 }
663 mReorderAlarm.cancelAlarm();
Adam Cohen2801caf2011-05-13 20:57:39 -0700664 }
665
Winson Chunga48487a2012-03-20 16:19:37 -0700666 public void onDropCompleted(View target, DragObject d, boolean isFlingToDelete,
667 boolean success) {
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700668 if (success) {
Adam Cohen05e0f402011-08-01 12:12:49 -0700669 if (mDeleteFolderOnDropCompleted && !mItemAddedBackToSelfViaIcon) {
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700670 replaceFolderWithFinalItem();
671 }
672 } else {
673 // The drag failed, we need to return the item to the folder
674 mFolderIcon.onDrop(d);
675
676 // We're going to trigger a "closeFolder" which may occur before this item has
677 // been added back to the folder -- this could cause the folder to be deleted
678 if (mOnExitAlarm.alarmPending()) {
679 mSuppressFolderDeletion = true;
680 }
681 }
682
683 if (target != this) {
684 if (mOnExitAlarm.alarmPending()) {
685 mOnExitAlarm.cancelAlarm();
686 completeDragExit();
687 }
688 }
689 mDeleteFolderOnDropCompleted = false;
690 mDragInProgress = false;
Adam Cohen05e0f402011-08-01 12:12:49 -0700691 mItemAddedBackToSelfViaIcon = false;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700692 mCurrentDragInfo = null;
693 mCurrentDragView = null;
694 mSuppressOnAdd = false;
Adam Cohen4045eb72011-10-06 11:44:26 -0700695
696 // Reordering may have occured, and we need to save the new item locations. We do this once
697 // at the end to prevent unnecessary database operations.
698 updateItemLocationsInDatabase();
699 }
700
Winson Chunga48487a2012-03-20 16:19:37 -0700701 @Override
Winson Chung043f2af2012-03-01 16:09:54 -0800702 public boolean supportsFlingToDelete() {
703 return true;
704 }
705
Winson Chunga48487a2012-03-20 16:19:37 -0700706 public void onFlingToDelete(DragObject d, int x, int y, PointF vec) {
707 // Do nothing
708 }
709
710 @Override
711 public void onFlingToDeleteCompleted() {
712 // Do nothing
713 }
714
Adam Cohen4045eb72011-10-06 11:44:26 -0700715 private void updateItemLocationsInDatabase() {
716 ArrayList<View> list = getItemsInReadingOrder();
717 for (int i = 0; i < list.size(); i++) {
718 View v = list.get(i);
719 ItemInfo info = (ItemInfo) v.getTag();
720 LauncherModel.moveItemInDatabase(mLauncher, info, mInfo.id, 0,
721 info.cellX, info.cellY);
722 }
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700723 }
Adam Cohen228da5a2011-07-27 22:23:47 -0700724
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700725 public void notifyDrop() {
726 if (mDragInProgress) {
Adam Cohen05e0f402011-08-01 12:12:49 -0700727 mItemAddedBackToSelfViaIcon = true;
Adam Cohen76078c42011-06-09 15:06:52 -0700728 }
Adam Cohendf2cc412011-04-27 16:56:57 -0700729 }
730
731 public boolean isDropEnabled() {
732 return true;
733 }
734
Adam Cohencb3382b2011-05-24 14:07:08 -0700735 public DropTarget getDropTargetDelegate(DragObject d) {
Adam Cohendf2cc412011-04-27 16:56:57 -0700736 return null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800737 }
Adam Cohena9cf38f2011-05-02 15:36:58 -0700738
Adam Cohen4045eb72011-10-06 11:44:26 -0700739 private void setupContentDimensions(int count) {
Adam Cohen2801caf2011-05-13 20:57:39 -0700740 ArrayList<View> list = getItemsInReadingOrder();
741
742 int countX = mContent.getCountX();
743 int countY = mContent.getCountY();
Adam Cohen7c693212011-05-18 15:26:57 -0700744 boolean done = false;
Adam Cohen2801caf2011-05-13 20:57:39 -0700745
Adam Cohen7c693212011-05-18 15:26:57 -0700746 while (!done) {
747 int oldCountX = countX;
748 int oldCountY = countY;
749 if (countX * countY < count) {
750 // Current grid is too small, expand it
Adam Cohen78dc83e2011-11-15 17:10:00 -0800751 if ((countX <= countY || countY == mMaxCountY) && countX < mMaxCountX) {
Adam Cohen7c693212011-05-18 15:26:57 -0700752 countX++;
753 } else if (countY < mMaxCountY) {
754 countY++;
755 }
756 if (countY == 0) countY++;
757 } else if ((countY - 1) * countX >= count && countY >= countX) {
758 countY = Math.max(0, countY - 1);
759 } else if ((countX - 1) * countY >= count) {
760 countX = Math.max(0, countX - 1);
Adam Cohen2801caf2011-05-13 20:57:39 -0700761 }
Adam Cohen7c693212011-05-18 15:26:57 -0700762 done = countX == oldCountX && countY == oldCountY;
Adam Cohen2801caf2011-05-13 20:57:39 -0700763 }
Adam Cohen7c693212011-05-18 15:26:57 -0700764 mContent.setGridSize(countX, countY);
Adam Cohen2801caf2011-05-13 20:57:39 -0700765 arrangeChildren(list);
766 }
767
768 public boolean isFull() {
Adam Cohen78dc83e2011-11-15 17:10:00 -0800769 return getItemCount() >= mMaxNumItems;
Adam Cohen2801caf2011-05-13 20:57:39 -0700770 }
771
772 private void centerAboutIcon() {
Adam Cohen8e776a62011-06-28 18:10:06 -0700773 DragLayer.LayoutParams lp = (DragLayer.LayoutParams) getLayoutParams();
Adam Cohen2801caf2011-05-13 20:57:39 -0700774
775 int width = getPaddingLeft() + getPaddingRight() + mContent.getDesiredWidth();
Adam Cohenf4bb1cd2011-07-22 14:36:03 -0700776 int height = getPaddingTop() + getPaddingBottom() + mContent.getDesiredHeight()
777 + mFolderNameHeight;
Adam Cohen8e776a62011-06-28 18:10:06 -0700778 DragLayer parent = (DragLayer) mLauncher.findViewById(R.id.drag_layer);
Adam Cohen2801caf2011-05-13 20:57:39 -0700779
Adam Cohen8e776a62011-06-28 18:10:06 -0700780 parent.getDescendantRectRelativeToSelf(mFolderIcon, mTempRect);
781
782 int centerX = mTempRect.centerX();
783 int centerY = mTempRect.centerY();
Adam Cohen2801caf2011-05-13 20:57:39 -0700784 int centeredLeft = centerX - width / 2;
785 int centeredTop = centerY - height / 2;
786
Adam Cohen7cc1bc42011-12-13 21:28:43 -0800787 int currentPage = mLauncher.getWorkspace().getCurrentPage();
788 // In case the workspace is scrolling, we need to use the final scroll to compute
789 // the folders bounds.
790 mLauncher.getWorkspace().setFinalScrollForPageChange(currentPage);
Adam Cohen35e7e642011-07-17 14:47:18 -0700791 // We first fetch the currently visible CellLayoutChildren
Adam Cohen7cc1bc42011-12-13 21:28:43 -0800792 CellLayout currentLayout = (CellLayout) mLauncher.getWorkspace().getChildAt(currentPage);
Michael Jurkaa52570f2012-03-20 03:18:20 -0700793 ShortcutAndWidgetContainer boundingLayout = currentLayout.getShortcutsAndWidgets();
Adam Cohen35e7e642011-07-17 14:47:18 -0700794 Rect bounds = new Rect();
795 parent.getDescendantRectRelativeToSelf(boundingLayout, bounds);
Adam Cohen7cc1bc42011-12-13 21:28:43 -0800796 // We reset the workspaces scroll
797 mLauncher.getWorkspace().resetFinalScrollForPageChange(currentPage);
Adam Cohen2801caf2011-05-13 20:57:39 -0700798
Adam Cohen35e7e642011-07-17 14:47:18 -0700799 // We need to bound the folder to the currently visible CellLayoutChildren
800 int left = Math.min(Math.max(bounds.left, centeredLeft),
801 bounds.left + bounds.width() - width);
802 int top = Math.min(Math.max(bounds.top, centeredTop),
803 bounds.top + bounds.height() - height);
804 // If the folder doesn't fit within the bounds, center it about the desired bounds
805 if (width >= bounds.width()) {
806 left = bounds.left + (bounds.width() - width) / 2;
Adam Cohen0e4857c2011-06-30 17:05:14 -0700807 }
Adam Cohen35e7e642011-07-17 14:47:18 -0700808 if (height >= bounds.height()) {
809 top = bounds.top + (bounds.height() - height) / 2;
Adam Cohen0e4857c2011-06-30 17:05:14 -0700810 }
Adam Cohen2801caf2011-05-13 20:57:39 -0700811
812 int folderPivotX = width / 2 + (centeredLeft - left);
813 int folderPivotY = height / 2 + (centeredTop - top);
814 setPivotX(folderPivotX);
815 setPivotY(folderPivotY);
816 int folderIconPivotX = (int) (mFolderIcon.getMeasuredWidth() *
817 (1.0f * folderPivotX / width));
818 int folderIconPivotY = (int) (mFolderIcon.getMeasuredHeight() *
819 (1.0f * folderPivotY / height));
Adam Cohen3bf84d32012-05-07 20:17:14 -0700820
Adam Cohen2801caf2011-05-13 20:57:39 -0700821 mFolderIcon.setPivotX(folderIconPivotX);
822 mFolderIcon.setPivotY(folderIconPivotY);
823
Adam Cohen662b5982011-12-13 17:45:21 -0800824 lp.width = width;
825 lp.height = height;
826 lp.x = left;
827 lp.y = top;
Adam Cohen2801caf2011-05-13 20:57:39 -0700828 }
829
830 private void setupContentForNumItems(int count) {
Adam Cohen4045eb72011-10-06 11:44:26 -0700831 setupContentDimensions(count);
Adam Cohen2801caf2011-05-13 20:57:39 -0700832
Adam Cohen8e776a62011-06-28 18:10:06 -0700833 DragLayer.LayoutParams lp = (DragLayer.LayoutParams) getLayoutParams();
Adam Cohen2801caf2011-05-13 20:57:39 -0700834 if (lp == null) {
Adam Cohen8e776a62011-06-28 18:10:06 -0700835 lp = new DragLayer.LayoutParams(0, 0);
836 lp.customPosition = true;
Adam Cohen2801caf2011-05-13 20:57:39 -0700837 setLayoutParams(lp);
838 }
839 centerAboutIcon();
840 }
841
Adam Cohen234c4cd2011-07-17 21:03:04 -0700842 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
843 int width = getPaddingLeft() + getPaddingRight() + mContent.getDesiredWidth();
Adam Cohenf4bb1cd2011-07-22 14:36:03 -0700844 int height = getPaddingTop() + getPaddingBottom() + mContent.getDesiredHeight()
845 + mFolderNameHeight;
Adam Cohen234c4cd2011-07-17 21:03:04 -0700846
847 int contentWidthSpec = MeasureSpec.makeMeasureSpec(mContent.getDesiredWidth(),
848 MeasureSpec.EXACTLY);
849 int contentHeightSpec = MeasureSpec.makeMeasureSpec(mContent.getDesiredHeight(),
850 MeasureSpec.EXACTLY);
851 mContent.measure(contentWidthSpec, contentHeightSpec);
852
853 mFolderName.measure(contentWidthSpec,
854 MeasureSpec.makeMeasureSpec(mFolderNameHeight, MeasureSpec.EXACTLY));
855 setMeasuredDimension(width, height);
856 }
857
Adam Cohen2801caf2011-05-13 20:57:39 -0700858 private void arrangeChildren(ArrayList<View> list) {
859 int[] vacant = new int[2];
860 if (list == null) {
861 list = getItemsInReadingOrder();
862 }
863 mContent.removeAllViews();
864
865 for (int i = 0; i < list.size(); i++) {
866 View v = list.get(i);
867 mContent.getVacantCell(vacant, 1, 1);
868 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) v.getLayoutParams();
869 lp.cellX = vacant[0];
870 lp.cellY = vacant[1];
871 ItemInfo info = (ItemInfo) v.getTag();
Adam Cohen2792a332011-09-26 21:09:47 -0700872 if (info.cellX != vacant[0] || info.cellY != vacant[1]) {
873 info.cellX = vacant[0];
874 info.cellY = vacant[1];
875 LauncherModel.addOrMoveItemInDatabase(mLauncher, info, mInfo.id, 0,
876 info.cellX, info.cellY);
877 }
Adam Cohen2801caf2011-05-13 20:57:39 -0700878 boolean insert = false;
879 mContent.addViewToCellLayout(v, insert ? 0 : -1, (int)info.id, lp, true);
880 }
Adam Cohen7c693212011-05-18 15:26:57 -0700881 mItemsInvalidated = true;
Adam Cohen2801caf2011-05-13 20:57:39 -0700882 }
883
Adam Cohena9cf38f2011-05-02 15:36:58 -0700884 public int getItemCount() {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700885 return mContent.getShortcutsAndWidgets().getChildCount();
Adam Cohena9cf38f2011-05-02 15:36:58 -0700886 }
887
888 public View getItemAt(int index) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700889 return mContent.getShortcutsAndWidgets().getChildAt(index);
Adam Cohena9cf38f2011-05-02 15:36:58 -0700890 }
891
Adam Cohen2801caf2011-05-13 20:57:39 -0700892 private void onCloseComplete() {
Adam Cohen05e0f402011-08-01 12:12:49 -0700893 DragLayer parent = (DragLayer) getParent();
Adam Cohen4554ee12011-08-03 16:13:21 -0700894 parent.removeView(this);
895 mDragController.removeDropTarget((DropTarget) this);
Adam Cohen05e0f402011-08-01 12:12:49 -0700896 clearFocus();
Adam Cohenac56cff2011-09-28 20:45:37 -0700897 mFolderIcon.requestFocus();
Adam Cohen05e0f402011-08-01 12:12:49 -0700898
Adam Cohen2801caf2011-05-13 20:57:39 -0700899 if (mRearrangeOnClose) {
900 setupContentForNumItems(getItemCount());
901 mRearrangeOnClose = false;
902 }
Adam Cohenafb01ee2011-06-23 15:38:03 -0700903 if (getItemCount() <= 1) {
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700904 if (!mDragInProgress && !mSuppressFolderDeletion) {
905 replaceFolderWithFinalItem();
906 } else if (mDragInProgress) {
907 mDeleteFolderOnDropCompleted = true;
908 }
Adam Cohenafb01ee2011-06-23 15:38:03 -0700909 }
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700910 mSuppressFolderDeletion = false;
Adam Cohenafb01ee2011-06-23 15:38:03 -0700911 }
912
913 private void replaceFolderWithFinalItem() {
914 ItemInfo finalItem = null;
915
916 if (getItemCount() == 1) {
917 finalItem = mInfo.contents.get(0);
918 }
919
920 // Remove the folder completely
Winson Chung3d503fb2011-07-13 17:25:49 -0700921 CellLayout cellLayout = mLauncher.getCellLayout(mInfo.container, mInfo.screen);
Adam Cohenafb01ee2011-06-23 15:38:03 -0700922 cellLayout.removeView(mFolderIcon);
923 if (mFolderIcon instanceof DropTarget) {
924 mDragController.removeDropTarget((DropTarget) mFolderIcon);
925 }
926 mLauncher.removeFolder(mInfo);
927
928 if (finalItem != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -0700929 LauncherModel.addOrMoveItemInDatabase(mLauncher, finalItem, mInfo.container,
930 mInfo.screen, mInfo.cellX, mInfo.cellY);
Adam Cohenafb01ee2011-06-23 15:38:03 -0700931 }
Adam Cohen716b51e2011-06-30 12:09:54 -0700932 LauncherModel.deleteItemFromDatabase(mLauncher, mInfo);
Adam Cohenafb01ee2011-06-23 15:38:03 -0700933
934 // Add the last remaining child to the workspace in place of the folder
935 if (finalItem != null) {
936 View child = mLauncher.createShortcut(R.layout.application, cellLayout,
937 (ShortcutInfo) finalItem);
938
Winson Chung3d503fb2011-07-13 17:25:49 -0700939 mLauncher.getWorkspace().addInScreen(child, mInfo.container, mInfo.screen, mInfo.cellX,
940 mInfo.cellY, mInfo.spanX, mInfo.spanY);
Adam Cohenafb01ee2011-06-23 15:38:03 -0700941 }
Adam Cohen2801caf2011-05-13 20:57:39 -0700942 }
943
Adam Cohenac56cff2011-09-28 20:45:37 -0700944 // This method keeps track of the last item in the folder for the purposes
945 // of keyboard focus
946 private void updateTextViewFocus() {
947 View lastChild = getItemAt(getItemCount() - 1);
948 getItemAt(getItemCount() - 1);
949 if (lastChild != null) {
950 mFolderName.setNextFocusDownId(lastChild.getId());
951 mFolderName.setNextFocusRightId(lastChild.getId());
952 mFolderName.setNextFocusLeftId(lastChild.getId());
953 mFolderName.setNextFocusUpId(lastChild.getId());
954 }
955 }
956
Adam Cohenbfbfd262011-06-13 16:55:12 -0700957 public void onDrop(DragObject d) {
958 ShortcutInfo item;
959 if (d.dragInfo instanceof ApplicationInfo) {
960 // Came from all apps -- make a copy
961 item = ((ApplicationInfo) d.dragInfo).makeShortcut();
962 item.spanX = 1;
963 item.spanY = 1;
964 } else {
965 item = (ShortcutInfo) d.dragInfo;
966 }
Adam Cohen05e0f402011-08-01 12:12:49 -0700967 // Dragged from self onto self, currently this is the only path possible, however
968 // we keep this as a distinct code path.
Adam Cohenbfbfd262011-06-13 16:55:12 -0700969 if (item == mCurrentDragInfo) {
970 ShortcutInfo si = (ShortcutInfo) mCurrentDragView.getTag();
971 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) mCurrentDragView.getLayoutParams();
972 si.cellX = lp.cellX = mEmptyCell[0];
973 si.cellX = lp.cellY = mEmptyCell[1];
974 mContent.addViewToCellLayout(mCurrentDragView, -1, (int)item.id, lp, true);
Adam Cohenfc53cd22011-07-20 15:45:11 -0700975 if (d.dragView.hasDrawn()) {
976 mLauncher.getDragLayer().animateViewIntoPosition(d.dragView, mCurrentDragView);
977 } else {
978 mCurrentDragView.setVisibility(VISIBLE);
979 }
Adam Cohene9166b22011-07-08 17:11:11 -0700980 mItemsInvalidated = true;
Adam Cohen4045eb72011-10-06 11:44:26 -0700981 setupContentDimensions(getItemCount());
Adam Cohen716b51e2011-06-30 12:09:54 -0700982 mSuppressOnAdd = true;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700983 }
984 mInfo.add(item);
985 }
986
987 public void onAdd(ShortcutInfo item) {
988 mItemsInvalidated = true;
Adam Cohen05e0f402011-08-01 12:12:49 -0700989 // If the item was dropped onto this open folder, we have done the work associated
990 // with adding the item to the folder, as indicated by mSuppressOnAdd being set
Adam Cohenbfbfd262011-06-13 16:55:12 -0700991 if (mSuppressOnAdd) return;
992 if (!findAndSetEmptyCells(item)) {
993 // The current layout is full, can we expand it?
994 setupContentForNumItems(getItemCount() + 1);
995 findAndSetEmptyCells(item);
996 }
997 createAndAddShortcut(item);
998 LauncherModel.addOrMoveItemInDatabase(
999 mLauncher, item, mInfo.id, 0, item.cellX, item.cellY);
1000 }
1001
Adam Cohena9cf38f2011-05-02 15:36:58 -07001002 public void onRemove(ShortcutInfo item) {
Adam Cohen7c693212011-05-18 15:26:57 -07001003 mItemsInvalidated = true;
Adam Cohen05e0f402011-08-01 12:12:49 -07001004 // If this item is being dragged from this open folder, we have already handled
1005 // the work associated with removing the item, so we don't have to do anything here.
Adam Cohenbfbfd262011-06-13 16:55:12 -07001006 if (item == mCurrentDragInfo) return;
Adam Cohendf1e4e82011-06-24 15:57:39 -07001007 View v = getViewForInfo(item);
Adam Cohena9cf38f2011-05-02 15:36:58 -07001008 mContent.removeView(v);
Adam Cohen2801caf2011-05-13 20:57:39 -07001009 if (mState == STATE_ANIMATING) {
1010 mRearrangeOnClose = true;
1011 } else {
1012 setupContentForNumItems(getItemCount());
1013 }
Adam Cohenafb01ee2011-06-23 15:38:03 -07001014 if (getItemCount() <= 1) {
1015 replaceFolderWithFinalItem();
1016 }
Adam Cohena9cf38f2011-05-02 15:36:58 -07001017 }
Adam Cohen7c693212011-05-18 15:26:57 -07001018
Adam Cohendf1e4e82011-06-24 15:57:39 -07001019 private View getViewForInfo(ShortcutInfo item) {
1020 for (int j = 0; j < mContent.getCountY(); j++) {
1021 for (int i = 0; i < mContent.getCountX(); i++) {
1022 View v = mContent.getChildAt(i, j);
1023 if (v.getTag() == item) {
1024 return v;
1025 }
1026 }
1027 }
1028 return null;
1029 }
1030
Adam Cohen76078c42011-06-09 15:06:52 -07001031 public void onItemsChanged() {
Adam Cohenac56cff2011-09-28 20:45:37 -07001032 updateTextViewFocus();
Adam Cohen76078c42011-06-09 15:06:52 -07001033 }
Adam Cohenac56cff2011-09-28 20:45:37 -07001034
Adam Cohen76fc0852011-06-17 13:26:23 -07001035 public void onTitleChanged(CharSequence title) {
1036 }
Adam Cohen76078c42011-06-09 15:06:52 -07001037
Adam Cohen7c693212011-05-18 15:26:57 -07001038 public ArrayList<View> getItemsInReadingOrder() {
Adam Cohen76078c42011-06-09 15:06:52 -07001039 return getItemsInReadingOrder(true);
1040 }
1041
1042 public ArrayList<View> getItemsInReadingOrder(boolean includeCurrentDragItem) {
Adam Cohen7c693212011-05-18 15:26:57 -07001043 if (mItemsInvalidated) {
1044 mItemsInReadingOrder.clear();
1045 for (int j = 0; j < mContent.getCountY(); j++) {
1046 for (int i = 0; i < mContent.getCountX(); i++) {
1047 View v = mContent.getChildAt(i, j);
1048 if (v != null) {
Adam Cohen76078c42011-06-09 15:06:52 -07001049 ShortcutInfo info = (ShortcutInfo) v.getTag();
1050 if (info != mCurrentDragInfo || includeCurrentDragItem) {
1051 mItemsInReadingOrder.add(v);
1052 }
Adam Cohen7c693212011-05-18 15:26:57 -07001053 }
1054 }
1055 }
1056 mItemsInvalidated = false;
1057 }
1058 return mItemsInReadingOrder;
1059 }
Adam Cohen8dfcba42011-07-07 16:38:18 -07001060
1061 public void getLocationInDragLayer(int[] loc) {
1062 mLauncher.getDragLayer().getLocationInDragLayer(this, loc);
1063 }
Adam Cohenea0818d2011-09-30 20:06:58 -07001064
1065 public void onFocusChange(View v, boolean hasFocus) {
1066 if (v == mFolderName && hasFocus) {
1067 startEditingFolderName();
1068 }
1069 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001070}