blob: a6757d7f1a8f16d77c2b5c980eb830e09818bab5 [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 Cohenac56cff2011-09-28 20:45:37 -0700159 mFolderName = (FolderEditText) findViewById(R.id.folder_name);
160 mFolderName.setFolder(this);
Adam Cohenea0818d2011-09-30 20:06:58 -0700161 mFolderName.setOnFocusChangeListener(this);
Adam Cohen76fc0852011-06-17 13:26:23 -0700162
163 // We find out how tall the text view wants to be (it is set to wrap_content), so that
164 // we can allocate the appropriate amount of space for it.
165 int measureSpec = MeasureSpec.UNSPECIFIED;
166 mFolderName.measure(measureSpec, measureSpec);
167 mFolderNameHeight = mFolderName.getMeasuredHeight();
168
169 // We disable action mode for now since it messes up the view on phones
170 mFolderName.setCustomSelectionActionModeCallback(mActionModeCallback);
Adam Cohen76fc0852011-06-17 13:26:23 -0700171 mFolderName.setOnEditorActionListener(this);
Adam Cohen4ef610f2011-06-21 22:37:36 -0700172 mFolderName.setSelectAllOnFocus(true);
Adam Cohen7a14d0b2011-06-24 11:36:35 -0700173 mFolderName.setInputType(mFolderName.getInputType() |
174 InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS | InputType.TYPE_TEXT_FLAG_CAP_WORDS);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800175 }
Adam Cohen2801caf2011-05-13 20:57:39 -0700176
Adam Cohen76fc0852011-06-17 13:26:23 -0700177 private ActionMode.Callback mActionModeCallback = new ActionMode.Callback() {
178 public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
179 return false;
180 }
181
182 public boolean onCreateActionMode(ActionMode mode, Menu menu) {
183 return false;
184 }
185
186 public void onDestroyActionMode(ActionMode mode) {
187 }
188
189 public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
190 return false;
191 }
192 };
193
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800194 public void onClick(View v) {
Adam Cohendf2cc412011-04-27 16:56:57 -0700195 Object tag = v.getTag();
196 if (tag instanceof ShortcutInfo) {
197 // refactor this code from Folder
198 ShortcutInfo item = (ShortcutInfo) tag;
199 int[] pos = new int[2];
200 v.getLocationOnScreen(pos);
201 item.intent.setSourceBounds(new Rect(pos[0], pos[1],
202 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
203 mLauncher.startActivitySafely(item.intent, item);
Adam Cohendf2cc412011-04-27 16:56:57 -0700204 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800205 }
206
207 public boolean onLongClick(View v) {
Adam Cohendf2cc412011-04-27 16:56:57 -0700208 Object tag = v.getTag();
209 if (tag instanceof ShortcutInfo) {
Adam Cohendf2cc412011-04-27 16:56:57 -0700210 ShortcutInfo item = (ShortcutInfo) tag;
211 if (!v.isInTouchMode()) {
212 return false;
213 }
214
Winson Chung7d7541e2011-09-16 20:14:36 -0700215 mLauncher.dismissFolderCling(null);
216
Adam Cohendf2cc412011-04-27 16:56:57 -0700217 mLauncher.getWorkspace().onDragStartedWithItem(v);
Adam Cohenac8c8762011-07-13 11:15:27 -0700218 mLauncher.getWorkspace().beginDragShared(v, this);
Adam Cohenbadf71e2011-05-26 19:08:29 -0700219 mIconDrawable = ((TextView) v).getCompoundDrawables()[1];
Adam Cohen76078c42011-06-09 15:06:52 -0700220
221 mCurrentDragInfo = item;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700222 mEmptyCell[0] = item.cellX;
223 mEmptyCell[1] = item.cellY;
224 mCurrentDragView = v;
Adam Cohenfc53cd22011-07-20 15:45:11 -0700225
226 mContent.removeView(mCurrentDragView);
227 mInfo.remove(mCurrentDragInfo);
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700228 mDragInProgress = true;
Adam Cohen05e0f402011-08-01 12:12:49 -0700229 mItemAddedBackToSelfViaIcon = false;
Adam Cohendf2cc412011-04-27 16:56:57 -0700230 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800231 return true;
232 }
233
Adam Cohen76fc0852011-06-17 13:26:23 -0700234 public boolean isEditingName() {
235 return mIsEditingName;
236 }
237
238 public void startEditingFolderName() {
Adam Cohena65beee2011-06-27 21:32:23 -0700239 mFolderName.setHint("");
Adam Cohen76fc0852011-06-17 13:26:23 -0700240 mIsEditingName = true;
241 }
242
243 public void dismissEditingName() {
244 mInputMethodManager.hideSoftInputFromWindow(getWindowToken(), 0);
245 doneEditingFolderName(true);
246 }
247
248 public void doneEditingFolderName(boolean commit) {
Adam Cohena65beee2011-06-27 21:32:23 -0700249 mFolderName.setHint(sHintText);
Adam Cohen1df26a32011-08-12 16:15:23 -0700250 // Convert to a string here to ensure that no other state associated with the text field
251 // gets saved.
Adam Cohen3371da02011-10-25 21:38:29 -0700252 String newTitle = mFolderName.getText().toString();
253 mInfo.setTitle(newTitle);
Adam Cohen76fc0852011-06-17 13:26:23 -0700254 LauncherModel.updateItemInDatabase(mLauncher, mInfo);
Adam Cohenac56cff2011-09-28 20:45:37 -0700255
Adam Cohen3371da02011-10-25 21:38:29 -0700256 if (commit) {
257 sendCustomAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED,
258 String.format(mContext.getString(R.string.folder_renamed), newTitle));
259 }
Adam Cohenac56cff2011-09-28 20:45:37 -0700260 // In order to clear the focus from the text field, we set the focus on ourself. This
261 // ensures that every time the field is clicked, focus is gained, giving reliable behavior.
262 requestFocus();
263
Adam Cohene601a432011-07-26 21:51:30 -0700264 Selection.setSelection((Spannable) mFolderName.getText(), 0, 0);
Adam Cohen76fc0852011-06-17 13:26:23 -0700265 mIsEditingName = false;
266 }
267
268 public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
269 if (actionId == EditorInfo.IME_ACTION_DONE) {
270 dismissEditingName();
271 return true;
272 }
273 return false;
274 }
275
276 public View getEditTextRegion() {
277 return mFolderName;
278 }
279
Adam Cohenbadf71e2011-05-26 19:08:29 -0700280 public Drawable getDragDrawable() {
281 return mIconDrawable;
282 }
283
Adam Cohen0c872ba2011-05-05 10:34:16 -0700284 /**
285 * We need to handle touch events to prevent them from falling through to the workspace below.
286 */
287 @Override
288 public boolean onTouchEvent(MotionEvent ev) {
289 return true;
290 }
291
Joe Onorato00acb122009-08-04 16:04:30 -0400292 public void setDragController(DragController dragController) {
293 mDragController = dragController;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800294 }
295
Adam Cohen2801caf2011-05-13 20:57:39 -0700296 void setFolderIcon(FolderIcon icon) {
297 mFolderIcon = icon;
298 }
299
Adam Cohen3371da02011-10-25 21:38:29 -0700300 @Override
301 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
302 // When the folder gets focus, we don't want to announce the list of items.
303 return true;
304 }
305
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800306 /**
307 * @return the FolderInfo object associated with this folder
308 */
309 FolderInfo getInfo() {
310 return mInfo;
311 }
312
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800313 void bind(FolderInfo info) {
314 mInfo = info;
Adam Cohendf2cc412011-04-27 16:56:57 -0700315 ArrayList<ShortcutInfo> children = info.contents;
Adam Cohenc508b2d2011-06-28 14:41:44 -0700316 ArrayList<ShortcutInfo> overflow = new ArrayList<ShortcutInfo>();
Adam Cohen7c693212011-05-18 15:26:57 -0700317 setupContentForNumItems(children.size());
Adam Cohen0057bbc2011-08-12 18:30:51 -0700318 int count = 0;
Adam Cohendf2cc412011-04-27 16:56:57 -0700319 for (int i = 0; i < children.size(); i++) {
320 ShortcutInfo child = (ShortcutInfo) children.get(i);
Adam Cohenc508b2d2011-06-28 14:41:44 -0700321 if (!createAndAddShortcut(child)) {
322 overflow.add(child);
Adam Cohen0057bbc2011-08-12 18:30:51 -0700323 } else {
324 count++;
Adam Cohenc508b2d2011-06-28 14:41:44 -0700325 }
Adam Cohendf2cc412011-04-27 16:56:57 -0700326 }
Adam Cohenc508b2d2011-06-28 14:41:44 -0700327
Adam Cohen0057bbc2011-08-12 18:30:51 -0700328 // We rearrange the items in case there are any empty gaps
329 setupContentForNumItems(count);
330
Adam Cohenc508b2d2011-06-28 14:41:44 -0700331 // If our folder has too many items we prune them from the list. This is an issue
332 // when upgrading from the old Folders implementation which could contain an unlimited
333 // number of items.
334 for (ShortcutInfo item: overflow) {
335 mInfo.remove(item);
336 LauncherModel.deleteItemFromDatabase(mLauncher, item);
337 }
338
Adam Cohen4dbe6d92011-05-18 17:14:15 -0700339 mItemsInvalidated = true;
Adam Cohenac56cff2011-09-28 20:45:37 -0700340 updateTextViewFocus();
Adam Cohena9cf38f2011-05-02 15:36:58 -0700341 mInfo.addListener(this);
Adam Cohen4ef610f2011-06-21 22:37:36 -0700342
Adam Cohenafb01ee2011-06-23 15:38:03 -0700343 if (!sDefaultFolderName.contentEquals(mInfo.title)) {
Adam Cohen4ef610f2011-06-21 22:37:36 -0700344 mFolderName.setText(mInfo.title);
345 } else {
346 mFolderName.setText("");
347 }
Adam Cohendf2cc412011-04-27 16:56:57 -0700348 }
349
350 /**
351 * Creates a new UserFolder, inflated from R.layout.user_folder.
352 *
353 * @param context The application's context.
354 *
355 * @return A new UserFolder.
356 */
357 static Folder fromXml(Context context) {
358 return (Folder) LayoutInflater.from(context).inflate(R.layout.user_folder, null);
359 }
360
361 /**
362 * This method is intended to make the UserFolder to be visually identical in size and position
363 * to its associated FolderIcon. This allows for a seamless transition into the expanded state.
364 */
365 private void positionAndSizeAsIcon() {
Adam Cohen3e8f8112011-07-02 18:03:00 -0700366 if (!(getParent() instanceof DragLayer)) return;
Adam Cohendf2cc412011-04-27 16:56:57 -0700367
Adam Cohen8e776a62011-06-28 18:10:06 -0700368 DragLayer.LayoutParams lp = (DragLayer.LayoutParams) getLayoutParams();
Adam Cohendf2cc412011-04-27 16:56:57 -0700369
Adam Cohen2801caf2011-05-13 20:57:39 -0700370 if (mMode == PARTIAL_GROW) {
371 setScaleX(0.8f);
372 setScaleY(0.8f);
373 setAlpha(0f);
374 } else {
Adam Cohen3e8f8112011-07-02 18:03:00 -0700375 mLauncher.getDragLayer().getDescendantRectRelativeToSelf(mFolderIcon, mIconRect);
376 lp.width = mIconRect.width();
377 lp.height = mIconRect.height();
378 lp.x = mIconRect.left;
379 lp.y = mIconRect.top;
Adam Cohen2801caf2011-05-13 20:57:39 -0700380 mContent.setAlpha(0);
381 }
Adam Cohendf2cc412011-04-27 16:56:57 -0700382 mState = STATE_SMALL;
383 }
384
385 public void animateOpen() {
Adam Cohen3e8f8112011-07-02 18:03:00 -0700386 positionAndSizeAsIcon();
387
Adam Cohen8e776a62011-06-28 18:10:06 -0700388 if (!(getParent() instanceof DragLayer)) return;
Adam Cohendf2cc412011-04-27 16:56:57 -0700389
Adam Cohen2801caf2011-05-13 20:57:39 -0700390 ObjectAnimator oa;
Adam Cohen8e776a62011-06-28 18:10:06 -0700391 DragLayer.LayoutParams lp = (DragLayer.LayoutParams) getLayoutParams();
Adam Cohendf2cc412011-04-27 16:56:57 -0700392
Adam Cohen2801caf2011-05-13 20:57:39 -0700393 centerAboutIcon();
394 if (mMode == PARTIAL_GROW) {
395 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1);
396 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.0f);
397 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.0f);
398 oa = ObjectAnimator.ofPropertyValuesHolder(this, alpha, scaleX, scaleY);
399 } else {
400 PropertyValuesHolder width = PropertyValuesHolder.ofInt("width", mNewSize.width());
401 PropertyValuesHolder height = PropertyValuesHolder.ofInt("height", mNewSize.height());
402 PropertyValuesHolder x = PropertyValuesHolder.ofInt("x", mNewSize.left);
403 PropertyValuesHolder y = PropertyValuesHolder.ofInt("y", mNewSize.top);
404 oa = ObjectAnimator.ofPropertyValuesHolder(lp, width, height, x, y);
405 oa.addUpdateListener(new AnimatorUpdateListener() {
406 public void onAnimationUpdate(ValueAnimator animation) {
407 requestLayout();
408 }
409 });
Adam Cohendf2cc412011-04-27 16:56:57 -0700410
Adam Cohen2801caf2011-05-13 20:57:39 -0700411 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1.0f);
412 ObjectAnimator alphaOa = ObjectAnimator.ofPropertyValuesHolder(mContent, alpha);
413 alphaOa.setDuration(mExpandDuration);
414 alphaOa.setInterpolator(new AccelerateInterpolator(2.0f));
415 alphaOa.start();
416 }
Adam Cohendf2cc412011-04-27 16:56:57 -0700417
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,
422 String.format(mContext.getString(R.string.folder_opened),
423 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) {
442 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
443 AccessibilityEvent event = AccessibilityEvent.obtain(type);
444 onInitializeAccessibilityEvent(event);
445 event.getText().add(text);
446 AccessibilityManager.getInstance(mContext).sendAccessibilityEvent(event);
447 }
448 }
449
Adam Cohenac56cff2011-09-28 20:45:37 -0700450 private void setFocusOnFirstChild() {
451 View firstChild = mContent.getChildAt(0, 0);
452 if (firstChild != null) {
453 firstChild.requestFocus();
454 }
455 }
456
Adam Cohendf2cc412011-04-27 16:56:57 -0700457 public void animateClosed() {
Adam Cohen8e776a62011-06-28 18:10:06 -0700458 if (!(getParent() instanceof DragLayer)) return;
Adam Cohendf2cc412011-04-27 16:56:57 -0700459
Adam Cohen2801caf2011-05-13 20:57:39 -0700460 ObjectAnimator oa;
Adam Cohen2801caf2011-05-13 20:57:39 -0700461 if (mMode == PARTIAL_GROW) {
462 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0);
463 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 0.9f);
464 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 0.9f);
465 oa = ObjectAnimator.ofPropertyValuesHolder(this, alpha, scaleX, scaleY);
466 } else {
Adam Cohen8e776a62011-06-28 18:10:06 -0700467 DragLayer.LayoutParams lp = (DragLayer.LayoutParams) getLayoutParams();
Adam Cohendf2cc412011-04-27 16:56:57 -0700468
Adam Cohen3e8f8112011-07-02 18:03:00 -0700469 PropertyValuesHolder width = PropertyValuesHolder.ofInt("width", mIconRect.width());
470 PropertyValuesHolder height = PropertyValuesHolder.ofInt("height", mIconRect.height());
471 PropertyValuesHolder x = PropertyValuesHolder.ofInt("x", mIconRect.left);
472 PropertyValuesHolder y = PropertyValuesHolder.ofInt("y", mIconRect.top);
Adam Cohen2801caf2011-05-13 20:57:39 -0700473 oa = ObjectAnimator.ofPropertyValuesHolder(lp, width, height, x, y);
474 oa.addUpdateListener(new AnimatorUpdateListener() {
475 public void onAnimationUpdate(ValueAnimator animation) {
476 requestLayout();
477 }
478 });
Adam Cohendf2cc412011-04-27 16:56:57 -0700479
Adam Cohen2801caf2011-05-13 20:57:39 -0700480 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0f);
481 ObjectAnimator alphaOa = ObjectAnimator.ofPropertyValuesHolder(mContent, alpha);
482 alphaOa.setDuration(mExpandDuration);
483 alphaOa.setInterpolator(new DecelerateInterpolator(2.0f));
484 alphaOa.start();
485 }
Adam Cohendf2cc412011-04-27 16:56:57 -0700486
Adam Cohen2801caf2011-05-13 20:57:39 -0700487 oa.addListener(new AnimatorListenerAdapter() {
Adam Cohendf2cc412011-04-27 16:56:57 -0700488 @Override
489 public void onAnimationEnd(Animator animation) {
Adam Cohen2801caf2011-05-13 20:57:39 -0700490 onCloseComplete();
Adam Cohen2801caf2011-05-13 20:57:39 -0700491 mState = STATE_SMALL;
Adam Cohendf2cc412011-04-27 16:56:57 -0700492 }
493 @Override
494 public void onAnimationStart(Animator animation) {
Adam Cohen3371da02011-10-25 21:38:29 -0700495 sendCustomAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED,
496 mContext.getString(R.string.folder_closed));
Adam Cohendf2cc412011-04-27 16:56:57 -0700497 mState = STATE_ANIMATING;
498 }
499 });
Adam Cohen2801caf2011-05-13 20:57:39 -0700500 oa.setDuration(mExpandDuration);
501 oa.start();
Adam Cohendf2cc412011-04-27 16:56:57 -0700502 }
503
504 void notifyDataSetChanged() {
505 // recreate all the children if the data set changes under us. We may want to do this more
506 // intelligently (ie just removing the views that should no longer exist)
507 mContent.removeAllViewsInLayout();
508 bind(mInfo);
509 }
510
Adam Cohencb3382b2011-05-24 14:07:08 -0700511 public boolean acceptDrop(DragObject d) {
512 final ItemInfo item = (ItemInfo) d.dragInfo;
Adam Cohendf2cc412011-04-27 16:56:57 -0700513 final int itemType = item.itemType;
Adam Cohen2801caf2011-05-13 20:57:39 -0700514 return ((itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION ||
515 itemType == LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT) &&
516 !isFull());
Adam Cohendf2cc412011-04-27 16:56:57 -0700517 }
518
Adam Cohendf2cc412011-04-27 16:56:57 -0700519 protected boolean findAndSetEmptyCells(ShortcutInfo item) {
520 int[] emptyCell = new int[2];
521 if (mContent.findCellForSpan(emptyCell, item.spanX, item.spanY)) {
522 item.cellX = emptyCell[0];
523 item.cellY = emptyCell[1];
Adam Cohendf2cc412011-04-27 16:56:57 -0700524 return true;
525 } else {
526 return false;
527 }
528 }
529
Adam Cohenc508b2d2011-06-28 14:41:44 -0700530 protected boolean createAndAddShortcut(ShortcutInfo item) {
Adam Cohendf2cc412011-04-27 16:56:57 -0700531 final TextView textView =
Adam Cohene87b9242011-06-29 14:01:26 -0700532 (TextView) mInflater.inflate(R.layout.application, this, false);
Adam Cohendf2cc412011-04-27 16:56:57 -0700533 textView.setCompoundDrawablesWithIntrinsicBounds(null,
534 new FastBitmapDrawable(item.getIcon(mIconCache)), null, null);
535 textView.setText(item.title);
536 textView.setTag(item);
537
538 textView.setOnClickListener(this);
539 textView.setOnLongClickListener(this);
540
Adam Cohenc508b2d2011-06-28 14:41:44 -0700541 // We need to check here to verify that the given item's location isn't already occupied
542 // by another item. If it is, we need to find the next available slot and assign
543 // it that position. This is an issue when upgrading from the old Folders implementation
544 // which could contain an unlimited number of items.
Adam Cohen0057bbc2011-08-12 18:30:51 -0700545 if (mContent.getChildAt(item.cellX, item.cellY) != null || item.cellX < 0 || item.cellY < 0
546 || item.cellX >= mContent.getCountX() || item.cellY >= mContent.getCountY()) {
Adam Cohenc508b2d2011-06-28 14:41:44 -0700547 if (!findAndSetEmptyCells(item)) {
548 return false;
549 }
550 }
551
Adam Cohendf2cc412011-04-27 16:56:57 -0700552 CellLayout.LayoutParams lp =
553 new CellLayout.LayoutParams(item.cellX, item.cellY, item.spanX, item.spanY);
554 boolean insert = false;
Adam Cohenac56cff2011-09-28 20:45:37 -0700555 textView.setOnKeyListener(new FolderKeyEventListener());
Adam Cohendf2cc412011-04-27 16:56:57 -0700556 mContent.addViewToCellLayout(textView, insert ? 0 : -1, (int)item.id, lp, true);
Adam Cohenc508b2d2011-06-28 14:41:44 -0700557 return true;
Adam Cohendf2cc412011-04-27 16:56:57 -0700558 }
559
Adam Cohencb3382b2011-05-24 14:07:08 -0700560 public void onDragEnter(DragObject d) {
Adam Cohenbfbfd262011-06-13 16:55:12 -0700561 mPreviousTargetCell[0] = -1;
562 mPreviousTargetCell[1] = -1;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700563 mOnExitAlarm.cancelAlarm();
564 }
565
566 OnAlarmListener mReorderAlarmListener = new OnAlarmListener() {
567 public void onAlarm(Alarm alarm) {
568 realTimeReorder(mEmptyCell, mTargetCell);
569 }
570 };
571
572 boolean readingOrderGreaterThan(int[] v1, int[] v2) {
573 if (v1[1] > v2[1] || (v1[1] == v2[1] && v1[0] > v2[0])) {
574 return true;
575 } else {
576 return false;
577 }
578 }
579
580 private void realTimeReorder(int[] empty, int[] target) {
581 boolean wrap;
582 int startX;
583 int endX;
584 int startY;
Adam Cohen76fc0852011-06-17 13:26:23 -0700585 int delay = 0;
586 float delayAmount = 30;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700587 if (readingOrderGreaterThan(target, empty)) {
588 wrap = empty[0] >= mContent.getCountX() - 1;
589 startY = wrap ? empty[1] + 1 : empty[1];
590 for (int y = startY; y <= target[1]; y++) {
591 startX = y == empty[1] ? empty[0] + 1 : 0;
592 endX = y < target[1] ? mContent.getCountX() - 1 : target[0];
593 for (int x = startX; x <= endX; x++) {
594 View v = mContent.getChildAt(x,y);
595 if (mContent.animateChildToPosition(v, empty[0], empty[1],
Adam Cohen76fc0852011-06-17 13:26:23 -0700596 REORDER_ANIMATION_DURATION, delay)) {
Adam Cohenbfbfd262011-06-13 16:55:12 -0700597 empty[0] = x;
598 empty[1] = y;
Adam Cohen76fc0852011-06-17 13:26:23 -0700599 delay += delayAmount;
600 delayAmount *= 0.9;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700601 }
602 }
603 }
604 } else {
605 wrap = empty[0] == 0;
606 startY = wrap ? empty[1] - 1 : empty[1];
607 for (int y = startY; y >= target[1]; y--) {
608 startX = y == empty[1] ? empty[0] - 1 : mContent.getCountX() - 1;
609 endX = y > target[1] ? 0 : target[0];
610 for (int x = startX; x >= endX; x--) {
611 View v = mContent.getChildAt(x,y);
612 if (mContent.animateChildToPosition(v, empty[0], empty[1],
Adam Cohen76fc0852011-06-17 13:26:23 -0700613 REORDER_ANIMATION_DURATION, delay)) {
Adam Cohenbfbfd262011-06-13 16:55:12 -0700614 empty[0] = x;
615 empty[1] = y;
Adam Cohen76fc0852011-06-17 13:26:23 -0700616 delay += delayAmount;
617 delayAmount *= 0.9;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700618 }
619 }
620 }
621 }
Adam Cohendf2cc412011-04-27 16:56:57 -0700622 }
623
Adam Cohencb3382b2011-05-24 14:07:08 -0700624 public void onDragOver(DragObject d) {
Adam Cohenbfbfd262011-06-13 16:55:12 -0700625 float[] r = getDragViewVisualCenter(d.x, d.y, d.xOffset, d.yOffset, d.dragView, null);
626 mTargetCell = mContent.findNearestArea((int) r[0], (int) r[1], 1, 1, mTargetCell);
627
628 if (mTargetCell[0] != mPreviousTargetCell[0] || mTargetCell[1] != mPreviousTargetCell[1]) {
629 mReorderAlarm.cancelAlarm();
630 mReorderAlarm.setOnAlarmListener(mReorderAlarmListener);
631 mReorderAlarm.setAlarm(150);
632 mPreviousTargetCell[0] = mTargetCell[0];
633 mPreviousTargetCell[1] = mTargetCell[1];
634 }
Adam Cohendf2cc412011-04-27 16:56:57 -0700635 }
636
Adam Cohenbfbfd262011-06-13 16:55:12 -0700637 // This is used to compute the visual center of the dragView. The idea is that
638 // the visual center represents the user's interpretation of where the item is, and hence
639 // is the appropriate point to use when determining drop location.
640 private float[] getDragViewVisualCenter(int x, int y, int xOffset, int yOffset,
641 DragView dragView, float[] recycle) {
642 float res[];
643 if (recycle == null) {
644 res = new float[2];
645 } else {
646 res = recycle;
647 }
648
649 // These represent the visual top and left of drag view if a dragRect was provided.
650 // If a dragRect was not provided, then they correspond to the actual view left and
651 // top, as the dragRect is in that case taken to be the entire dragView.
652 // R.dimen.dragViewOffsetY.
653 int left = x - xOffset;
654 int top = y - yOffset;
655
656 // In order to find the visual center, we shift by half the dragRect
657 res[0] = left + dragView.getDragRegion().width() / 2;
658 res[1] = top + dragView.getDragRegion().height() / 2;
659
660 return res;
661 }
662
663 OnAlarmListener mOnExitAlarmListener = new OnAlarmListener() {
664 public void onAlarm(Alarm alarm) {
Adam Cohen3e8f8112011-07-02 18:03:00 -0700665 completeDragExit();
Adam Cohenbfbfd262011-06-13 16:55:12 -0700666 }
667 };
668
Adam Cohen95bb8002011-07-03 23:40:28 -0700669 public void completeDragExit() {
Adam Cohen3e8f8112011-07-02 18:03:00 -0700670 mLauncher.closeFolder();
671 mCurrentDragInfo = null;
672 mCurrentDragView = null;
673 mSuppressOnAdd = false;
674 mRearrangeOnClose = true;
675 }
676
Adam Cohencb3382b2011-05-24 14:07:08 -0700677 public void onDragExit(DragObject d) {
Adam Cohenbfbfd262011-06-13 16:55:12 -0700678 // We only close the folder if this is a true drag exit, ie. not because a drop
679 // has occurred above the folder.
680 if (!d.dragComplete) {
681 mOnExitAlarm.setOnAlarmListener(mOnExitAlarmListener);
682 mOnExitAlarm.setAlarm(ON_EXIT_CLOSE_DELAY);
683 }
684 mReorderAlarm.cancelAlarm();
Adam Cohen2801caf2011-05-13 20:57:39 -0700685 }
686
Adam Cohenc0dcf592011-06-01 15:30:43 -0700687 public void onDropCompleted(View target, DragObject d, boolean success) {
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700688 if (success) {
Adam Cohen05e0f402011-08-01 12:12:49 -0700689 if (mDeleteFolderOnDropCompleted && !mItemAddedBackToSelfViaIcon) {
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700690 replaceFolderWithFinalItem();
691 }
692 } else {
693 // The drag failed, we need to return the item to the folder
694 mFolderIcon.onDrop(d);
695
696 // We're going to trigger a "closeFolder" which may occur before this item has
697 // been added back to the folder -- this could cause the folder to be deleted
698 if (mOnExitAlarm.alarmPending()) {
699 mSuppressFolderDeletion = true;
700 }
701 }
702
703 if (target != this) {
704 if (mOnExitAlarm.alarmPending()) {
705 mOnExitAlarm.cancelAlarm();
706 completeDragExit();
707 }
708 }
709 mDeleteFolderOnDropCompleted = false;
710 mDragInProgress = false;
Adam Cohen05e0f402011-08-01 12:12:49 -0700711 mItemAddedBackToSelfViaIcon = false;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700712 mCurrentDragInfo = null;
713 mCurrentDragView = null;
714 mSuppressOnAdd = false;
Adam Cohen4045eb72011-10-06 11:44:26 -0700715
716 // Reordering may have occured, and we need to save the new item locations. We do this once
717 // at the end to prevent unnecessary database operations.
718 updateItemLocationsInDatabase();
719 }
720
721 private void updateItemLocationsInDatabase() {
722 ArrayList<View> list = getItemsInReadingOrder();
723 for (int i = 0; i < list.size(); i++) {
724 View v = list.get(i);
725 ItemInfo info = (ItemInfo) v.getTag();
726 LauncherModel.moveItemInDatabase(mLauncher, info, mInfo.id, 0,
727 info.cellX, info.cellY);
728 }
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700729 }
Adam Cohen228da5a2011-07-27 22:23:47 -0700730
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700731 public void notifyDrop() {
732 if (mDragInProgress) {
Adam Cohen05e0f402011-08-01 12:12:49 -0700733 mItemAddedBackToSelfViaIcon = true;
Adam Cohen76078c42011-06-09 15:06:52 -0700734 }
Adam Cohendf2cc412011-04-27 16:56:57 -0700735 }
736
737 public boolean isDropEnabled() {
738 return true;
739 }
740
Adam Cohencb3382b2011-05-24 14:07:08 -0700741 public DropTarget getDropTargetDelegate(DragObject d) {
Adam Cohendf2cc412011-04-27 16:56:57 -0700742 return null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800743 }
Adam Cohena9cf38f2011-05-02 15:36:58 -0700744
Adam Cohen4045eb72011-10-06 11:44:26 -0700745 private void setupContentDimensions(int count) {
Adam Cohen2801caf2011-05-13 20:57:39 -0700746 ArrayList<View> list = getItemsInReadingOrder();
747
748 int countX = mContent.getCountX();
749 int countY = mContent.getCountY();
Adam Cohen7c693212011-05-18 15:26:57 -0700750 boolean done = false;
Adam Cohen2801caf2011-05-13 20:57:39 -0700751
Adam Cohen7c693212011-05-18 15:26:57 -0700752 while (!done) {
753 int oldCountX = countX;
754 int oldCountY = countY;
755 if (countX * countY < count) {
756 // Current grid is too small, expand it
Adam Cohen78dc83e2011-11-15 17:10:00 -0800757 if ((countX <= countY || countY == mMaxCountY) && countX < mMaxCountX) {
Adam Cohen7c693212011-05-18 15:26:57 -0700758 countX++;
759 } else if (countY < mMaxCountY) {
760 countY++;
761 }
762 if (countY == 0) countY++;
763 } else if ((countY - 1) * countX >= count && countY >= countX) {
764 countY = Math.max(0, countY - 1);
765 } else if ((countX - 1) * countY >= count) {
766 countX = Math.max(0, countX - 1);
Adam Cohen2801caf2011-05-13 20:57:39 -0700767 }
Adam Cohen7c693212011-05-18 15:26:57 -0700768 done = countX == oldCountX && countY == oldCountY;
Adam Cohen2801caf2011-05-13 20:57:39 -0700769 }
Adam Cohen7c693212011-05-18 15:26:57 -0700770 mContent.setGridSize(countX, countY);
Adam Cohen2801caf2011-05-13 20:57:39 -0700771 arrangeChildren(list);
772 }
773
774 public boolean isFull() {
Adam Cohen78dc83e2011-11-15 17:10:00 -0800775 return getItemCount() >= mMaxNumItems;
Adam Cohen2801caf2011-05-13 20:57:39 -0700776 }
777
778 private void centerAboutIcon() {
Adam Cohen8e776a62011-06-28 18:10:06 -0700779 DragLayer.LayoutParams lp = (DragLayer.LayoutParams) getLayoutParams();
Adam Cohen2801caf2011-05-13 20:57:39 -0700780
781 int width = getPaddingLeft() + getPaddingRight() + mContent.getDesiredWidth();
Adam Cohenf4bb1cd2011-07-22 14:36:03 -0700782 int height = getPaddingTop() + getPaddingBottom() + mContent.getDesiredHeight()
783 + mFolderNameHeight;
Adam Cohen8e776a62011-06-28 18:10:06 -0700784 DragLayer parent = (DragLayer) mLauncher.findViewById(R.id.drag_layer);
Adam Cohen2801caf2011-05-13 20:57:39 -0700785
Adam Cohen8e776a62011-06-28 18:10:06 -0700786 parent.getDescendantRectRelativeToSelf(mFolderIcon, mTempRect);
787
788 int centerX = mTempRect.centerX();
789 int centerY = mTempRect.centerY();
Adam Cohen2801caf2011-05-13 20:57:39 -0700790 int centeredLeft = centerX - width / 2;
791 int centeredTop = centerY - height / 2;
792
Adam Cohen35e7e642011-07-17 14:47:18 -0700793 // We first fetch the currently visible CellLayoutChildren
Winson Chung3d503fb2011-07-13 17:25:49 -0700794 CellLayout currentPage = mLauncher.getWorkspace().getCurrentDropLayout();
Adam Cohen35e7e642011-07-17 14:47:18 -0700795 CellLayoutChildren boundingLayout = currentPage.getChildrenLayout();
796 Rect bounds = new Rect();
797 parent.getDescendantRectRelativeToSelf(boundingLayout, bounds);
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));
820 mFolderIcon.setPivotX(folderIconPivotX);
821 mFolderIcon.setPivotY(folderIconPivotY);
822
823 if (mMode == PARTIAL_GROW) {
824 lp.width = width;
825 lp.height = height;
826 lp.x = left;
827 lp.y = top;
828 } else {
829 mNewSize.set(left, top, left + width, top + height);
830 }
831 }
832
833 private void setupContentForNumItems(int count) {
Adam Cohen4045eb72011-10-06 11:44:26 -0700834 setupContentDimensions(count);
Adam Cohen2801caf2011-05-13 20:57:39 -0700835
Adam Cohen8e776a62011-06-28 18:10:06 -0700836 DragLayer.LayoutParams lp = (DragLayer.LayoutParams) getLayoutParams();
Adam Cohen2801caf2011-05-13 20:57:39 -0700837 if (lp == null) {
Adam Cohen8e776a62011-06-28 18:10:06 -0700838 lp = new DragLayer.LayoutParams(0, 0);
839 lp.customPosition = true;
Adam Cohen2801caf2011-05-13 20:57:39 -0700840 setLayoutParams(lp);
841 }
842 centerAboutIcon();
843 }
844
Adam Cohen234c4cd2011-07-17 21:03:04 -0700845 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
846 int width = getPaddingLeft() + getPaddingRight() + mContent.getDesiredWidth();
Adam Cohenf4bb1cd2011-07-22 14:36:03 -0700847 int height = getPaddingTop() + getPaddingBottom() + mContent.getDesiredHeight()
848 + mFolderNameHeight;
Adam Cohen234c4cd2011-07-17 21:03:04 -0700849
850 int contentWidthSpec = MeasureSpec.makeMeasureSpec(mContent.getDesiredWidth(),
851 MeasureSpec.EXACTLY);
852 int contentHeightSpec = MeasureSpec.makeMeasureSpec(mContent.getDesiredHeight(),
853 MeasureSpec.EXACTLY);
854 mContent.measure(contentWidthSpec, contentHeightSpec);
855
856 mFolderName.measure(contentWidthSpec,
857 MeasureSpec.makeMeasureSpec(mFolderNameHeight, MeasureSpec.EXACTLY));
858 setMeasuredDimension(width, height);
859 }
860
Adam Cohen2801caf2011-05-13 20:57:39 -0700861 private void arrangeChildren(ArrayList<View> list) {
862 int[] vacant = new int[2];
863 if (list == null) {
864 list = getItemsInReadingOrder();
865 }
866 mContent.removeAllViews();
867
868 for (int i = 0; i < list.size(); i++) {
869 View v = list.get(i);
870 mContent.getVacantCell(vacant, 1, 1);
871 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) v.getLayoutParams();
872 lp.cellX = vacant[0];
873 lp.cellY = vacant[1];
874 ItemInfo info = (ItemInfo) v.getTag();
Adam Cohen2792a332011-09-26 21:09:47 -0700875 if (info.cellX != vacant[0] || info.cellY != vacant[1]) {
876 info.cellX = vacant[0];
877 info.cellY = vacant[1];
878 LauncherModel.addOrMoveItemInDatabase(mLauncher, info, mInfo.id, 0,
879 info.cellX, info.cellY);
880 }
Adam Cohen2801caf2011-05-13 20:57:39 -0700881 boolean insert = false;
882 mContent.addViewToCellLayout(v, insert ? 0 : -1, (int)info.id, lp, true);
883 }
Adam Cohen7c693212011-05-18 15:26:57 -0700884 mItemsInvalidated = true;
Adam Cohen2801caf2011-05-13 20:57:39 -0700885 }
886
Adam Cohena9cf38f2011-05-02 15:36:58 -0700887 public int getItemCount() {
888 return mContent.getChildrenLayout().getChildCount();
889 }
890
891 public View getItemAt(int index) {
892 return mContent.getChildrenLayout().getChildAt(index);
893 }
894
Adam Cohen2801caf2011-05-13 20:57:39 -0700895 private void onCloseComplete() {
Adam Cohen05e0f402011-08-01 12:12:49 -0700896 DragLayer parent = (DragLayer) getParent();
Adam Cohen4554ee12011-08-03 16:13:21 -0700897 parent.removeView(this);
898 mDragController.removeDropTarget((DropTarget) this);
Adam Cohen05e0f402011-08-01 12:12:49 -0700899 clearFocus();
Adam Cohenac56cff2011-09-28 20:45:37 -0700900 mFolderIcon.requestFocus();
Adam Cohen05e0f402011-08-01 12:12:49 -0700901
Adam Cohen2801caf2011-05-13 20:57:39 -0700902 if (mRearrangeOnClose) {
903 setupContentForNumItems(getItemCount());
904 mRearrangeOnClose = false;
905 }
Adam Cohenafb01ee2011-06-23 15:38:03 -0700906 if (getItemCount() <= 1) {
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700907 if (!mDragInProgress && !mSuppressFolderDeletion) {
908 replaceFolderWithFinalItem();
909 } else if (mDragInProgress) {
910 mDeleteFolderOnDropCompleted = true;
911 }
Adam Cohenafb01ee2011-06-23 15:38:03 -0700912 }
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700913 mSuppressFolderDeletion = false;
Adam Cohenafb01ee2011-06-23 15:38:03 -0700914 }
915
916 private void replaceFolderWithFinalItem() {
917 ItemInfo finalItem = null;
918
919 if (getItemCount() == 1) {
920 finalItem = mInfo.contents.get(0);
921 }
922
923 // Remove the folder completely
Winson Chung3d503fb2011-07-13 17:25:49 -0700924 CellLayout cellLayout = mLauncher.getCellLayout(mInfo.container, mInfo.screen);
Adam Cohenafb01ee2011-06-23 15:38:03 -0700925 cellLayout.removeView(mFolderIcon);
926 if (mFolderIcon instanceof DropTarget) {
927 mDragController.removeDropTarget((DropTarget) mFolderIcon);
928 }
929 mLauncher.removeFolder(mInfo);
930
931 if (finalItem != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -0700932 LauncherModel.addOrMoveItemInDatabase(mLauncher, finalItem, mInfo.container,
933 mInfo.screen, mInfo.cellX, mInfo.cellY);
Adam Cohenafb01ee2011-06-23 15:38:03 -0700934 }
Adam Cohen716b51e2011-06-30 12:09:54 -0700935 LauncherModel.deleteItemFromDatabase(mLauncher, mInfo);
Adam Cohenafb01ee2011-06-23 15:38:03 -0700936
937 // Add the last remaining child to the workspace in place of the folder
938 if (finalItem != null) {
939 View child = mLauncher.createShortcut(R.layout.application, cellLayout,
940 (ShortcutInfo) finalItem);
941
Winson Chung3d503fb2011-07-13 17:25:49 -0700942 mLauncher.getWorkspace().addInScreen(child, mInfo.container, mInfo.screen, mInfo.cellX,
943 mInfo.cellY, mInfo.spanX, mInfo.spanY);
Adam Cohenafb01ee2011-06-23 15:38:03 -0700944 }
Adam Cohen2801caf2011-05-13 20:57:39 -0700945 }
946
Adam Cohenac56cff2011-09-28 20:45:37 -0700947 // This method keeps track of the last item in the folder for the purposes
948 // of keyboard focus
949 private void updateTextViewFocus() {
950 View lastChild = getItemAt(getItemCount() - 1);
951 getItemAt(getItemCount() - 1);
952 if (lastChild != null) {
953 mFolderName.setNextFocusDownId(lastChild.getId());
954 mFolderName.setNextFocusRightId(lastChild.getId());
955 mFolderName.setNextFocusLeftId(lastChild.getId());
956 mFolderName.setNextFocusUpId(lastChild.getId());
957 }
958 }
959
Adam Cohenbfbfd262011-06-13 16:55:12 -0700960 public void onDrop(DragObject d) {
961 ShortcutInfo item;
962 if (d.dragInfo instanceof ApplicationInfo) {
963 // Came from all apps -- make a copy
964 item = ((ApplicationInfo) d.dragInfo).makeShortcut();
965 item.spanX = 1;
966 item.spanY = 1;
967 } else {
968 item = (ShortcutInfo) d.dragInfo;
969 }
Adam Cohen05e0f402011-08-01 12:12:49 -0700970 // Dragged from self onto self, currently this is the only path possible, however
971 // we keep this as a distinct code path.
Adam Cohenbfbfd262011-06-13 16:55:12 -0700972 if (item == mCurrentDragInfo) {
973 ShortcutInfo si = (ShortcutInfo) mCurrentDragView.getTag();
974 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) mCurrentDragView.getLayoutParams();
975 si.cellX = lp.cellX = mEmptyCell[0];
976 si.cellX = lp.cellY = mEmptyCell[1];
977 mContent.addViewToCellLayout(mCurrentDragView, -1, (int)item.id, lp, true);
Adam Cohenfc53cd22011-07-20 15:45:11 -0700978 if (d.dragView.hasDrawn()) {
979 mLauncher.getDragLayer().animateViewIntoPosition(d.dragView, mCurrentDragView);
980 } else {
981 mCurrentDragView.setVisibility(VISIBLE);
982 }
Adam Cohene9166b22011-07-08 17:11:11 -0700983 mItemsInvalidated = true;
Adam Cohen4045eb72011-10-06 11:44:26 -0700984 setupContentDimensions(getItemCount());
Adam Cohen716b51e2011-06-30 12:09:54 -0700985 mSuppressOnAdd = true;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700986 }
987 mInfo.add(item);
988 }
989
990 public void onAdd(ShortcutInfo item) {
991 mItemsInvalidated = true;
Adam Cohen05e0f402011-08-01 12:12:49 -0700992 // If the item was dropped onto this open folder, we have done the work associated
993 // with adding the item to the folder, as indicated by mSuppressOnAdd being set
Adam Cohenbfbfd262011-06-13 16:55:12 -0700994 if (mSuppressOnAdd) return;
995 if (!findAndSetEmptyCells(item)) {
996 // The current layout is full, can we expand it?
997 setupContentForNumItems(getItemCount() + 1);
998 findAndSetEmptyCells(item);
999 }
1000 createAndAddShortcut(item);
1001 LauncherModel.addOrMoveItemInDatabase(
1002 mLauncher, item, mInfo.id, 0, item.cellX, item.cellY);
1003 }
1004
Adam Cohena9cf38f2011-05-02 15:36:58 -07001005 public void onRemove(ShortcutInfo item) {
Adam Cohen7c693212011-05-18 15:26:57 -07001006 mItemsInvalidated = true;
Adam Cohen05e0f402011-08-01 12:12:49 -07001007 // If this item is being dragged from this open folder, we have already handled
1008 // the work associated with removing the item, so we don't have to do anything here.
Adam Cohenbfbfd262011-06-13 16:55:12 -07001009 if (item == mCurrentDragInfo) return;
Adam Cohendf1e4e82011-06-24 15:57:39 -07001010 View v = getViewForInfo(item);
Adam Cohena9cf38f2011-05-02 15:36:58 -07001011 mContent.removeView(v);
Adam Cohen2801caf2011-05-13 20:57:39 -07001012 if (mState == STATE_ANIMATING) {
1013 mRearrangeOnClose = true;
1014 } else {
1015 setupContentForNumItems(getItemCount());
1016 }
Adam Cohenafb01ee2011-06-23 15:38:03 -07001017 if (getItemCount() <= 1) {
1018 replaceFolderWithFinalItem();
1019 }
Adam Cohena9cf38f2011-05-02 15:36:58 -07001020 }
Adam Cohen7c693212011-05-18 15:26:57 -07001021
Adam Cohendf1e4e82011-06-24 15:57:39 -07001022 private View getViewForInfo(ShortcutInfo item) {
1023 for (int j = 0; j < mContent.getCountY(); j++) {
1024 for (int i = 0; i < mContent.getCountX(); i++) {
1025 View v = mContent.getChildAt(i, j);
1026 if (v.getTag() == item) {
1027 return v;
1028 }
1029 }
1030 }
1031 return null;
1032 }
1033
Adam Cohen76078c42011-06-09 15:06:52 -07001034 public void onItemsChanged() {
Adam Cohenac56cff2011-09-28 20:45:37 -07001035 updateTextViewFocus();
Adam Cohen76078c42011-06-09 15:06:52 -07001036 }
Adam Cohenac56cff2011-09-28 20:45:37 -07001037
Adam Cohen76fc0852011-06-17 13:26:23 -07001038 public void onTitleChanged(CharSequence title) {
1039 }
Adam Cohen76078c42011-06-09 15:06:52 -07001040
Adam Cohen7c693212011-05-18 15:26:57 -07001041 public ArrayList<View> getItemsInReadingOrder() {
Adam Cohen76078c42011-06-09 15:06:52 -07001042 return getItemsInReadingOrder(true);
1043 }
1044
1045 public ArrayList<View> getItemsInReadingOrder(boolean includeCurrentDragItem) {
Adam Cohen7c693212011-05-18 15:26:57 -07001046 if (mItemsInvalidated) {
1047 mItemsInReadingOrder.clear();
1048 for (int j = 0; j < mContent.getCountY(); j++) {
1049 for (int i = 0; i < mContent.getCountX(); i++) {
1050 View v = mContent.getChildAt(i, j);
1051 if (v != null) {
Adam Cohen76078c42011-06-09 15:06:52 -07001052 ShortcutInfo info = (ShortcutInfo) v.getTag();
1053 if (info != mCurrentDragInfo || includeCurrentDragItem) {
1054 mItemsInReadingOrder.add(v);
1055 }
Adam Cohen7c693212011-05-18 15:26:57 -07001056 }
1057 }
1058 }
1059 mItemsInvalidated = false;
1060 }
1061 return mItemsInReadingOrder;
1062 }
Adam Cohen8dfcba42011-07-07 16:38:18 -07001063
1064 public void getLocationInDragLayer(int[] loc) {
1065 mLauncher.getDragLayer().getLocationInDragLayer(this, loc);
1066 }
Adam Cohenea0818d2011-09-30 20:06:58 -07001067
1068 public void onFocusChange(View v, boolean hasFocus) {
1069 if (v == mFolderName && hasFocus) {
1070 startEditingFolderName();
1071 }
1072 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001073}