blob: 33b5de17c79045a5c313713cc9dced2d40d7505b [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;
86 private Rect mNewSize = new Rect();
Adam Cohen3e8f8112011-07-02 18:03:00 -070087 private Rect mIconRect = new Rect();
Adam Cohen7c693212011-05-18 15:26:57 -070088 private ArrayList<View> mItemsInReadingOrder = new ArrayList<View>();
Adam Cohenbadf71e2011-05-26 19:08:29 -070089 private Drawable mIconDrawable;
Adam Cohen7c693212011-05-18 15:26:57 -070090 boolean mItemsInvalidated = false;
Adam Cohenbfbfd262011-06-13 16:55:12 -070091 private ShortcutInfo mCurrentDragInfo;
92 private View mCurrentDragView;
93 boolean mSuppressOnAdd = false;
94 private int[] mTargetCell = new int[2];
95 private int[] mPreviousTargetCell = new int[2];
96 private int[] mEmptyCell = new int[2];
97 private Alarm mReorderAlarm = new Alarm();
98 private Alarm mOnExitAlarm = new Alarm();
Adam Cohen76fc0852011-06-17 13:26:23 -070099 private int mFolderNameHeight;
Adam Cohen8e776a62011-06-28 18:10:06 -0700100 private Rect mTempRect = new Rect();
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700101 private boolean mDragInProgress = false;
102 private boolean mDeleteFolderOnDropCompleted = false;
103 private boolean mSuppressFolderDeletion = false;
Adam Cohen05e0f402011-08-01 12:12:49 -0700104 private boolean mItemAddedBackToSelfViaIcon = false;
Adam Cohenac56cff2011-09-28 20:45:37 -0700105 FolderEditText mFolderName;
Adam Cohen228da5a2011-07-27 22:23:47 -0700106
Adam Cohen76fc0852011-06-17 13:26:23 -0700107 private boolean mIsEditingName = false;
108 private InputMethodManager mInputMethodManager;
Adam Cohendf2cc412011-04-27 16:56:57 -0700109
Adam Cohena65beee2011-06-27 21:32:23 -0700110 private static String sDefaultFolderName;
111 private static String sHintText;
112
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800113 /**
114 * Used to inflate the Workspace from XML.
115 *
116 * @param context The application's context.
117 * @param attrs The attribtues set containing the Workspace's customization values.
118 */
119 public Folder(Context context, AttributeSet attrs) {
120 super(context, attrs);
121 setAlwaysDrawnWithCacheEnabled(false);
Adam Cohendf2cc412011-04-27 16:56:57 -0700122 mInflater = LayoutInflater.from(context);
123 mIconCache = ((LauncherApplication)context.getApplicationContext()).getIconCache();
Adam Cohen8e776a62011-06-28 18:10:06 -0700124 mMaxCountX = LauncherModel.getCellCountX();
125 mMaxCountY = LauncherModel.getCellCountY();
Adam Cohen76fc0852011-06-17 13:26:23 -0700126
127 mInputMethodManager = (InputMethodManager)
128 mContext.getSystemService(Context.INPUT_METHOD_SERVICE);
129
130 Resources res = getResources();
131 mExpandDuration = res.getInteger(R.integer.config_folderAnimDuration);
Adam Cohen4ef610f2011-06-21 22:37:36 -0700132
133 if (sDefaultFolderName == null) {
134 sDefaultFolderName = res.getString(R.string.folder_name);
135 }
Adam Cohena65beee2011-06-27 21:32:23 -0700136 if (sHintText == null) {
137 sHintText = res.getString(R.string.folder_hint_text);
138 }
Adam Cohen4eac29a2011-07-11 17:53:37 -0700139 mLauncher = (Launcher) context;
Adam Cohenac56cff2011-09-28 20:45:37 -0700140 // We need this view to be focusable in touch mode so that when text editing of the folder
141 // name is complete, we have something to focus on, thus hiding the cursor and giving
142 // reliable behvior when clicking the text field (since it will always gain focus on click).
143 setFocusableInTouchMode(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800144 }
145
146 @Override
147 protected void onFinishInflate() {
148 super.onFinishInflate();
Adam Cohendf2cc412011-04-27 16:56:57 -0700149 mContent = (CellLayout) findViewById(R.id.folder_content);
Adam Cohen2801caf2011-05-13 20:57:39 -0700150 mContent.setGridSize(0, 0);
Adam Cohenac56cff2011-09-28 20:45:37 -0700151 mFolderName = (FolderEditText) findViewById(R.id.folder_name);
152 mFolderName.setFolder(this);
Adam Cohenea0818d2011-09-30 20:06:58 -0700153 mFolderName.setOnFocusChangeListener(this);
Adam Cohen76fc0852011-06-17 13:26:23 -0700154
155 // We find out how tall the text view wants to be (it is set to wrap_content), so that
156 // we can allocate the appropriate amount of space for it.
157 int measureSpec = MeasureSpec.UNSPECIFIED;
158 mFolderName.measure(measureSpec, measureSpec);
159 mFolderNameHeight = mFolderName.getMeasuredHeight();
160
161 // We disable action mode for now since it messes up the view on phones
162 mFolderName.setCustomSelectionActionModeCallback(mActionModeCallback);
Adam Cohen76fc0852011-06-17 13:26:23 -0700163 mFolderName.setOnEditorActionListener(this);
Adam Cohen4ef610f2011-06-21 22:37:36 -0700164 mFolderName.setSelectAllOnFocus(true);
Adam Cohen7a14d0b2011-06-24 11:36:35 -0700165 mFolderName.setInputType(mFolderName.getInputType() |
166 InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS | InputType.TYPE_TEXT_FLAG_CAP_WORDS);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800167 }
Adam Cohen2801caf2011-05-13 20:57:39 -0700168
Adam Cohen76fc0852011-06-17 13:26:23 -0700169 private ActionMode.Callback mActionModeCallback = new ActionMode.Callback() {
170 public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
171 return false;
172 }
173
174 public boolean onCreateActionMode(ActionMode mode, Menu menu) {
175 return false;
176 }
177
178 public void onDestroyActionMode(ActionMode mode) {
179 }
180
181 public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
182 return false;
183 }
184 };
185
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800186 public void onClick(View v) {
Adam Cohendf2cc412011-04-27 16:56:57 -0700187 Object tag = v.getTag();
188 if (tag instanceof ShortcutInfo) {
189 // refactor this code from Folder
190 ShortcutInfo item = (ShortcutInfo) tag;
191 int[] pos = new int[2];
192 v.getLocationOnScreen(pos);
193 item.intent.setSourceBounds(new Rect(pos[0], pos[1],
194 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
195 mLauncher.startActivitySafely(item.intent, item);
Adam Cohendf2cc412011-04-27 16:56:57 -0700196 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800197 }
198
199 public boolean onLongClick(View v) {
Adam Cohendf2cc412011-04-27 16:56:57 -0700200 Object tag = v.getTag();
201 if (tag instanceof ShortcutInfo) {
Adam Cohendf2cc412011-04-27 16:56:57 -0700202 ShortcutInfo item = (ShortcutInfo) tag;
203 if (!v.isInTouchMode()) {
204 return false;
205 }
206
Winson Chung7d7541e2011-09-16 20:14:36 -0700207 mLauncher.dismissFolderCling(null);
208
Adam Cohendf2cc412011-04-27 16:56:57 -0700209 mLauncher.getWorkspace().onDragStartedWithItem(v);
Adam Cohenac8c8762011-07-13 11:15:27 -0700210 mLauncher.getWorkspace().beginDragShared(v, this);
Adam Cohenbadf71e2011-05-26 19:08:29 -0700211 mIconDrawable = ((TextView) v).getCompoundDrawables()[1];
Adam Cohen76078c42011-06-09 15:06:52 -0700212
213 mCurrentDragInfo = item;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700214 mEmptyCell[0] = item.cellX;
215 mEmptyCell[1] = item.cellY;
216 mCurrentDragView = v;
Adam Cohenfc53cd22011-07-20 15:45:11 -0700217
218 mContent.removeView(mCurrentDragView);
219 mInfo.remove(mCurrentDragInfo);
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700220 mDragInProgress = true;
Adam Cohen05e0f402011-08-01 12:12:49 -0700221 mItemAddedBackToSelfViaIcon = false;
Adam Cohendf2cc412011-04-27 16:56:57 -0700222 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800223 return true;
224 }
225
Adam Cohen76fc0852011-06-17 13:26:23 -0700226 public boolean isEditingName() {
227 return mIsEditingName;
228 }
229
230 public void startEditingFolderName() {
Adam Cohena65beee2011-06-27 21:32:23 -0700231 mFolderName.setHint("");
Adam Cohen76fc0852011-06-17 13:26:23 -0700232 mIsEditingName = true;
233 }
234
235 public void dismissEditingName() {
236 mInputMethodManager.hideSoftInputFromWindow(getWindowToken(), 0);
237 doneEditingFolderName(true);
238 }
239
240 public void doneEditingFolderName(boolean commit) {
Adam Cohena65beee2011-06-27 21:32:23 -0700241 mFolderName.setHint(sHintText);
Adam Cohen1df26a32011-08-12 16:15:23 -0700242 // Convert to a string here to ensure that no other state associated with the text field
243 // gets saved.
Adam Cohen3371da02011-10-25 21:38:29 -0700244 String newTitle = mFolderName.getText().toString();
245 mInfo.setTitle(newTitle);
Adam Cohen76fc0852011-06-17 13:26:23 -0700246 LauncherModel.updateItemInDatabase(mLauncher, mInfo);
Adam Cohenac56cff2011-09-28 20:45:37 -0700247
Adam Cohen3371da02011-10-25 21:38:29 -0700248 if (commit) {
249 sendCustomAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED,
250 String.format(mContext.getString(R.string.folder_renamed), newTitle));
251 }
Adam Cohenac56cff2011-09-28 20:45:37 -0700252 // In order to clear the focus from the text field, we set the focus on ourself. This
253 // ensures that every time the field is clicked, focus is gained, giving reliable behavior.
254 requestFocus();
255
Adam Cohene601a432011-07-26 21:51:30 -0700256 Selection.setSelection((Spannable) mFolderName.getText(), 0, 0);
Adam Cohen76fc0852011-06-17 13:26:23 -0700257 mIsEditingName = false;
258 }
259
260 public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
261 if (actionId == EditorInfo.IME_ACTION_DONE) {
262 dismissEditingName();
263 return true;
264 }
265 return false;
266 }
267
268 public View getEditTextRegion() {
269 return mFolderName;
270 }
271
Adam Cohenbadf71e2011-05-26 19:08:29 -0700272 public Drawable getDragDrawable() {
273 return mIconDrawable;
274 }
275
Adam Cohen0c872ba2011-05-05 10:34:16 -0700276 /**
277 * We need to handle touch events to prevent them from falling through to the workspace below.
278 */
279 @Override
280 public boolean onTouchEvent(MotionEvent ev) {
281 return true;
282 }
283
Joe Onorato00acb122009-08-04 16:04:30 -0400284 public void setDragController(DragController dragController) {
285 mDragController = dragController;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800286 }
287
Adam Cohen2801caf2011-05-13 20:57:39 -0700288 void setFolderIcon(FolderIcon icon) {
289 mFolderIcon = icon;
290 }
291
Adam Cohen3371da02011-10-25 21:38:29 -0700292 @Override
293 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
294 // When the folder gets focus, we don't want to announce the list of items.
295 return true;
296 }
297
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800298 /**
299 * @return the FolderInfo object associated with this folder
300 */
301 FolderInfo getInfo() {
302 return mInfo;
303 }
304
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800305 void bind(FolderInfo info) {
306 mInfo = info;
Adam Cohendf2cc412011-04-27 16:56:57 -0700307 ArrayList<ShortcutInfo> children = info.contents;
Adam Cohenc508b2d2011-06-28 14:41:44 -0700308 ArrayList<ShortcutInfo> overflow = new ArrayList<ShortcutInfo>();
Adam Cohen7c693212011-05-18 15:26:57 -0700309 setupContentForNumItems(children.size());
Adam Cohen0057bbc2011-08-12 18:30:51 -0700310 int count = 0;
Adam Cohendf2cc412011-04-27 16:56:57 -0700311 for (int i = 0; i < children.size(); i++) {
312 ShortcutInfo child = (ShortcutInfo) children.get(i);
Adam Cohenc508b2d2011-06-28 14:41:44 -0700313 if (!createAndAddShortcut(child)) {
314 overflow.add(child);
Adam Cohen0057bbc2011-08-12 18:30:51 -0700315 } else {
316 count++;
Adam Cohenc508b2d2011-06-28 14:41:44 -0700317 }
Adam Cohendf2cc412011-04-27 16:56:57 -0700318 }
Adam Cohenc508b2d2011-06-28 14:41:44 -0700319
Adam Cohen0057bbc2011-08-12 18:30:51 -0700320 // We rearrange the items in case there are any empty gaps
321 setupContentForNumItems(count);
322
Adam Cohenc508b2d2011-06-28 14:41:44 -0700323 // If our folder has too many items we prune them from the list. This is an issue
324 // when upgrading from the old Folders implementation which could contain an unlimited
325 // number of items.
326 for (ShortcutInfo item: overflow) {
327 mInfo.remove(item);
328 LauncherModel.deleteItemFromDatabase(mLauncher, item);
329 }
330
Adam Cohen4dbe6d92011-05-18 17:14:15 -0700331 mItemsInvalidated = true;
Adam Cohenac56cff2011-09-28 20:45:37 -0700332 updateTextViewFocus();
Adam Cohena9cf38f2011-05-02 15:36:58 -0700333 mInfo.addListener(this);
Adam Cohen4ef610f2011-06-21 22:37:36 -0700334
Adam Cohenafb01ee2011-06-23 15:38:03 -0700335 if (!sDefaultFolderName.contentEquals(mInfo.title)) {
Adam Cohen4ef610f2011-06-21 22:37:36 -0700336 mFolderName.setText(mInfo.title);
337 } else {
338 mFolderName.setText("");
339 }
Adam Cohendf2cc412011-04-27 16:56:57 -0700340 }
341
342 /**
343 * Creates a new UserFolder, inflated from R.layout.user_folder.
344 *
345 * @param context The application's context.
346 *
347 * @return A new UserFolder.
348 */
349 static Folder fromXml(Context context) {
350 return (Folder) LayoutInflater.from(context).inflate(R.layout.user_folder, null);
351 }
352
353 /**
354 * This method is intended to make the UserFolder to be visually identical in size and position
355 * to its associated FolderIcon. This allows for a seamless transition into the expanded state.
356 */
357 private void positionAndSizeAsIcon() {
Adam Cohen3e8f8112011-07-02 18:03:00 -0700358 if (!(getParent() instanceof DragLayer)) return;
Adam Cohendf2cc412011-04-27 16:56:57 -0700359
Adam Cohen8e776a62011-06-28 18:10:06 -0700360 DragLayer.LayoutParams lp = (DragLayer.LayoutParams) getLayoutParams();
Adam Cohendf2cc412011-04-27 16:56:57 -0700361
Adam Cohen2801caf2011-05-13 20:57:39 -0700362 if (mMode == PARTIAL_GROW) {
363 setScaleX(0.8f);
364 setScaleY(0.8f);
365 setAlpha(0f);
366 } else {
Adam Cohen3e8f8112011-07-02 18:03:00 -0700367 mLauncher.getDragLayer().getDescendantRectRelativeToSelf(mFolderIcon, mIconRect);
368 lp.width = mIconRect.width();
369 lp.height = mIconRect.height();
370 lp.x = mIconRect.left;
371 lp.y = mIconRect.top;
Adam Cohen2801caf2011-05-13 20:57:39 -0700372 mContent.setAlpha(0);
373 }
Adam Cohendf2cc412011-04-27 16:56:57 -0700374 mState = STATE_SMALL;
375 }
376
377 public void animateOpen() {
Adam Cohen3e8f8112011-07-02 18:03:00 -0700378 positionAndSizeAsIcon();
379
Adam Cohen8e776a62011-06-28 18:10:06 -0700380 if (!(getParent() instanceof DragLayer)) return;
Adam Cohendf2cc412011-04-27 16:56:57 -0700381
Adam Cohen2801caf2011-05-13 20:57:39 -0700382 ObjectAnimator oa;
Adam Cohen8e776a62011-06-28 18:10:06 -0700383 DragLayer.LayoutParams lp = (DragLayer.LayoutParams) getLayoutParams();
Adam Cohendf2cc412011-04-27 16:56:57 -0700384
Adam Cohen2801caf2011-05-13 20:57:39 -0700385 centerAboutIcon();
386 if (mMode == PARTIAL_GROW) {
387 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1);
388 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.0f);
389 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.0f);
390 oa = ObjectAnimator.ofPropertyValuesHolder(this, alpha, scaleX, scaleY);
391 } else {
392 PropertyValuesHolder width = PropertyValuesHolder.ofInt("width", mNewSize.width());
393 PropertyValuesHolder height = PropertyValuesHolder.ofInt("height", mNewSize.height());
394 PropertyValuesHolder x = PropertyValuesHolder.ofInt("x", mNewSize.left);
395 PropertyValuesHolder y = PropertyValuesHolder.ofInt("y", mNewSize.top);
396 oa = ObjectAnimator.ofPropertyValuesHolder(lp, width, height, x, y);
397 oa.addUpdateListener(new AnimatorUpdateListener() {
398 public void onAnimationUpdate(ValueAnimator animation) {
399 requestLayout();
400 }
401 });
Adam Cohendf2cc412011-04-27 16:56:57 -0700402
Adam Cohen2801caf2011-05-13 20:57:39 -0700403 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1.0f);
404 ObjectAnimator alphaOa = ObjectAnimator.ofPropertyValuesHolder(mContent, alpha);
405 alphaOa.setDuration(mExpandDuration);
406 alphaOa.setInterpolator(new AccelerateInterpolator(2.0f));
407 alphaOa.start();
408 }
Adam Cohendf2cc412011-04-27 16:56:57 -0700409
Adam Cohen2801caf2011-05-13 20:57:39 -0700410 oa.addListener(new AnimatorListenerAdapter() {
Adam Cohendf2cc412011-04-27 16:56:57 -0700411 @Override
412 public void onAnimationStart(Animator animation) {
Adam Cohen3371da02011-10-25 21:38:29 -0700413 sendCustomAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED,
414 String.format(mContext.getString(R.string.folder_opened),
415 mContent.getCountX(), mContent.getCountY()));
Adam Cohendf2cc412011-04-27 16:56:57 -0700416 mState = STATE_ANIMATING;
417 }
418 @Override
419 public void onAnimationEnd(Animator animation) {
Adam Cohen2801caf2011-05-13 20:57:39 -0700420 mState = STATE_OPEN;
Winson Chung7a74ac92011-09-20 17:43:51 -0700421
422 Cling cling = mLauncher.showFirstRunFoldersCling();
423 if (cling != null) {
424 cling.bringToFront();
425 }
Adam Cohenac56cff2011-09-28 20:45:37 -0700426 setFocusOnFirstChild();
Adam Cohendf2cc412011-04-27 16:56:57 -0700427 }
428 });
Adam Cohen2801caf2011-05-13 20:57:39 -0700429 oa.setDuration(mExpandDuration);
430 oa.start();
Adam Cohendf2cc412011-04-27 16:56:57 -0700431 }
432
Adam Cohen3371da02011-10-25 21:38:29 -0700433 private void sendCustomAccessibilityEvent(int type, String text) {
434 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
435 AccessibilityEvent event = AccessibilityEvent.obtain(type);
436 onInitializeAccessibilityEvent(event);
437 event.getText().add(text);
438 AccessibilityManager.getInstance(mContext).sendAccessibilityEvent(event);
439 }
440 }
441
Adam Cohenac56cff2011-09-28 20:45:37 -0700442 private void setFocusOnFirstChild() {
443 View firstChild = mContent.getChildAt(0, 0);
444 if (firstChild != null) {
445 firstChild.requestFocus();
446 }
447 }
448
Adam Cohendf2cc412011-04-27 16:56:57 -0700449 public void animateClosed() {
Adam Cohen8e776a62011-06-28 18:10:06 -0700450 if (!(getParent() instanceof DragLayer)) return;
Adam Cohendf2cc412011-04-27 16:56:57 -0700451
Adam Cohen2801caf2011-05-13 20:57:39 -0700452 ObjectAnimator oa;
Adam Cohen2801caf2011-05-13 20:57:39 -0700453 if (mMode == PARTIAL_GROW) {
454 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0);
455 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 0.9f);
456 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 0.9f);
457 oa = ObjectAnimator.ofPropertyValuesHolder(this, alpha, scaleX, scaleY);
458 } else {
Adam Cohen8e776a62011-06-28 18:10:06 -0700459 DragLayer.LayoutParams lp = (DragLayer.LayoutParams) getLayoutParams();
Adam Cohendf2cc412011-04-27 16:56:57 -0700460
Adam Cohen3e8f8112011-07-02 18:03:00 -0700461 PropertyValuesHolder width = PropertyValuesHolder.ofInt("width", mIconRect.width());
462 PropertyValuesHolder height = PropertyValuesHolder.ofInt("height", mIconRect.height());
463 PropertyValuesHolder x = PropertyValuesHolder.ofInt("x", mIconRect.left);
464 PropertyValuesHolder y = PropertyValuesHolder.ofInt("y", mIconRect.top);
Adam Cohen2801caf2011-05-13 20:57:39 -0700465 oa = ObjectAnimator.ofPropertyValuesHolder(lp, width, height, x, y);
466 oa.addUpdateListener(new AnimatorUpdateListener() {
467 public void onAnimationUpdate(ValueAnimator animation) {
468 requestLayout();
469 }
470 });
Adam Cohendf2cc412011-04-27 16:56:57 -0700471
Adam Cohen2801caf2011-05-13 20:57:39 -0700472 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0f);
473 ObjectAnimator alphaOa = ObjectAnimator.ofPropertyValuesHolder(mContent, alpha);
474 alphaOa.setDuration(mExpandDuration);
475 alphaOa.setInterpolator(new DecelerateInterpolator(2.0f));
476 alphaOa.start();
477 }
Adam Cohendf2cc412011-04-27 16:56:57 -0700478
Adam Cohen2801caf2011-05-13 20:57:39 -0700479 oa.addListener(new AnimatorListenerAdapter() {
Adam Cohendf2cc412011-04-27 16:56:57 -0700480 @Override
481 public void onAnimationEnd(Animator animation) {
Adam Cohen2801caf2011-05-13 20:57:39 -0700482 onCloseComplete();
Adam Cohen2801caf2011-05-13 20:57:39 -0700483 mState = STATE_SMALL;
Adam Cohendf2cc412011-04-27 16:56:57 -0700484 }
485 @Override
486 public void onAnimationStart(Animator animation) {
Adam Cohen3371da02011-10-25 21:38:29 -0700487 sendCustomAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED,
488 mContext.getString(R.string.folder_closed));
Adam Cohendf2cc412011-04-27 16:56:57 -0700489 mState = STATE_ANIMATING;
490 }
491 });
Adam Cohen2801caf2011-05-13 20:57:39 -0700492 oa.setDuration(mExpandDuration);
493 oa.start();
Adam Cohendf2cc412011-04-27 16:56:57 -0700494 }
495
496 void notifyDataSetChanged() {
497 // recreate all the children if the data set changes under us. We may want to do this more
498 // intelligently (ie just removing the views that should no longer exist)
499 mContent.removeAllViewsInLayout();
500 bind(mInfo);
501 }
502
Adam Cohencb3382b2011-05-24 14:07:08 -0700503 public boolean acceptDrop(DragObject d) {
504 final ItemInfo item = (ItemInfo) d.dragInfo;
Adam Cohendf2cc412011-04-27 16:56:57 -0700505 final int itemType = item.itemType;
Adam Cohen2801caf2011-05-13 20:57:39 -0700506 return ((itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION ||
507 itemType == LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT) &&
508 !isFull());
Adam Cohendf2cc412011-04-27 16:56:57 -0700509 }
510
Adam Cohendf2cc412011-04-27 16:56:57 -0700511 protected boolean findAndSetEmptyCells(ShortcutInfo item) {
512 int[] emptyCell = new int[2];
513 if (mContent.findCellForSpan(emptyCell, item.spanX, item.spanY)) {
514 item.cellX = emptyCell[0];
515 item.cellY = emptyCell[1];
Adam Cohendf2cc412011-04-27 16:56:57 -0700516 return true;
517 } else {
518 return false;
519 }
520 }
521
Adam Cohenc508b2d2011-06-28 14:41:44 -0700522 protected boolean createAndAddShortcut(ShortcutInfo item) {
Adam Cohendf2cc412011-04-27 16:56:57 -0700523 final TextView textView =
Adam Cohene87b9242011-06-29 14:01:26 -0700524 (TextView) mInflater.inflate(R.layout.application, this, false);
Adam Cohendf2cc412011-04-27 16:56:57 -0700525 textView.setCompoundDrawablesWithIntrinsicBounds(null,
526 new FastBitmapDrawable(item.getIcon(mIconCache)), null, null);
527 textView.setText(item.title);
528 textView.setTag(item);
529
530 textView.setOnClickListener(this);
531 textView.setOnLongClickListener(this);
532
Adam Cohenc508b2d2011-06-28 14:41:44 -0700533 // We need to check here to verify that the given item's location isn't already occupied
534 // by another item. If it is, we need to find the next available slot and assign
535 // it that position. This is an issue when upgrading from the old Folders implementation
536 // which could contain an unlimited number of items.
Adam Cohen0057bbc2011-08-12 18:30:51 -0700537 if (mContent.getChildAt(item.cellX, item.cellY) != null || item.cellX < 0 || item.cellY < 0
538 || item.cellX >= mContent.getCountX() || item.cellY >= mContent.getCountY()) {
Adam Cohenc508b2d2011-06-28 14:41:44 -0700539 if (!findAndSetEmptyCells(item)) {
540 return false;
541 }
542 }
543
Adam Cohendf2cc412011-04-27 16:56:57 -0700544 CellLayout.LayoutParams lp =
545 new CellLayout.LayoutParams(item.cellX, item.cellY, item.spanX, item.spanY);
546 boolean insert = false;
Adam Cohenac56cff2011-09-28 20:45:37 -0700547 textView.setOnKeyListener(new FolderKeyEventListener());
Adam Cohendf2cc412011-04-27 16:56:57 -0700548 mContent.addViewToCellLayout(textView, insert ? 0 : -1, (int)item.id, lp, true);
Adam Cohenc508b2d2011-06-28 14:41:44 -0700549 return true;
Adam Cohendf2cc412011-04-27 16:56:57 -0700550 }
551
Adam Cohencb3382b2011-05-24 14:07:08 -0700552 public void onDragEnter(DragObject d) {
Adam Cohenbfbfd262011-06-13 16:55:12 -0700553 mPreviousTargetCell[0] = -1;
554 mPreviousTargetCell[1] = -1;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700555 mOnExitAlarm.cancelAlarm();
556 }
557
558 OnAlarmListener mReorderAlarmListener = new OnAlarmListener() {
559 public void onAlarm(Alarm alarm) {
560 realTimeReorder(mEmptyCell, mTargetCell);
561 }
562 };
563
564 boolean readingOrderGreaterThan(int[] v1, int[] v2) {
565 if (v1[1] > v2[1] || (v1[1] == v2[1] && v1[0] > v2[0])) {
566 return true;
567 } else {
568 return false;
569 }
570 }
571
572 private void realTimeReorder(int[] empty, int[] target) {
573 boolean wrap;
574 int startX;
575 int endX;
576 int startY;
Adam Cohen76fc0852011-06-17 13:26:23 -0700577 int delay = 0;
578 float delayAmount = 30;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700579 if (readingOrderGreaterThan(target, empty)) {
580 wrap = empty[0] >= mContent.getCountX() - 1;
581 startY = wrap ? empty[1] + 1 : empty[1];
582 for (int y = startY; y <= target[1]; y++) {
583 startX = y == empty[1] ? empty[0] + 1 : 0;
584 endX = y < target[1] ? mContent.getCountX() - 1 : target[0];
585 for (int x = startX; x <= endX; x++) {
586 View v = mContent.getChildAt(x,y);
587 if (mContent.animateChildToPosition(v, empty[0], empty[1],
Adam Cohen76fc0852011-06-17 13:26:23 -0700588 REORDER_ANIMATION_DURATION, delay)) {
Adam Cohenbfbfd262011-06-13 16:55:12 -0700589 empty[0] = x;
590 empty[1] = y;
Adam Cohen76fc0852011-06-17 13:26:23 -0700591 delay += delayAmount;
592 delayAmount *= 0.9;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700593 }
594 }
595 }
596 } else {
597 wrap = empty[0] == 0;
598 startY = wrap ? empty[1] - 1 : empty[1];
599 for (int y = startY; y >= target[1]; y--) {
600 startX = y == empty[1] ? empty[0] - 1 : mContent.getCountX() - 1;
601 endX = y > target[1] ? 0 : target[0];
602 for (int x = startX; x >= endX; x--) {
603 View v = mContent.getChildAt(x,y);
604 if (mContent.animateChildToPosition(v, empty[0], empty[1],
Adam Cohen76fc0852011-06-17 13:26:23 -0700605 REORDER_ANIMATION_DURATION, delay)) {
Adam Cohenbfbfd262011-06-13 16:55:12 -0700606 empty[0] = x;
607 empty[1] = y;
Adam Cohen76fc0852011-06-17 13:26:23 -0700608 delay += delayAmount;
609 delayAmount *= 0.9;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700610 }
611 }
612 }
613 }
Adam Cohendf2cc412011-04-27 16:56:57 -0700614 }
615
Adam Cohencb3382b2011-05-24 14:07:08 -0700616 public void onDragOver(DragObject d) {
Adam Cohenbfbfd262011-06-13 16:55:12 -0700617 float[] r = getDragViewVisualCenter(d.x, d.y, d.xOffset, d.yOffset, d.dragView, null);
618 mTargetCell = mContent.findNearestArea((int) r[0], (int) r[1], 1, 1, mTargetCell);
619
620 if (mTargetCell[0] != mPreviousTargetCell[0] || mTargetCell[1] != mPreviousTargetCell[1]) {
621 mReorderAlarm.cancelAlarm();
622 mReorderAlarm.setOnAlarmListener(mReorderAlarmListener);
623 mReorderAlarm.setAlarm(150);
624 mPreviousTargetCell[0] = mTargetCell[0];
625 mPreviousTargetCell[1] = mTargetCell[1];
626 }
Adam Cohendf2cc412011-04-27 16:56:57 -0700627 }
628
Adam Cohenbfbfd262011-06-13 16:55:12 -0700629 // This is used to compute the visual center of the dragView. The idea is that
630 // the visual center represents the user's interpretation of where the item is, and hence
631 // is the appropriate point to use when determining drop location.
632 private float[] getDragViewVisualCenter(int x, int y, int xOffset, int yOffset,
633 DragView dragView, float[] recycle) {
634 float res[];
635 if (recycle == null) {
636 res = new float[2];
637 } else {
638 res = recycle;
639 }
640
641 // These represent the visual top and left of drag view if a dragRect was provided.
642 // If a dragRect was not provided, then they correspond to the actual view left and
643 // top, as the dragRect is in that case taken to be the entire dragView.
644 // R.dimen.dragViewOffsetY.
645 int left = x - xOffset;
646 int top = y - yOffset;
647
648 // In order to find the visual center, we shift by half the dragRect
649 res[0] = left + dragView.getDragRegion().width() / 2;
650 res[1] = top + dragView.getDragRegion().height() / 2;
651
652 return res;
653 }
654
655 OnAlarmListener mOnExitAlarmListener = new OnAlarmListener() {
656 public void onAlarm(Alarm alarm) {
Adam Cohen3e8f8112011-07-02 18:03:00 -0700657 completeDragExit();
Adam Cohenbfbfd262011-06-13 16:55:12 -0700658 }
659 };
660
Adam Cohen95bb8002011-07-03 23:40:28 -0700661 public void completeDragExit() {
Adam Cohen3e8f8112011-07-02 18:03:00 -0700662 mLauncher.closeFolder();
663 mCurrentDragInfo = null;
664 mCurrentDragView = null;
665 mSuppressOnAdd = false;
666 mRearrangeOnClose = true;
667 }
668
Adam Cohencb3382b2011-05-24 14:07:08 -0700669 public void onDragExit(DragObject d) {
Adam Cohenbfbfd262011-06-13 16:55:12 -0700670 // We only close the folder if this is a true drag exit, ie. not because a drop
671 // has occurred above the folder.
672 if (!d.dragComplete) {
673 mOnExitAlarm.setOnAlarmListener(mOnExitAlarmListener);
674 mOnExitAlarm.setAlarm(ON_EXIT_CLOSE_DELAY);
675 }
676 mReorderAlarm.cancelAlarm();
Adam Cohen2801caf2011-05-13 20:57:39 -0700677 }
678
Adam Cohenc0dcf592011-06-01 15:30:43 -0700679 public void onDropCompleted(View target, DragObject d, boolean success) {
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700680 if (success) {
Adam Cohen05e0f402011-08-01 12:12:49 -0700681 if (mDeleteFolderOnDropCompleted && !mItemAddedBackToSelfViaIcon) {
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700682 replaceFolderWithFinalItem();
683 }
684 } else {
685 // The drag failed, we need to return the item to the folder
686 mFolderIcon.onDrop(d);
687
688 // We're going to trigger a "closeFolder" which may occur before this item has
689 // been added back to the folder -- this could cause the folder to be deleted
690 if (mOnExitAlarm.alarmPending()) {
691 mSuppressFolderDeletion = true;
692 }
693 }
694
695 if (target != this) {
696 if (mOnExitAlarm.alarmPending()) {
697 mOnExitAlarm.cancelAlarm();
698 completeDragExit();
699 }
700 }
701 mDeleteFolderOnDropCompleted = false;
702 mDragInProgress = false;
Adam Cohen05e0f402011-08-01 12:12:49 -0700703 mItemAddedBackToSelfViaIcon = false;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700704 mCurrentDragInfo = null;
705 mCurrentDragView = null;
706 mSuppressOnAdd = false;
Adam Cohen4045eb72011-10-06 11:44:26 -0700707
708 // Reordering may have occured, and we need to save the new item locations. We do this once
709 // at the end to prevent unnecessary database operations.
710 updateItemLocationsInDatabase();
711 }
712
713 private void updateItemLocationsInDatabase() {
714 ArrayList<View> list = getItemsInReadingOrder();
715 for (int i = 0; i < list.size(); i++) {
716 View v = list.get(i);
717 ItemInfo info = (ItemInfo) v.getTag();
718 LauncherModel.moveItemInDatabase(mLauncher, info, mInfo.id, 0,
719 info.cellX, info.cellY);
720 }
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700721 }
Adam Cohen228da5a2011-07-27 22:23:47 -0700722
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700723 public void notifyDrop() {
724 if (mDragInProgress) {
Adam Cohen05e0f402011-08-01 12:12:49 -0700725 mItemAddedBackToSelfViaIcon = true;
Adam Cohen76078c42011-06-09 15:06:52 -0700726 }
Adam Cohendf2cc412011-04-27 16:56:57 -0700727 }
728
729 public boolean isDropEnabled() {
730 return true;
731 }
732
Adam Cohencb3382b2011-05-24 14:07:08 -0700733 public DropTarget getDropTargetDelegate(DragObject d) {
Adam Cohendf2cc412011-04-27 16:56:57 -0700734 return null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800735 }
Adam Cohena9cf38f2011-05-02 15:36:58 -0700736
Adam Cohen4045eb72011-10-06 11:44:26 -0700737 private void setupContentDimensions(int count) {
Adam Cohen2801caf2011-05-13 20:57:39 -0700738 ArrayList<View> list = getItemsInReadingOrder();
739
740 int countX = mContent.getCountX();
741 int countY = mContent.getCountY();
Adam Cohen7c693212011-05-18 15:26:57 -0700742 boolean done = false;
Adam Cohen2801caf2011-05-13 20:57:39 -0700743
Adam Cohen7c693212011-05-18 15:26:57 -0700744 while (!done) {
745 int oldCountX = countX;
746 int oldCountY = countY;
747 if (countX * countY < count) {
748 // Current grid is too small, expand it
749 if (countX <= countY && countX < mMaxCountX) {
750 countX++;
751 } else if (countY < mMaxCountY) {
752 countY++;
753 }
754 if (countY == 0) countY++;
755 } else if ((countY - 1) * countX >= count && countY >= countX) {
756 countY = Math.max(0, countY - 1);
757 } else if ((countX - 1) * countY >= count) {
758 countX = Math.max(0, countX - 1);
Adam Cohen2801caf2011-05-13 20:57:39 -0700759 }
Adam Cohen7c693212011-05-18 15:26:57 -0700760 done = countX == oldCountX && countY == oldCountY;
Adam Cohen2801caf2011-05-13 20:57:39 -0700761 }
Adam Cohen7c693212011-05-18 15:26:57 -0700762 mContent.setGridSize(countX, countY);
Adam Cohen2801caf2011-05-13 20:57:39 -0700763 arrangeChildren(list);
764 }
765
766 public boolean isFull() {
767 return getItemCount() >= mMaxCountX * mMaxCountY;
768 }
769
770 private void centerAboutIcon() {
Adam Cohen8e776a62011-06-28 18:10:06 -0700771 DragLayer.LayoutParams lp = (DragLayer.LayoutParams) getLayoutParams();
Adam Cohen2801caf2011-05-13 20:57:39 -0700772
773 int width = getPaddingLeft() + getPaddingRight() + mContent.getDesiredWidth();
Adam Cohenf4bb1cd2011-07-22 14:36:03 -0700774 int height = getPaddingTop() + getPaddingBottom() + mContent.getDesiredHeight()
775 + mFolderNameHeight;
Adam Cohen8e776a62011-06-28 18:10:06 -0700776 DragLayer parent = (DragLayer) mLauncher.findViewById(R.id.drag_layer);
Adam Cohen2801caf2011-05-13 20:57:39 -0700777
Adam Cohen8e776a62011-06-28 18:10:06 -0700778 parent.getDescendantRectRelativeToSelf(mFolderIcon, mTempRect);
779
780 int centerX = mTempRect.centerX();
781 int centerY = mTempRect.centerY();
Adam Cohen2801caf2011-05-13 20:57:39 -0700782 int centeredLeft = centerX - width / 2;
783 int centeredTop = centerY - height / 2;
784
Adam Cohen35e7e642011-07-17 14:47:18 -0700785 // We first fetch the currently visible CellLayoutChildren
Winson Chung3d503fb2011-07-13 17:25:49 -0700786 CellLayout currentPage = mLauncher.getWorkspace().getCurrentDropLayout();
Adam Cohen35e7e642011-07-17 14:47:18 -0700787 CellLayoutChildren boundingLayout = currentPage.getChildrenLayout();
788 Rect bounds = new Rect();
789 parent.getDescendantRectRelativeToSelf(boundingLayout, bounds);
Adam Cohen2801caf2011-05-13 20:57:39 -0700790
Adam Cohen35e7e642011-07-17 14:47:18 -0700791 // We need to bound the folder to the currently visible CellLayoutChildren
792 int left = Math.min(Math.max(bounds.left, centeredLeft),
793 bounds.left + bounds.width() - width);
794 int top = Math.min(Math.max(bounds.top, centeredTop),
795 bounds.top + bounds.height() - height);
796 // If the folder doesn't fit within the bounds, center it about the desired bounds
797 if (width >= bounds.width()) {
798 left = bounds.left + (bounds.width() - width) / 2;
Adam Cohen0e4857c2011-06-30 17:05:14 -0700799 }
Adam Cohen35e7e642011-07-17 14:47:18 -0700800 if (height >= bounds.height()) {
801 top = bounds.top + (bounds.height() - height) / 2;
Adam Cohen0e4857c2011-06-30 17:05:14 -0700802 }
Adam Cohen2801caf2011-05-13 20:57:39 -0700803
804 int folderPivotX = width / 2 + (centeredLeft - left);
805 int folderPivotY = height / 2 + (centeredTop - top);
806 setPivotX(folderPivotX);
807 setPivotY(folderPivotY);
808 int folderIconPivotX = (int) (mFolderIcon.getMeasuredWidth() *
809 (1.0f * folderPivotX / width));
810 int folderIconPivotY = (int) (mFolderIcon.getMeasuredHeight() *
811 (1.0f * folderPivotY / height));
812 mFolderIcon.setPivotX(folderIconPivotX);
813 mFolderIcon.setPivotY(folderIconPivotY);
814
815 if (mMode == PARTIAL_GROW) {
816 lp.width = width;
817 lp.height = height;
818 lp.x = left;
819 lp.y = top;
820 } else {
821 mNewSize.set(left, top, left + width, top + height);
822 }
823 }
824
825 private void setupContentForNumItems(int count) {
Adam Cohen4045eb72011-10-06 11:44:26 -0700826 setupContentDimensions(count);
Adam Cohen2801caf2011-05-13 20:57:39 -0700827
Adam Cohen8e776a62011-06-28 18:10:06 -0700828 DragLayer.LayoutParams lp = (DragLayer.LayoutParams) getLayoutParams();
Adam Cohen2801caf2011-05-13 20:57:39 -0700829 if (lp == null) {
Adam Cohen8e776a62011-06-28 18:10:06 -0700830 lp = new DragLayer.LayoutParams(0, 0);
831 lp.customPosition = true;
Adam Cohen2801caf2011-05-13 20:57:39 -0700832 setLayoutParams(lp);
833 }
834 centerAboutIcon();
835 }
836
Adam Cohen234c4cd2011-07-17 21:03:04 -0700837 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
838 int width = getPaddingLeft() + getPaddingRight() + mContent.getDesiredWidth();
Adam Cohenf4bb1cd2011-07-22 14:36:03 -0700839 int height = getPaddingTop() + getPaddingBottom() + mContent.getDesiredHeight()
840 + mFolderNameHeight;
Adam Cohen234c4cd2011-07-17 21:03:04 -0700841
842 int contentWidthSpec = MeasureSpec.makeMeasureSpec(mContent.getDesiredWidth(),
843 MeasureSpec.EXACTLY);
844 int contentHeightSpec = MeasureSpec.makeMeasureSpec(mContent.getDesiredHeight(),
845 MeasureSpec.EXACTLY);
846 mContent.measure(contentWidthSpec, contentHeightSpec);
847
848 mFolderName.measure(contentWidthSpec,
849 MeasureSpec.makeMeasureSpec(mFolderNameHeight, MeasureSpec.EXACTLY));
850 setMeasuredDimension(width, height);
851 }
852
Adam Cohen2801caf2011-05-13 20:57:39 -0700853 private void arrangeChildren(ArrayList<View> list) {
854 int[] vacant = new int[2];
855 if (list == null) {
856 list = getItemsInReadingOrder();
857 }
858 mContent.removeAllViews();
859
860 for (int i = 0; i < list.size(); i++) {
861 View v = list.get(i);
862 mContent.getVacantCell(vacant, 1, 1);
863 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) v.getLayoutParams();
864 lp.cellX = vacant[0];
865 lp.cellY = vacant[1];
866 ItemInfo info = (ItemInfo) v.getTag();
Adam Cohen2792a332011-09-26 21:09:47 -0700867 if (info.cellX != vacant[0] || info.cellY != vacant[1]) {
868 info.cellX = vacant[0];
869 info.cellY = vacant[1];
870 LauncherModel.addOrMoveItemInDatabase(mLauncher, info, mInfo.id, 0,
871 info.cellX, info.cellY);
872 }
Adam Cohen2801caf2011-05-13 20:57:39 -0700873 boolean insert = false;
874 mContent.addViewToCellLayout(v, insert ? 0 : -1, (int)info.id, lp, true);
875 }
Adam Cohen7c693212011-05-18 15:26:57 -0700876 mItemsInvalidated = true;
Adam Cohen2801caf2011-05-13 20:57:39 -0700877 }
878
Adam Cohena9cf38f2011-05-02 15:36:58 -0700879 public int getItemCount() {
880 return mContent.getChildrenLayout().getChildCount();
881 }
882
883 public View getItemAt(int index) {
884 return mContent.getChildrenLayout().getChildAt(index);
885 }
886
Adam Cohen2801caf2011-05-13 20:57:39 -0700887 private void onCloseComplete() {
Adam Cohen05e0f402011-08-01 12:12:49 -0700888 DragLayer parent = (DragLayer) getParent();
Adam Cohen4554ee12011-08-03 16:13:21 -0700889 parent.removeView(this);
890 mDragController.removeDropTarget((DropTarget) this);
Adam Cohen05e0f402011-08-01 12:12:49 -0700891 clearFocus();
Adam Cohenac56cff2011-09-28 20:45:37 -0700892 mFolderIcon.requestFocus();
Adam Cohen05e0f402011-08-01 12:12:49 -0700893
Adam Cohen2801caf2011-05-13 20:57:39 -0700894 if (mRearrangeOnClose) {
895 setupContentForNumItems(getItemCount());
896 mRearrangeOnClose = false;
897 }
Adam Cohenafb01ee2011-06-23 15:38:03 -0700898 if (getItemCount() <= 1) {
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700899 if (!mDragInProgress && !mSuppressFolderDeletion) {
900 replaceFolderWithFinalItem();
901 } else if (mDragInProgress) {
902 mDeleteFolderOnDropCompleted = true;
903 }
Adam Cohenafb01ee2011-06-23 15:38:03 -0700904 }
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700905 mSuppressFolderDeletion = false;
Adam Cohenafb01ee2011-06-23 15:38:03 -0700906 }
907
908 private void replaceFolderWithFinalItem() {
909 ItemInfo finalItem = null;
910
911 if (getItemCount() == 1) {
912 finalItem = mInfo.contents.get(0);
913 }
914
915 // Remove the folder completely
Winson Chung3d503fb2011-07-13 17:25:49 -0700916 CellLayout cellLayout = mLauncher.getCellLayout(mInfo.container, mInfo.screen);
Adam Cohenafb01ee2011-06-23 15:38:03 -0700917 cellLayout.removeView(mFolderIcon);
918 if (mFolderIcon instanceof DropTarget) {
919 mDragController.removeDropTarget((DropTarget) mFolderIcon);
920 }
921 mLauncher.removeFolder(mInfo);
922
923 if (finalItem != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -0700924 LauncherModel.addOrMoveItemInDatabase(mLauncher, finalItem, mInfo.container,
925 mInfo.screen, mInfo.cellX, mInfo.cellY);
Adam Cohenafb01ee2011-06-23 15:38:03 -0700926 }
Adam Cohen716b51e2011-06-30 12:09:54 -0700927 LauncherModel.deleteItemFromDatabase(mLauncher, mInfo);
Adam Cohenafb01ee2011-06-23 15:38:03 -0700928
929 // Add the last remaining child to the workspace in place of the folder
930 if (finalItem != null) {
931 View child = mLauncher.createShortcut(R.layout.application, cellLayout,
932 (ShortcutInfo) finalItem);
933
Winson Chung3d503fb2011-07-13 17:25:49 -0700934 mLauncher.getWorkspace().addInScreen(child, mInfo.container, mInfo.screen, mInfo.cellX,
935 mInfo.cellY, mInfo.spanX, mInfo.spanY);
Adam Cohenafb01ee2011-06-23 15:38:03 -0700936 }
Adam Cohen2801caf2011-05-13 20:57:39 -0700937 }
938
Adam Cohenac56cff2011-09-28 20:45:37 -0700939 // This method keeps track of the last item in the folder for the purposes
940 // of keyboard focus
941 private void updateTextViewFocus() {
942 View lastChild = getItemAt(getItemCount() - 1);
943 getItemAt(getItemCount() - 1);
944 if (lastChild != null) {
945 mFolderName.setNextFocusDownId(lastChild.getId());
946 mFolderName.setNextFocusRightId(lastChild.getId());
947 mFolderName.setNextFocusLeftId(lastChild.getId());
948 mFolderName.setNextFocusUpId(lastChild.getId());
949 }
950 }
951
Adam Cohenbfbfd262011-06-13 16:55:12 -0700952 public void onDrop(DragObject d) {
953 ShortcutInfo item;
954 if (d.dragInfo instanceof ApplicationInfo) {
955 // Came from all apps -- make a copy
956 item = ((ApplicationInfo) d.dragInfo).makeShortcut();
957 item.spanX = 1;
958 item.spanY = 1;
959 } else {
960 item = (ShortcutInfo) d.dragInfo;
961 }
Adam Cohen05e0f402011-08-01 12:12:49 -0700962 // Dragged from self onto self, currently this is the only path possible, however
963 // we keep this as a distinct code path.
Adam Cohenbfbfd262011-06-13 16:55:12 -0700964 if (item == mCurrentDragInfo) {
965 ShortcutInfo si = (ShortcutInfo) mCurrentDragView.getTag();
966 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) mCurrentDragView.getLayoutParams();
967 si.cellX = lp.cellX = mEmptyCell[0];
968 si.cellX = lp.cellY = mEmptyCell[1];
969 mContent.addViewToCellLayout(mCurrentDragView, -1, (int)item.id, lp, true);
Adam Cohenfc53cd22011-07-20 15:45:11 -0700970 if (d.dragView.hasDrawn()) {
971 mLauncher.getDragLayer().animateViewIntoPosition(d.dragView, mCurrentDragView);
972 } else {
973 mCurrentDragView.setVisibility(VISIBLE);
974 }
Adam Cohene9166b22011-07-08 17:11:11 -0700975 mItemsInvalidated = true;
Adam Cohen4045eb72011-10-06 11:44:26 -0700976 setupContentDimensions(getItemCount());
Adam Cohen716b51e2011-06-30 12:09:54 -0700977 mSuppressOnAdd = true;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700978 }
979 mInfo.add(item);
980 }
981
982 public void onAdd(ShortcutInfo item) {
983 mItemsInvalidated = true;
Adam Cohen05e0f402011-08-01 12:12:49 -0700984 // If the item was dropped onto this open folder, we have done the work associated
985 // with adding the item to the folder, as indicated by mSuppressOnAdd being set
Adam Cohenbfbfd262011-06-13 16:55:12 -0700986 if (mSuppressOnAdd) return;
987 if (!findAndSetEmptyCells(item)) {
988 // The current layout is full, can we expand it?
989 setupContentForNumItems(getItemCount() + 1);
990 findAndSetEmptyCells(item);
991 }
992 createAndAddShortcut(item);
993 LauncherModel.addOrMoveItemInDatabase(
994 mLauncher, item, mInfo.id, 0, item.cellX, item.cellY);
995 }
996
Adam Cohena9cf38f2011-05-02 15:36:58 -0700997 public void onRemove(ShortcutInfo item) {
Adam Cohen7c693212011-05-18 15:26:57 -0700998 mItemsInvalidated = true;
Adam Cohen05e0f402011-08-01 12:12:49 -0700999 // If this item is being dragged from this open folder, we have already handled
1000 // the work associated with removing the item, so we don't have to do anything here.
Adam Cohenbfbfd262011-06-13 16:55:12 -07001001 if (item == mCurrentDragInfo) return;
Adam Cohendf1e4e82011-06-24 15:57:39 -07001002 View v = getViewForInfo(item);
Adam Cohena9cf38f2011-05-02 15:36:58 -07001003 mContent.removeView(v);
Adam Cohen2801caf2011-05-13 20:57:39 -07001004 if (mState == STATE_ANIMATING) {
1005 mRearrangeOnClose = true;
1006 } else {
1007 setupContentForNumItems(getItemCount());
1008 }
Adam Cohenafb01ee2011-06-23 15:38:03 -07001009 if (getItemCount() <= 1) {
1010 replaceFolderWithFinalItem();
1011 }
Adam Cohena9cf38f2011-05-02 15:36:58 -07001012 }
Adam Cohen7c693212011-05-18 15:26:57 -07001013
Adam Cohendf1e4e82011-06-24 15:57:39 -07001014 private View getViewForInfo(ShortcutInfo item) {
1015 for (int j = 0; j < mContent.getCountY(); j++) {
1016 for (int i = 0; i < mContent.getCountX(); i++) {
1017 View v = mContent.getChildAt(i, j);
1018 if (v.getTag() == item) {
1019 return v;
1020 }
1021 }
1022 }
1023 return null;
1024 }
1025
Adam Cohen76078c42011-06-09 15:06:52 -07001026 public void onItemsChanged() {
Adam Cohenac56cff2011-09-28 20:45:37 -07001027 updateTextViewFocus();
Adam Cohen76078c42011-06-09 15:06:52 -07001028 }
Adam Cohenac56cff2011-09-28 20:45:37 -07001029
Adam Cohen76fc0852011-06-17 13:26:23 -07001030 public void onTitleChanged(CharSequence title) {
1031 }
Adam Cohen76078c42011-06-09 15:06:52 -07001032
Adam Cohen7c693212011-05-18 15:26:57 -07001033 public ArrayList<View> getItemsInReadingOrder() {
Adam Cohen76078c42011-06-09 15:06:52 -07001034 return getItemsInReadingOrder(true);
1035 }
1036
1037 public ArrayList<View> getItemsInReadingOrder(boolean includeCurrentDragItem) {
Adam Cohen7c693212011-05-18 15:26:57 -07001038 if (mItemsInvalidated) {
1039 mItemsInReadingOrder.clear();
1040 for (int j = 0; j < mContent.getCountY(); j++) {
1041 for (int i = 0; i < mContent.getCountX(); i++) {
1042 View v = mContent.getChildAt(i, j);
1043 if (v != null) {
Adam Cohen76078c42011-06-09 15:06:52 -07001044 ShortcutInfo info = (ShortcutInfo) v.getTag();
1045 if (info != mCurrentDragInfo || includeCurrentDragItem) {
1046 mItemsInReadingOrder.add(v);
1047 }
Adam Cohen7c693212011-05-18 15:26:57 -07001048 }
1049 }
1050 }
1051 mItemsInvalidated = false;
1052 }
1053 return mItemsInReadingOrder;
1054 }
Adam Cohen8dfcba42011-07-07 16:38:18 -07001055
1056 public void getLocationInDragLayer(int[] loc) {
1057 mLauncher.getDragLayer().getLocationInDragLayer(this, loc);
1058 }
Adam Cohenea0818d2011-09-30 20:06:58 -07001059
1060 public void onFocusChange(View v, boolean hasFocus) {
1061 if (v == mFolderName && hasFocus) {
1062 startEditingFolderName();
1063 }
1064 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001065}