blob: fdde4d59332d62c3a1319cf9bf091a62874e47cb [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;
23import android.animation.ValueAnimator;
24import android.animation.ValueAnimator.AnimatorUpdateListener;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080025import android.content.Context;
Adam Cohen76fc0852011-06-17 13:26:23 -070026import android.content.res.Resources;
Romain Guyfb5411e2010-02-24 10:04:17 -080027import android.graphics.Rect;
Adam Cohenbadf71e2011-05-26 19:08:29 -070028import android.graphics.drawable.Drawable;
Adam Cohen7a14d0b2011-06-24 11:36:35 -070029import android.text.InputType;
Adam Cohene601a432011-07-26 21:51:30 -070030import android.text.Selection;
31import android.text.Spannable;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080032import android.util.AttributeSet;
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 Cohen2801caf2011-05-13 20:57:39 -070042import android.view.animation.AccelerateInterpolator;
43import android.view.animation.DecelerateInterpolator;
Adam Cohen76fc0852011-06-17 13:26:23 -070044import android.view.inputmethod.EditorInfo;
45import android.view.inputmethod.InputMethodManager;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080046import android.widget.LinearLayout;
Adam Cohendf2cc412011-04-27 16:56:57 -070047import android.widget.TextView;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080048
Romain Guyedcce092010-03-04 13:03:17 -080049import com.android.launcher.R;
Adam Cohena9cf38f2011-05-02 15:36:58 -070050import com.android.launcher2.FolderInfo.FolderListener;
Romain Guyedcce092010-03-04 13:03:17 -080051
Adam Cohenc0dcf592011-06-01 15:30:43 -070052import java.util.ArrayList;
53
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
Adam Cohendf2cc412011-04-27 16:56:57 -070061 private static final String TAG = "Launcher.Folder";
62
Adam Cohen4eac29a2011-07-11 17:53:37 -070063 protected DragController mDragController;
64 protected Launcher mLauncher;
65 protected FolderInfo mInfo;
66
Adam Cohendf2cc412011-04-27 16:56:57 -070067 static final int STATE_NONE = -1;
68 static final int STATE_SMALL = 0;
69 static final int STATE_ANIMATING = 1;
70 static final int STATE_OPEN = 2;
71
72 private int mExpandDuration;
73 protected CellLayout mContent;
74 private final LayoutInflater mInflater;
75 private final IconCache mIconCache;
76 private int mState = STATE_NONE;
Adam Cohen2801caf2011-05-13 20:57:39 -070077 private static final int FULL_GROW = 0;
78 private static final int PARTIAL_GROW = 1;
Adam Cohenbfbfd262011-06-13 16:55:12 -070079 private static final int REORDER_ANIMATION_DURATION = 230;
80 private static final int ON_EXIT_CLOSE_DELAY = 800;
Adam Cohen2801caf2011-05-13 20:57:39 -070081 private int mMode = PARTIAL_GROW;
82 private boolean mRearrangeOnClose = false;
83 private FolderIcon mFolderIcon;
84 private int mMaxCountX;
85 private int mMaxCountY;
Adam Cohen78dc83e2011-11-15 17:10:00 -080086 private int mMaxNumItems;
Adam Cohen2801caf2011-05-13 20:57:39 -070087 private Rect mNewSize = new Rect();
Adam Cohen3e8f8112011-07-02 18:03:00 -070088 private Rect mIconRect = new Rect();
Adam Cohen7c693212011-05-18 15:26:57 -070089 private ArrayList<View> mItemsInReadingOrder = new ArrayList<View>();
Adam Cohenbadf71e2011-05-26 19:08:29 -070090 private Drawable mIconDrawable;
Adam Cohen7c693212011-05-18 15:26:57 -070091 boolean mItemsInvalidated = false;
Adam Cohenbfbfd262011-06-13 16:55:12 -070092 private ShortcutInfo mCurrentDragInfo;
93 private View mCurrentDragView;
94 boolean mSuppressOnAdd = false;
95 private int[] mTargetCell = new int[2];
96 private int[] mPreviousTargetCell = new int[2];
97 private int[] mEmptyCell = new int[2];
98 private Alarm mReorderAlarm = new Alarm();
99 private Alarm mOnExitAlarm = new Alarm();
Adam Cohen76fc0852011-06-17 13:26:23 -0700100 private int mFolderNameHeight;
Adam Cohen8e776a62011-06-28 18:10:06 -0700101 private Rect mTempRect = new Rect();
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700102 private boolean mDragInProgress = false;
103 private boolean mDeleteFolderOnDropCompleted = false;
104 private boolean mSuppressFolderDeletion = false;
Adam Cohen05e0f402011-08-01 12:12:49 -0700105 private boolean mItemAddedBackToSelfViaIcon = false;
Adam Cohenac56cff2011-09-28 20:45:37 -0700106 FolderEditText mFolderName;
Adam Cohen228da5a2011-07-27 22:23:47 -0700107
Adam Cohen76fc0852011-06-17 13:26:23 -0700108 private boolean mIsEditingName = false;
109 private InputMethodManager mInputMethodManager;
Adam Cohendf2cc412011-04-27 16:56:57 -0700110
Adam Cohena65beee2011-06-27 21:32:23 -0700111 private static String sDefaultFolderName;
112 private static String sHintText;
113
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800114 /**
115 * Used to inflate the Workspace from XML.
116 *
117 * @param context The application's context.
118 * @param attrs The attribtues set containing the Workspace's customization values.
119 */
120 public Folder(Context context, AttributeSet attrs) {
121 super(context, attrs);
122 setAlwaysDrawnWithCacheEnabled(false);
Adam Cohendf2cc412011-04-27 16:56:57 -0700123 mInflater = LayoutInflater.from(context);
124 mIconCache = ((LauncherApplication)context.getApplicationContext()).getIconCache();
Adam Cohen78dc83e2011-11-15 17:10:00 -0800125
126 Resources res = getResources();
127 mMaxCountX = res.getInteger(R.integer.folder_max_count_x);
128 mMaxCountY = res.getInteger(R.integer.folder_max_count_y);
129 mMaxNumItems = res.getInteger(R.integer.folder_max_num_items);
130 if (mMaxCountX < 0 || mMaxCountY < 0 || mMaxNumItems < 0) {
131 mMaxCountX = LauncherModel.getCellCountX();
132 mMaxCountY = LauncherModel.getCellCountY();
133 mMaxNumItems = mMaxCountX * mMaxCountY;
134 }
Adam Cohen76fc0852011-06-17 13:26:23 -0700135
136 mInputMethodManager = (InputMethodManager)
137 mContext.getSystemService(Context.INPUT_METHOD_SERVICE);
138
Adam Cohen76fc0852011-06-17 13:26:23 -0700139 mExpandDuration = res.getInteger(R.integer.config_folderAnimDuration);
Adam Cohen4ef610f2011-06-21 22:37:36 -0700140
141 if (sDefaultFolderName == null) {
142 sDefaultFolderName = res.getString(R.string.folder_name);
143 }
Adam Cohena65beee2011-06-27 21:32:23 -0700144 if (sHintText == null) {
145 sHintText = res.getString(R.string.folder_hint_text);
146 }
Adam Cohen4eac29a2011-07-11 17:53:37 -0700147 mLauncher = (Launcher) context;
Adam Cohenac56cff2011-09-28 20:45:37 -0700148 // We need this view to be focusable in touch mode so that when text editing of the folder
149 // name is complete, we have something to focus on, thus hiding the cursor and giving
150 // reliable behvior when clicking the text field (since it will always gain focus on click).
151 setFocusableInTouchMode(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800152 }
153
154 @Override
155 protected void onFinishInflate() {
156 super.onFinishInflate();
Adam Cohendf2cc412011-04-27 16:56:57 -0700157 mContent = (CellLayout) findViewById(R.id.folder_content);
Adam Cohen2801caf2011-05-13 20:57:39 -0700158 mContent.setGridSize(0, 0);
Adam Cohenb970a8a2011-11-18 16:29:58 -0800159 mContent.getChildrenLayout().setMotionEventSplittingEnabled(false);
Adam Cohenac56cff2011-09-28 20:45:37 -0700160 mFolderName = (FolderEditText) findViewById(R.id.folder_name);
161 mFolderName.setFolder(this);
Adam Cohenea0818d2011-09-30 20:06:58 -0700162 mFolderName.setOnFocusChangeListener(this);
Adam Cohen76fc0852011-06-17 13:26:23 -0700163
164 // We find out how tall the text view wants to be (it is set to wrap_content), so that
165 // we can allocate the appropriate amount of space for it.
166 int measureSpec = MeasureSpec.UNSPECIFIED;
167 mFolderName.measure(measureSpec, measureSpec);
168 mFolderNameHeight = mFolderName.getMeasuredHeight();
169
170 // We disable action mode for now since it messes up the view on phones
171 mFolderName.setCustomSelectionActionModeCallback(mActionModeCallback);
Adam Cohen76fc0852011-06-17 13:26:23 -0700172 mFolderName.setOnEditorActionListener(this);
Adam Cohen4ef610f2011-06-21 22:37:36 -0700173 mFolderName.setSelectAllOnFocus(true);
Adam Cohen7a14d0b2011-06-24 11:36:35 -0700174 mFolderName.setInputType(mFolderName.getInputType() |
175 InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS | InputType.TYPE_TEXT_FLAG_CAP_WORDS);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800176 }
Adam Cohen2801caf2011-05-13 20:57:39 -0700177
Adam Cohen76fc0852011-06-17 13:26:23 -0700178 private ActionMode.Callback mActionModeCallback = new ActionMode.Callback() {
179 public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
180 return false;
181 }
182
183 public boolean onCreateActionMode(ActionMode mode, Menu menu) {
184 return false;
185 }
186
187 public void onDestroyActionMode(ActionMode mode) {
188 }
189
190 public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
191 return false;
192 }
193 };
194
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800195 public void onClick(View v) {
Adam Cohendf2cc412011-04-27 16:56:57 -0700196 Object tag = v.getTag();
197 if (tag instanceof ShortcutInfo) {
198 // refactor this code from Folder
199 ShortcutInfo item = (ShortcutInfo) tag;
200 int[] pos = new int[2];
201 v.getLocationOnScreen(pos);
202 item.intent.setSourceBounds(new Rect(pos[0], pos[1],
203 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
204 mLauncher.startActivitySafely(item.intent, item);
Adam Cohendf2cc412011-04-27 16:56:57 -0700205 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800206 }
207
208 public boolean onLongClick(View v) {
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,
259 String.format(mContext.getString(R.string.folder_renamed), newTitle));
260 }
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
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800314 void bind(FolderInfo info) {
315 mInfo = info;
Adam Cohendf2cc412011-04-27 16:56:57 -0700316 ArrayList<ShortcutInfo> children = info.contents;
Adam Cohenc508b2d2011-06-28 14:41:44 -0700317 ArrayList<ShortcutInfo> overflow = new ArrayList<ShortcutInfo>();
Adam Cohen7c693212011-05-18 15:26:57 -0700318 setupContentForNumItems(children.size());
Adam Cohen0057bbc2011-08-12 18:30:51 -0700319 int count = 0;
Adam Cohendf2cc412011-04-27 16:56:57 -0700320 for (int i = 0; i < children.size(); i++) {
321 ShortcutInfo child = (ShortcutInfo) children.get(i);
Adam Cohenc508b2d2011-06-28 14:41:44 -0700322 if (!createAndAddShortcut(child)) {
323 overflow.add(child);
Adam Cohen0057bbc2011-08-12 18:30:51 -0700324 } else {
325 count++;
Adam Cohenc508b2d2011-06-28 14:41:44 -0700326 }
Adam Cohendf2cc412011-04-27 16:56:57 -0700327 }
Adam Cohenc508b2d2011-06-28 14:41:44 -0700328
Adam Cohen0057bbc2011-08-12 18:30:51 -0700329 // We rearrange the items in case there are any empty gaps
330 setupContentForNumItems(count);
331
Adam Cohenc508b2d2011-06-28 14:41:44 -0700332 // If our folder has too many items we prune them from the list. This is an issue
333 // when upgrading from the old Folders implementation which could contain an unlimited
334 // number of items.
335 for (ShortcutInfo item: overflow) {
336 mInfo.remove(item);
337 LauncherModel.deleteItemFromDatabase(mLauncher, item);
338 }
339
Adam Cohen4dbe6d92011-05-18 17:14:15 -0700340 mItemsInvalidated = true;
Adam Cohenac56cff2011-09-28 20:45:37 -0700341 updateTextViewFocus();
Adam Cohena9cf38f2011-05-02 15:36:58 -0700342 mInfo.addListener(this);
Adam Cohen4ef610f2011-06-21 22:37:36 -0700343
Adam Cohenafb01ee2011-06-23 15:38:03 -0700344 if (!sDefaultFolderName.contentEquals(mInfo.title)) {
Adam Cohen4ef610f2011-06-21 22:37:36 -0700345 mFolderName.setText(mInfo.title);
346 } else {
347 mFolderName.setText("");
348 }
Adam Cohendf2cc412011-04-27 16:56:57 -0700349 }
350
351 /**
352 * Creates a new UserFolder, inflated from R.layout.user_folder.
353 *
354 * @param context The application's context.
355 *
356 * @return A new UserFolder.
357 */
358 static Folder fromXml(Context context) {
359 return (Folder) LayoutInflater.from(context).inflate(R.layout.user_folder, null);
360 }
361
362 /**
363 * This method is intended to make the UserFolder to be visually identical in size and position
364 * to its associated FolderIcon. This allows for a seamless transition into the expanded state.
365 */
366 private void positionAndSizeAsIcon() {
Adam Cohen3e8f8112011-07-02 18:03:00 -0700367 if (!(getParent() instanceof DragLayer)) return;
Adam Cohendf2cc412011-04-27 16:56:57 -0700368
Adam Cohen8e776a62011-06-28 18:10:06 -0700369 DragLayer.LayoutParams lp = (DragLayer.LayoutParams) getLayoutParams();
Adam Cohendf2cc412011-04-27 16:56:57 -0700370
Adam Cohen2801caf2011-05-13 20:57:39 -0700371 if (mMode == PARTIAL_GROW) {
372 setScaleX(0.8f);
373 setScaleY(0.8f);
374 setAlpha(0f);
375 } else {
Adam Cohen3e8f8112011-07-02 18:03:00 -0700376 mLauncher.getDragLayer().getDescendantRectRelativeToSelf(mFolderIcon, mIconRect);
377 lp.width = mIconRect.width();
378 lp.height = mIconRect.height();
379 lp.x = mIconRect.left;
380 lp.y = mIconRect.top;
Adam Cohen2801caf2011-05-13 20:57:39 -0700381 mContent.setAlpha(0);
382 }
Adam Cohendf2cc412011-04-27 16:56:57 -0700383 mState = STATE_SMALL;
384 }
385
386 public void animateOpen() {
Adam Cohen3e8f8112011-07-02 18:03:00 -0700387 positionAndSizeAsIcon();
388
Adam Cohen8e776a62011-06-28 18:10:06 -0700389 if (!(getParent() instanceof DragLayer)) return;
Adam Cohendf2cc412011-04-27 16:56:57 -0700390
Adam Cohen2801caf2011-05-13 20:57:39 -0700391 ObjectAnimator oa;
Adam Cohen8e776a62011-06-28 18:10:06 -0700392 DragLayer.LayoutParams lp = (DragLayer.LayoutParams) getLayoutParams();
Adam Cohendf2cc412011-04-27 16:56:57 -0700393
Adam Cohen2801caf2011-05-13 20:57:39 -0700394 centerAboutIcon();
395 if (mMode == PARTIAL_GROW) {
396 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1);
397 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.0f);
398 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.0f);
399 oa = ObjectAnimator.ofPropertyValuesHolder(this, alpha, scaleX, scaleY);
400 } else {
401 PropertyValuesHolder width = PropertyValuesHolder.ofInt("width", mNewSize.width());
402 PropertyValuesHolder height = PropertyValuesHolder.ofInt("height", mNewSize.height());
403 PropertyValuesHolder x = PropertyValuesHolder.ofInt("x", mNewSize.left);
404 PropertyValuesHolder y = PropertyValuesHolder.ofInt("y", mNewSize.top);
405 oa = ObjectAnimator.ofPropertyValuesHolder(lp, width, height, x, y);
406 oa.addUpdateListener(new AnimatorUpdateListener() {
407 public void onAnimationUpdate(ValueAnimator animation) {
408 requestLayout();
409 }
410 });
Adam Cohendf2cc412011-04-27 16:56:57 -0700411
Adam Cohen2801caf2011-05-13 20:57:39 -0700412 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1.0f);
413 ObjectAnimator alphaOa = ObjectAnimator.ofPropertyValuesHolder(mContent, alpha);
414 alphaOa.setDuration(mExpandDuration);
415 alphaOa.setInterpolator(new AccelerateInterpolator(2.0f));
416 alphaOa.start();
417 }
Adam Cohendf2cc412011-04-27 16:56:57 -0700418
Adam Cohen2801caf2011-05-13 20:57:39 -0700419 oa.addListener(new AnimatorListenerAdapter() {
Adam Cohendf2cc412011-04-27 16:56:57 -0700420 @Override
421 public void onAnimationStart(Animator animation) {
Adam Cohen3371da02011-10-25 21:38:29 -0700422 sendCustomAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED,
423 String.format(mContext.getString(R.string.folder_opened),
424 mContent.getCountX(), mContent.getCountY()));
Adam Cohendf2cc412011-04-27 16:56:57 -0700425 mState = STATE_ANIMATING;
426 }
427 @Override
428 public void onAnimationEnd(Animator animation) {
Adam Cohen2801caf2011-05-13 20:57:39 -0700429 mState = STATE_OPEN;
Winson Chung7a74ac92011-09-20 17:43:51 -0700430
431 Cling cling = mLauncher.showFirstRunFoldersCling();
432 if (cling != null) {
433 cling.bringToFront();
434 }
Adam Cohenac56cff2011-09-28 20:45:37 -0700435 setFocusOnFirstChild();
Adam Cohendf2cc412011-04-27 16:56:57 -0700436 }
437 });
Adam Cohen2801caf2011-05-13 20:57:39 -0700438 oa.setDuration(mExpandDuration);
439 oa.start();
Adam Cohendf2cc412011-04-27 16:56:57 -0700440 }
441
Adam Cohen3371da02011-10-25 21:38:29 -0700442 private void sendCustomAccessibilityEvent(int type, String text) {
443 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
444 AccessibilityEvent event = AccessibilityEvent.obtain(type);
445 onInitializeAccessibilityEvent(event);
446 event.getText().add(text);
447 AccessibilityManager.getInstance(mContext).sendAccessibilityEvent(event);
448 }
449 }
450
Adam Cohenac56cff2011-09-28 20:45:37 -0700451 private void setFocusOnFirstChild() {
452 View firstChild = mContent.getChildAt(0, 0);
453 if (firstChild != null) {
454 firstChild.requestFocus();
455 }
456 }
457
Adam Cohendf2cc412011-04-27 16:56:57 -0700458 public void animateClosed() {
Adam Cohen8e776a62011-06-28 18:10:06 -0700459 if (!(getParent() instanceof DragLayer)) return;
Adam Cohendf2cc412011-04-27 16:56:57 -0700460
Adam Cohen2801caf2011-05-13 20:57:39 -0700461 ObjectAnimator oa;
Adam Cohen2801caf2011-05-13 20:57:39 -0700462 if (mMode == PARTIAL_GROW) {
463 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0);
464 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 0.9f);
465 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 0.9f);
466 oa = ObjectAnimator.ofPropertyValuesHolder(this, alpha, scaleX, scaleY);
467 } else {
Adam Cohen8e776a62011-06-28 18:10:06 -0700468 DragLayer.LayoutParams lp = (DragLayer.LayoutParams) getLayoutParams();
Adam Cohendf2cc412011-04-27 16:56:57 -0700469
Adam Cohen3e8f8112011-07-02 18:03:00 -0700470 PropertyValuesHolder width = PropertyValuesHolder.ofInt("width", mIconRect.width());
471 PropertyValuesHolder height = PropertyValuesHolder.ofInt("height", mIconRect.height());
472 PropertyValuesHolder x = PropertyValuesHolder.ofInt("x", mIconRect.left);
473 PropertyValuesHolder y = PropertyValuesHolder.ofInt("y", mIconRect.top);
Adam Cohen2801caf2011-05-13 20:57:39 -0700474 oa = ObjectAnimator.ofPropertyValuesHolder(lp, width, height, x, y);
475 oa.addUpdateListener(new AnimatorUpdateListener() {
476 public void onAnimationUpdate(ValueAnimator animation) {
477 requestLayout();
478 }
479 });
Adam Cohendf2cc412011-04-27 16:56:57 -0700480
Adam Cohen2801caf2011-05-13 20:57:39 -0700481 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0f);
482 ObjectAnimator alphaOa = ObjectAnimator.ofPropertyValuesHolder(mContent, alpha);
483 alphaOa.setDuration(mExpandDuration);
484 alphaOa.setInterpolator(new DecelerateInterpolator(2.0f));
485 alphaOa.start();
486 }
Adam Cohendf2cc412011-04-27 16:56:57 -0700487
Adam Cohen2801caf2011-05-13 20:57:39 -0700488 oa.addListener(new AnimatorListenerAdapter() {
Adam Cohendf2cc412011-04-27 16:56:57 -0700489 @Override
490 public void onAnimationEnd(Animator animation) {
Adam Cohen2801caf2011-05-13 20:57:39 -0700491 onCloseComplete();
Adam Cohen2801caf2011-05-13 20:57:39 -0700492 mState = STATE_SMALL;
Adam Cohendf2cc412011-04-27 16:56:57 -0700493 }
494 @Override
495 public void onAnimationStart(Animator animation) {
Adam Cohen3371da02011-10-25 21:38:29 -0700496 sendCustomAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED,
497 mContext.getString(R.string.folder_closed));
Adam Cohendf2cc412011-04-27 16:56:57 -0700498 mState = STATE_ANIMATING;
499 }
500 });
Adam Cohen2801caf2011-05-13 20:57:39 -0700501 oa.setDuration(mExpandDuration);
502 oa.start();
Adam Cohendf2cc412011-04-27 16:56:57 -0700503 }
504
505 void notifyDataSetChanged() {
506 // recreate all the children if the data set changes under us. We may want to do this more
507 // intelligently (ie just removing the views that should no longer exist)
508 mContent.removeAllViewsInLayout();
509 bind(mInfo);
510 }
511
Adam Cohencb3382b2011-05-24 14:07:08 -0700512 public boolean acceptDrop(DragObject d) {
513 final ItemInfo item = (ItemInfo) d.dragInfo;
Adam Cohendf2cc412011-04-27 16:56:57 -0700514 final int itemType = item.itemType;
Adam Cohen2801caf2011-05-13 20:57:39 -0700515 return ((itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION ||
516 itemType == LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT) &&
517 !isFull());
Adam Cohendf2cc412011-04-27 16:56:57 -0700518 }
519
Adam Cohendf2cc412011-04-27 16:56:57 -0700520 protected boolean findAndSetEmptyCells(ShortcutInfo item) {
521 int[] emptyCell = new int[2];
522 if (mContent.findCellForSpan(emptyCell, item.spanX, item.spanY)) {
523 item.cellX = emptyCell[0];
524 item.cellY = emptyCell[1];
Adam Cohendf2cc412011-04-27 16:56:57 -0700525 return true;
526 } else {
527 return false;
528 }
529 }
530
Adam Cohenc508b2d2011-06-28 14:41:44 -0700531 protected boolean createAndAddShortcut(ShortcutInfo item) {
Adam Cohendf2cc412011-04-27 16:56:57 -0700532 final TextView textView =
Adam Cohene87b9242011-06-29 14:01:26 -0700533 (TextView) mInflater.inflate(R.layout.application, this, false);
Adam Cohendf2cc412011-04-27 16:56:57 -0700534 textView.setCompoundDrawablesWithIntrinsicBounds(null,
535 new FastBitmapDrawable(item.getIcon(mIconCache)), null, null);
536 textView.setText(item.title);
537 textView.setTag(item);
538
539 textView.setOnClickListener(this);
540 textView.setOnLongClickListener(this);
541
Adam Cohenc508b2d2011-06-28 14:41:44 -0700542 // We need to check here to verify that the given item's location isn't already occupied
543 // by another item. If it is, we need to find the next available slot and assign
544 // it that position. This is an issue when upgrading from the old Folders implementation
545 // which could contain an unlimited number of items.
Adam Cohen0057bbc2011-08-12 18:30:51 -0700546 if (mContent.getChildAt(item.cellX, item.cellY) != null || item.cellX < 0 || item.cellY < 0
547 || item.cellX >= mContent.getCountX() || item.cellY >= mContent.getCountY()) {
Adam Cohenc508b2d2011-06-28 14:41:44 -0700548 if (!findAndSetEmptyCells(item)) {
549 return false;
550 }
551 }
552
Adam Cohendf2cc412011-04-27 16:56:57 -0700553 CellLayout.LayoutParams lp =
554 new CellLayout.LayoutParams(item.cellX, item.cellY, item.spanX, item.spanY);
555 boolean insert = false;
Adam Cohenac56cff2011-09-28 20:45:37 -0700556 textView.setOnKeyListener(new FolderKeyEventListener());
Adam Cohendf2cc412011-04-27 16:56:57 -0700557 mContent.addViewToCellLayout(textView, insert ? 0 : -1, (int)item.id, lp, true);
Adam Cohenc508b2d2011-06-28 14:41:44 -0700558 return true;
Adam Cohendf2cc412011-04-27 16:56:57 -0700559 }
560
Adam Cohencb3382b2011-05-24 14:07:08 -0700561 public void onDragEnter(DragObject d) {
Adam Cohenbfbfd262011-06-13 16:55:12 -0700562 mPreviousTargetCell[0] = -1;
563 mPreviousTargetCell[1] = -1;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700564 mOnExitAlarm.cancelAlarm();
565 }
566
567 OnAlarmListener mReorderAlarmListener = new OnAlarmListener() {
568 public void onAlarm(Alarm alarm) {
569 realTimeReorder(mEmptyCell, mTargetCell);
570 }
571 };
572
573 boolean readingOrderGreaterThan(int[] v1, int[] v2) {
574 if (v1[1] > v2[1] || (v1[1] == v2[1] && v1[0] > v2[0])) {
575 return true;
576 } else {
577 return false;
578 }
579 }
580
581 private void realTimeReorder(int[] empty, int[] target) {
582 boolean wrap;
583 int startX;
584 int endX;
585 int startY;
Adam Cohen76fc0852011-06-17 13:26:23 -0700586 int delay = 0;
587 float delayAmount = 30;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700588 if (readingOrderGreaterThan(target, empty)) {
589 wrap = empty[0] >= mContent.getCountX() - 1;
590 startY = wrap ? empty[1] + 1 : empty[1];
591 for (int y = startY; y <= target[1]; y++) {
592 startX = y == empty[1] ? empty[0] + 1 : 0;
593 endX = y < target[1] ? mContent.getCountX() - 1 : target[0];
594 for (int x = startX; x <= endX; x++) {
595 View v = mContent.getChildAt(x,y);
596 if (mContent.animateChildToPosition(v, empty[0], empty[1],
Adam Cohen76fc0852011-06-17 13:26:23 -0700597 REORDER_ANIMATION_DURATION, delay)) {
Adam Cohenbfbfd262011-06-13 16:55:12 -0700598 empty[0] = x;
599 empty[1] = y;
Adam Cohen76fc0852011-06-17 13:26:23 -0700600 delay += delayAmount;
601 delayAmount *= 0.9;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700602 }
603 }
604 }
605 } else {
606 wrap = empty[0] == 0;
607 startY = wrap ? empty[1] - 1 : empty[1];
608 for (int y = startY; y >= target[1]; y--) {
609 startX = y == empty[1] ? empty[0] - 1 : mContent.getCountX() - 1;
610 endX = y > target[1] ? 0 : target[0];
611 for (int x = startX; x >= endX; x--) {
612 View v = mContent.getChildAt(x,y);
613 if (mContent.animateChildToPosition(v, empty[0], empty[1],
Adam Cohen76fc0852011-06-17 13:26:23 -0700614 REORDER_ANIMATION_DURATION, delay)) {
Adam Cohenbfbfd262011-06-13 16:55:12 -0700615 empty[0] = x;
616 empty[1] = y;
Adam Cohen76fc0852011-06-17 13:26:23 -0700617 delay += delayAmount;
618 delayAmount *= 0.9;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700619 }
620 }
621 }
622 }
Adam Cohendf2cc412011-04-27 16:56:57 -0700623 }
624
Adam Cohencb3382b2011-05-24 14:07:08 -0700625 public void onDragOver(DragObject d) {
Adam Cohenbfbfd262011-06-13 16:55:12 -0700626 float[] r = getDragViewVisualCenter(d.x, d.y, d.xOffset, d.yOffset, d.dragView, null);
627 mTargetCell = mContent.findNearestArea((int) r[0], (int) r[1], 1, 1, mTargetCell);
628
629 if (mTargetCell[0] != mPreviousTargetCell[0] || mTargetCell[1] != mPreviousTargetCell[1]) {
630 mReorderAlarm.cancelAlarm();
631 mReorderAlarm.setOnAlarmListener(mReorderAlarmListener);
632 mReorderAlarm.setAlarm(150);
633 mPreviousTargetCell[0] = mTargetCell[0];
634 mPreviousTargetCell[1] = mTargetCell[1];
635 }
Adam Cohendf2cc412011-04-27 16:56:57 -0700636 }
637
Adam Cohenbfbfd262011-06-13 16:55:12 -0700638 // This is used to compute the visual center of the dragView. The idea is that
639 // the visual center represents the user's interpretation of where the item is, and hence
640 // is the appropriate point to use when determining drop location.
641 private float[] getDragViewVisualCenter(int x, int y, int xOffset, int yOffset,
642 DragView dragView, float[] recycle) {
643 float res[];
644 if (recycle == null) {
645 res = new float[2];
646 } else {
647 res = recycle;
648 }
649
650 // These represent the visual top and left of drag view if a dragRect was provided.
651 // If a dragRect was not provided, then they correspond to the actual view left and
652 // top, as the dragRect is in that case taken to be the entire dragView.
653 // R.dimen.dragViewOffsetY.
654 int left = x - xOffset;
655 int top = y - yOffset;
656
657 // In order to find the visual center, we shift by half the dragRect
658 res[0] = left + dragView.getDragRegion().width() / 2;
659 res[1] = top + dragView.getDragRegion().height() / 2;
660
661 return res;
662 }
663
664 OnAlarmListener mOnExitAlarmListener = new OnAlarmListener() {
665 public void onAlarm(Alarm alarm) {
Adam Cohen3e8f8112011-07-02 18:03:00 -0700666 completeDragExit();
Adam Cohenbfbfd262011-06-13 16:55:12 -0700667 }
668 };
669
Adam Cohen95bb8002011-07-03 23:40:28 -0700670 public void completeDragExit() {
Adam Cohen3e8f8112011-07-02 18:03:00 -0700671 mLauncher.closeFolder();
672 mCurrentDragInfo = null;
673 mCurrentDragView = null;
674 mSuppressOnAdd = false;
675 mRearrangeOnClose = true;
676 }
677
Adam Cohencb3382b2011-05-24 14:07:08 -0700678 public void onDragExit(DragObject d) {
Adam Cohenbfbfd262011-06-13 16:55:12 -0700679 // We only close the folder if this is a true drag exit, ie. not because a drop
680 // has occurred above the folder.
681 if (!d.dragComplete) {
682 mOnExitAlarm.setOnAlarmListener(mOnExitAlarmListener);
683 mOnExitAlarm.setAlarm(ON_EXIT_CLOSE_DELAY);
684 }
685 mReorderAlarm.cancelAlarm();
Adam Cohen2801caf2011-05-13 20:57:39 -0700686 }
687
Adam Cohenc0dcf592011-06-01 15:30:43 -0700688 public void onDropCompleted(View target, DragObject d, boolean success) {
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700689 if (success) {
Adam Cohen05e0f402011-08-01 12:12:49 -0700690 if (mDeleteFolderOnDropCompleted && !mItemAddedBackToSelfViaIcon) {
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700691 replaceFolderWithFinalItem();
692 }
693 } else {
694 // The drag failed, we need to return the item to the folder
695 mFolderIcon.onDrop(d);
696
697 // We're going to trigger a "closeFolder" which may occur before this item has
698 // been added back to the folder -- this could cause the folder to be deleted
699 if (mOnExitAlarm.alarmPending()) {
700 mSuppressFolderDeletion = true;
701 }
702 }
703
704 if (target != this) {
705 if (mOnExitAlarm.alarmPending()) {
706 mOnExitAlarm.cancelAlarm();
707 completeDragExit();
708 }
709 }
710 mDeleteFolderOnDropCompleted = false;
711 mDragInProgress = false;
Adam Cohen05e0f402011-08-01 12:12:49 -0700712 mItemAddedBackToSelfViaIcon = false;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700713 mCurrentDragInfo = null;
714 mCurrentDragView = null;
715 mSuppressOnAdd = false;
Adam Cohen4045eb72011-10-06 11:44:26 -0700716
717 // Reordering may have occured, and we need to save the new item locations. We do this once
718 // at the end to prevent unnecessary database operations.
719 updateItemLocationsInDatabase();
720 }
721
722 private void updateItemLocationsInDatabase() {
723 ArrayList<View> list = getItemsInReadingOrder();
724 for (int i = 0; i < list.size(); i++) {
725 View v = list.get(i);
726 ItemInfo info = (ItemInfo) v.getTag();
727 LauncherModel.moveItemInDatabase(mLauncher, info, mInfo.id, 0,
728 info.cellX, info.cellY);
729 }
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700730 }
Adam Cohen228da5a2011-07-27 22:23:47 -0700731
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700732 public void notifyDrop() {
733 if (mDragInProgress) {
Adam Cohen05e0f402011-08-01 12:12:49 -0700734 mItemAddedBackToSelfViaIcon = true;
Adam Cohen76078c42011-06-09 15:06:52 -0700735 }
Adam Cohendf2cc412011-04-27 16:56:57 -0700736 }
737
738 public boolean isDropEnabled() {
739 return true;
740 }
741
Adam Cohencb3382b2011-05-24 14:07:08 -0700742 public DropTarget getDropTargetDelegate(DragObject d) {
Adam Cohendf2cc412011-04-27 16:56:57 -0700743 return null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800744 }
Adam Cohena9cf38f2011-05-02 15:36:58 -0700745
Adam Cohen4045eb72011-10-06 11:44:26 -0700746 private void setupContentDimensions(int count) {
Adam Cohen2801caf2011-05-13 20:57:39 -0700747 ArrayList<View> list = getItemsInReadingOrder();
748
749 int countX = mContent.getCountX();
750 int countY = mContent.getCountY();
Adam Cohen7c693212011-05-18 15:26:57 -0700751 boolean done = false;
Adam Cohen2801caf2011-05-13 20:57:39 -0700752
Adam Cohen7c693212011-05-18 15:26:57 -0700753 while (!done) {
754 int oldCountX = countX;
755 int oldCountY = countY;
756 if (countX * countY < count) {
757 // Current grid is too small, expand it
Adam Cohen78dc83e2011-11-15 17:10:00 -0800758 if ((countX <= countY || countY == mMaxCountY) && countX < mMaxCountX) {
Adam Cohen7c693212011-05-18 15:26:57 -0700759 countX++;
760 } else if (countY < mMaxCountY) {
761 countY++;
762 }
763 if (countY == 0) countY++;
764 } else if ((countY - 1) * countX >= count && countY >= countX) {
765 countY = Math.max(0, countY - 1);
766 } else if ((countX - 1) * countY >= count) {
767 countX = Math.max(0, countX - 1);
Adam Cohen2801caf2011-05-13 20:57:39 -0700768 }
Adam Cohen7c693212011-05-18 15:26:57 -0700769 done = countX == oldCountX && countY == oldCountY;
Adam Cohen2801caf2011-05-13 20:57:39 -0700770 }
Adam Cohen7c693212011-05-18 15:26:57 -0700771 mContent.setGridSize(countX, countY);
Adam Cohen2801caf2011-05-13 20:57:39 -0700772 arrangeChildren(list);
773 }
774
775 public boolean isFull() {
Adam Cohen78dc83e2011-11-15 17:10:00 -0800776 return getItemCount() >= mMaxNumItems;
Adam Cohen2801caf2011-05-13 20:57:39 -0700777 }
778
779 private void centerAboutIcon() {
Adam Cohen8e776a62011-06-28 18:10:06 -0700780 DragLayer.LayoutParams lp = (DragLayer.LayoutParams) getLayoutParams();
Adam Cohen2801caf2011-05-13 20:57:39 -0700781
782 int width = getPaddingLeft() + getPaddingRight() + mContent.getDesiredWidth();
Adam Cohenf4bb1cd2011-07-22 14:36:03 -0700783 int height = getPaddingTop() + getPaddingBottom() + mContent.getDesiredHeight()
784 + mFolderNameHeight;
Adam Cohen8e776a62011-06-28 18:10:06 -0700785 DragLayer parent = (DragLayer) mLauncher.findViewById(R.id.drag_layer);
Adam Cohen2801caf2011-05-13 20:57:39 -0700786
Adam Cohen8e776a62011-06-28 18:10:06 -0700787 parent.getDescendantRectRelativeToSelf(mFolderIcon, mTempRect);
788
789 int centerX = mTempRect.centerX();
790 int centerY = mTempRect.centerY();
Adam Cohen2801caf2011-05-13 20:57:39 -0700791 int centeredLeft = centerX - width / 2;
792 int centeredTop = centerY - height / 2;
793
Adam Cohen35e7e642011-07-17 14:47:18 -0700794 // We first fetch the currently visible CellLayoutChildren
Winson Chung3d503fb2011-07-13 17:25:49 -0700795 CellLayout currentPage = mLauncher.getWorkspace().getCurrentDropLayout();
Adam Cohen35e7e642011-07-17 14:47:18 -0700796 CellLayoutChildren boundingLayout = currentPage.getChildrenLayout();
797 Rect bounds = new Rect();
798 parent.getDescendantRectRelativeToSelf(boundingLayout, bounds);
Adam Cohen2801caf2011-05-13 20:57:39 -0700799
Adam Cohen35e7e642011-07-17 14:47:18 -0700800 // We need to bound the folder to the currently visible CellLayoutChildren
801 int left = Math.min(Math.max(bounds.left, centeredLeft),
802 bounds.left + bounds.width() - width);
803 int top = Math.min(Math.max(bounds.top, centeredTop),
804 bounds.top + bounds.height() - height);
805 // If the folder doesn't fit within the bounds, center it about the desired bounds
806 if (width >= bounds.width()) {
807 left = bounds.left + (bounds.width() - width) / 2;
Adam Cohen0e4857c2011-06-30 17:05:14 -0700808 }
Adam Cohen35e7e642011-07-17 14:47:18 -0700809 if (height >= bounds.height()) {
810 top = bounds.top + (bounds.height() - height) / 2;
Adam Cohen0e4857c2011-06-30 17:05:14 -0700811 }
Adam Cohen2801caf2011-05-13 20:57:39 -0700812
813 int folderPivotX = width / 2 + (centeredLeft - left);
814 int folderPivotY = height / 2 + (centeredTop - top);
815 setPivotX(folderPivotX);
816 setPivotY(folderPivotY);
817 int folderIconPivotX = (int) (mFolderIcon.getMeasuredWidth() *
818 (1.0f * folderPivotX / width));
819 int folderIconPivotY = (int) (mFolderIcon.getMeasuredHeight() *
820 (1.0f * folderPivotY / height));
821 mFolderIcon.setPivotX(folderIconPivotX);
822 mFolderIcon.setPivotY(folderIconPivotY);
823
824 if (mMode == PARTIAL_GROW) {
825 lp.width = width;
826 lp.height = height;
827 lp.x = left;
828 lp.y = top;
829 } else {
830 mNewSize.set(left, top, left + width, top + height);
831 }
832 }
833
834 private void setupContentForNumItems(int count) {
Adam Cohen4045eb72011-10-06 11:44:26 -0700835 setupContentDimensions(count);
Adam Cohen2801caf2011-05-13 20:57:39 -0700836
Adam Cohen8e776a62011-06-28 18:10:06 -0700837 DragLayer.LayoutParams lp = (DragLayer.LayoutParams) getLayoutParams();
Adam Cohen2801caf2011-05-13 20:57:39 -0700838 if (lp == null) {
Adam Cohen8e776a62011-06-28 18:10:06 -0700839 lp = new DragLayer.LayoutParams(0, 0);
840 lp.customPosition = true;
Adam Cohen2801caf2011-05-13 20:57:39 -0700841 setLayoutParams(lp);
842 }
843 centerAboutIcon();
844 }
845
Adam Cohen234c4cd2011-07-17 21:03:04 -0700846 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
847 int width = getPaddingLeft() + getPaddingRight() + mContent.getDesiredWidth();
Adam Cohenf4bb1cd2011-07-22 14:36:03 -0700848 int height = getPaddingTop() + getPaddingBottom() + mContent.getDesiredHeight()
849 + mFolderNameHeight;
Adam Cohen234c4cd2011-07-17 21:03:04 -0700850
851 int contentWidthSpec = MeasureSpec.makeMeasureSpec(mContent.getDesiredWidth(),
852 MeasureSpec.EXACTLY);
853 int contentHeightSpec = MeasureSpec.makeMeasureSpec(mContent.getDesiredHeight(),
854 MeasureSpec.EXACTLY);
855 mContent.measure(contentWidthSpec, contentHeightSpec);
856
857 mFolderName.measure(contentWidthSpec,
858 MeasureSpec.makeMeasureSpec(mFolderNameHeight, MeasureSpec.EXACTLY));
859 setMeasuredDimension(width, height);
860 }
861
Adam Cohen2801caf2011-05-13 20:57:39 -0700862 private void arrangeChildren(ArrayList<View> list) {
863 int[] vacant = new int[2];
864 if (list == null) {
865 list = getItemsInReadingOrder();
866 }
867 mContent.removeAllViews();
868
869 for (int i = 0; i < list.size(); i++) {
870 View v = list.get(i);
871 mContent.getVacantCell(vacant, 1, 1);
872 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) v.getLayoutParams();
873 lp.cellX = vacant[0];
874 lp.cellY = vacant[1];
875 ItemInfo info = (ItemInfo) v.getTag();
Adam Cohen2792a332011-09-26 21:09:47 -0700876 if (info.cellX != vacant[0] || info.cellY != vacant[1]) {
877 info.cellX = vacant[0];
878 info.cellY = vacant[1];
879 LauncherModel.addOrMoveItemInDatabase(mLauncher, info, mInfo.id, 0,
880 info.cellX, info.cellY);
881 }
Adam Cohen2801caf2011-05-13 20:57:39 -0700882 boolean insert = false;
883 mContent.addViewToCellLayout(v, insert ? 0 : -1, (int)info.id, lp, true);
884 }
Adam Cohen7c693212011-05-18 15:26:57 -0700885 mItemsInvalidated = true;
Adam Cohen2801caf2011-05-13 20:57:39 -0700886 }
887
Adam Cohena9cf38f2011-05-02 15:36:58 -0700888 public int getItemCount() {
889 return mContent.getChildrenLayout().getChildCount();
890 }
891
892 public View getItemAt(int index) {
893 return mContent.getChildrenLayout().getChildAt(index);
894 }
895
Adam Cohen2801caf2011-05-13 20:57:39 -0700896 private void onCloseComplete() {
Adam Cohen05e0f402011-08-01 12:12:49 -0700897 DragLayer parent = (DragLayer) getParent();
Adam Cohen4554ee12011-08-03 16:13:21 -0700898 parent.removeView(this);
899 mDragController.removeDropTarget((DropTarget) this);
Adam Cohen05e0f402011-08-01 12:12:49 -0700900 clearFocus();
Adam Cohenac56cff2011-09-28 20:45:37 -0700901 mFolderIcon.requestFocus();
Adam Cohen05e0f402011-08-01 12:12:49 -0700902
Adam Cohen2801caf2011-05-13 20:57:39 -0700903 if (mRearrangeOnClose) {
904 setupContentForNumItems(getItemCount());
905 mRearrangeOnClose = false;
906 }
Adam Cohenafb01ee2011-06-23 15:38:03 -0700907 if (getItemCount() <= 1) {
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700908 if (!mDragInProgress && !mSuppressFolderDeletion) {
909 replaceFolderWithFinalItem();
910 } else if (mDragInProgress) {
911 mDeleteFolderOnDropCompleted = true;
912 }
Adam Cohenafb01ee2011-06-23 15:38:03 -0700913 }
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700914 mSuppressFolderDeletion = false;
Adam Cohenafb01ee2011-06-23 15:38:03 -0700915 }
916
917 private void replaceFolderWithFinalItem() {
918 ItemInfo finalItem = null;
919
920 if (getItemCount() == 1) {
921 finalItem = mInfo.contents.get(0);
922 }
923
924 // Remove the folder completely
Winson Chung3d503fb2011-07-13 17:25:49 -0700925 CellLayout cellLayout = mLauncher.getCellLayout(mInfo.container, mInfo.screen);
Adam Cohenafb01ee2011-06-23 15:38:03 -0700926 cellLayout.removeView(mFolderIcon);
927 if (mFolderIcon instanceof DropTarget) {
928 mDragController.removeDropTarget((DropTarget) mFolderIcon);
929 }
930 mLauncher.removeFolder(mInfo);
931
932 if (finalItem != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -0700933 LauncherModel.addOrMoveItemInDatabase(mLauncher, finalItem, mInfo.container,
934 mInfo.screen, mInfo.cellX, mInfo.cellY);
Adam Cohenafb01ee2011-06-23 15:38:03 -0700935 }
Adam Cohen716b51e2011-06-30 12:09:54 -0700936 LauncherModel.deleteItemFromDatabase(mLauncher, mInfo);
Adam Cohenafb01ee2011-06-23 15:38:03 -0700937
938 // Add the last remaining child to the workspace in place of the folder
939 if (finalItem != null) {
940 View child = mLauncher.createShortcut(R.layout.application, cellLayout,
941 (ShortcutInfo) finalItem);
942
Winson Chung3d503fb2011-07-13 17:25:49 -0700943 mLauncher.getWorkspace().addInScreen(child, mInfo.container, mInfo.screen, mInfo.cellX,
944 mInfo.cellY, mInfo.spanX, mInfo.spanY);
Adam Cohenafb01ee2011-06-23 15:38:03 -0700945 }
Adam Cohen2801caf2011-05-13 20:57:39 -0700946 }
947
Adam Cohenac56cff2011-09-28 20:45:37 -0700948 // This method keeps track of the last item in the folder for the purposes
949 // of keyboard focus
950 private void updateTextViewFocus() {
951 View lastChild = getItemAt(getItemCount() - 1);
952 getItemAt(getItemCount() - 1);
953 if (lastChild != null) {
954 mFolderName.setNextFocusDownId(lastChild.getId());
955 mFolderName.setNextFocusRightId(lastChild.getId());
956 mFolderName.setNextFocusLeftId(lastChild.getId());
957 mFolderName.setNextFocusUpId(lastChild.getId());
958 }
959 }
960
Adam Cohenbfbfd262011-06-13 16:55:12 -0700961 public void onDrop(DragObject d) {
962 ShortcutInfo item;
963 if (d.dragInfo instanceof ApplicationInfo) {
964 // Came from all apps -- make a copy
965 item = ((ApplicationInfo) d.dragInfo).makeShortcut();
966 item.spanX = 1;
967 item.spanY = 1;
968 } else {
969 item = (ShortcutInfo) d.dragInfo;
970 }
Adam Cohen05e0f402011-08-01 12:12:49 -0700971 // Dragged from self onto self, currently this is the only path possible, however
972 // we keep this as a distinct code path.
Adam Cohenbfbfd262011-06-13 16:55:12 -0700973 if (item == mCurrentDragInfo) {
974 ShortcutInfo si = (ShortcutInfo) mCurrentDragView.getTag();
975 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) mCurrentDragView.getLayoutParams();
976 si.cellX = lp.cellX = mEmptyCell[0];
977 si.cellX = lp.cellY = mEmptyCell[1];
978 mContent.addViewToCellLayout(mCurrentDragView, -1, (int)item.id, lp, true);
Adam Cohenfc53cd22011-07-20 15:45:11 -0700979 if (d.dragView.hasDrawn()) {
980 mLauncher.getDragLayer().animateViewIntoPosition(d.dragView, mCurrentDragView);
981 } else {
982 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}