blob: 01939f839d125aa81a339520109a028d770bac22 [file] [log] [blame]
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Joe Onoratoa5902522009-07-30 13:37:37 -070017package com.android.launcher2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080018
Adam Cohendf2cc412011-04-27 16:56:57 -070019import android.animation.Animator;
20import android.animation.AnimatorListenerAdapter;
Adam Cohendf2cc412011-04-27 16:56:57 -070021import android.animation.ObjectAnimator;
22import android.animation.PropertyValuesHolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080023import android.content.Context;
Adam Cohen76fc0852011-06-17 13:26:23 -070024import android.content.res.Resources;
Winson Chung043f2af2012-03-01 16:09:54 -080025import android.graphics.PointF;
Romain Guyfb5411e2010-02-24 10:04:17 -080026import android.graphics.Rect;
Adam Cohenbadf71e2011-05-26 19:08:29 -070027import android.graphics.drawable.Drawable;
Adam Cohen7a14d0b2011-06-24 11:36:35 -070028import android.text.InputType;
Adam Cohene601a432011-07-26 21:51:30 -070029import android.text.Selection;
30import android.text.Spannable;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080031import android.util.AttributeSet;
Adam Cohen76fc0852011-06-17 13:26:23 -070032import android.view.ActionMode;
33import android.view.KeyEvent;
Adam Cohendf2cc412011-04-27 16:56:57 -070034import android.view.LayoutInflater;
Adam Cohen76fc0852011-06-17 13:26:23 -070035import android.view.Menu;
36import android.view.MenuItem;
Adam Cohen0c872ba2011-05-05 10:34:16 -070037import android.view.MotionEvent;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080038import android.view.View;
Adam Cohen3371da02011-10-25 21:38:29 -070039import android.view.accessibility.AccessibilityEvent;
40import android.view.accessibility.AccessibilityManager;
Adam Cohen76fc0852011-06-17 13:26:23 -070041import android.view.inputmethod.EditorInfo;
42import android.view.inputmethod.InputMethodManager;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080043import android.widget.LinearLayout;
Adam Cohendf2cc412011-04-27 16:56:57 -070044import android.widget.TextView;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080045
Romain Guyedcce092010-03-04 13:03:17 -080046import com.android.launcher.R;
Adam Cohena9cf38f2011-05-02 15:36:58 -070047import com.android.launcher2.FolderInfo.FolderListener;
Romain Guyedcce092010-03-04 13:03:17 -080048
Adam Cohenc0dcf592011-06-01 15:30:43 -070049import java.util.ArrayList;
50
The Android Open Source Project31dd5032009-03-03 19:32:27 -080051/**
52 * Represents a set of icons chosen by the user or generated by the system.
53 */
Adam Cohen8dfcba42011-07-07 16:38:18 -070054public class Folder extends LinearLayout implements DragSource, View.OnClickListener,
Adam Cohenea0818d2011-09-30 20:06:58 -070055 View.OnLongClickListener, DropTarget, FolderListener, TextView.OnEditorActionListener,
56 View.OnFocusChangeListener {
The Android Open Source Project31dd5032009-03-03 19:32:27 -080057
Michael Jurka3a9fced2012-04-13 14:44:29 -070058 @SuppressWarnings("unused")
Adam Cohendf2cc412011-04-27 16:56:57 -070059 private static final String TAG = "Launcher.Folder";
60
Adam Cohen4eac29a2011-07-11 17:53:37 -070061 protected DragController mDragController;
62 protected Launcher mLauncher;
63 protected FolderInfo mInfo;
64
Adam Cohendf2cc412011-04-27 16:56:57 -070065 static final int STATE_NONE = -1;
66 static final int STATE_SMALL = 0;
67 static final int STATE_ANIMATING = 1;
68 static final int STATE_OPEN = 2;
69
70 private int mExpandDuration;
71 protected CellLayout mContent;
72 private final LayoutInflater mInflater;
73 private final IconCache mIconCache;
74 private int mState = STATE_NONE;
Adam Cohenbfbfd262011-06-13 16:55:12 -070075 private static final int REORDER_ANIMATION_DURATION = 230;
76 private static final int ON_EXIT_CLOSE_DELAY = 800;
Adam Cohen2801caf2011-05-13 20:57:39 -070077 private boolean mRearrangeOnClose = false;
78 private FolderIcon mFolderIcon;
79 private int mMaxCountX;
80 private int mMaxCountY;
Adam Cohen78dc83e2011-11-15 17:10:00 -080081 private int mMaxNumItems;
Adam Cohen7c693212011-05-18 15:26:57 -070082 private ArrayList<View> mItemsInReadingOrder = new ArrayList<View>();
Adam Cohenbadf71e2011-05-26 19:08:29 -070083 private Drawable mIconDrawable;
Adam Cohen7c693212011-05-18 15:26:57 -070084 boolean mItemsInvalidated = false;
Adam Cohenbfbfd262011-06-13 16:55:12 -070085 private ShortcutInfo mCurrentDragInfo;
86 private View mCurrentDragView;
87 boolean mSuppressOnAdd = false;
88 private int[] mTargetCell = new int[2];
89 private int[] mPreviousTargetCell = new int[2];
90 private int[] mEmptyCell = new int[2];
91 private Alarm mReorderAlarm = new Alarm();
92 private Alarm mOnExitAlarm = new Alarm();
Adam Cohen76fc0852011-06-17 13:26:23 -070093 private int mFolderNameHeight;
Adam Cohen8e776a62011-06-28 18:10:06 -070094 private Rect mTempRect = new Rect();
Adam Cohen67bd9cc2011-07-29 14:07:04 -070095 private boolean mDragInProgress = false;
96 private boolean mDeleteFolderOnDropCompleted = false;
97 private boolean mSuppressFolderDeletion = false;
Adam Cohen05e0f402011-08-01 12:12:49 -070098 private boolean mItemAddedBackToSelfViaIcon = false;
Adam Cohenac56cff2011-09-28 20:45:37 -070099 FolderEditText mFolderName;
Adam Cohen228da5a2011-07-27 22:23:47 -0700100
Adam Cohen76fc0852011-06-17 13:26:23 -0700101 private boolean mIsEditingName = false;
102 private InputMethodManager mInputMethodManager;
Adam Cohendf2cc412011-04-27 16:56:57 -0700103
Adam Cohena65beee2011-06-27 21:32:23 -0700104 private static String sDefaultFolderName;
105 private static String sHintText;
106
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800107 /**
108 * Used to inflate the Workspace from XML.
109 *
110 * @param context The application's context.
111 * @param attrs The attribtues set containing the Workspace's customization values.
112 */
113 public Folder(Context context, AttributeSet attrs) {
114 super(context, attrs);
115 setAlwaysDrawnWithCacheEnabled(false);
Adam Cohendf2cc412011-04-27 16:56:57 -0700116 mInflater = LayoutInflater.from(context);
117 mIconCache = ((LauncherApplication)context.getApplicationContext()).getIconCache();
Adam Cohen78dc83e2011-11-15 17:10:00 -0800118
119 Resources res = getResources();
120 mMaxCountX = res.getInteger(R.integer.folder_max_count_x);
121 mMaxCountY = res.getInteger(R.integer.folder_max_count_y);
122 mMaxNumItems = res.getInteger(R.integer.folder_max_num_items);
123 if (mMaxCountX < 0 || mMaxCountY < 0 || mMaxNumItems < 0) {
124 mMaxCountX = LauncherModel.getCellCountX();
125 mMaxCountY = LauncherModel.getCellCountY();
126 mMaxNumItems = mMaxCountX * mMaxCountY;
127 }
Adam Cohen76fc0852011-06-17 13:26:23 -0700128
129 mInputMethodManager = (InputMethodManager)
130 mContext.getSystemService(Context.INPUT_METHOD_SERVICE);
131
Adam Cohen76fc0852011-06-17 13:26:23 -0700132 mExpandDuration = res.getInteger(R.integer.config_folderAnimDuration);
Adam Cohen4ef610f2011-06-21 22:37:36 -0700133
134 if (sDefaultFolderName == null) {
135 sDefaultFolderName = res.getString(R.string.folder_name);
136 }
Adam Cohena65beee2011-06-27 21:32:23 -0700137 if (sHintText == null) {
138 sHintText = res.getString(R.string.folder_hint_text);
139 }
Adam Cohen4eac29a2011-07-11 17:53:37 -0700140 mLauncher = (Launcher) context;
Adam Cohenac56cff2011-09-28 20:45:37 -0700141 // We need this view to be focusable in touch mode so that when text editing of the folder
142 // name is complete, we have something to focus on, thus hiding the cursor and giving
143 // reliable behvior when clicking the text field (since it will always gain focus on click).
144 setFocusableInTouchMode(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800145 }
146
147 @Override
148 protected void onFinishInflate() {
149 super.onFinishInflate();
Adam Cohendf2cc412011-04-27 16:56:57 -0700150 mContent = (CellLayout) findViewById(R.id.folder_content);
Adam Cohen2801caf2011-05-13 20:57:39 -0700151 mContent.setGridSize(0, 0);
Michael Jurkaa52570f2012-03-20 03:18:20 -0700152 mContent.getShortcutsAndWidgets().setMotionEventSplittingEnabled(false);
Adam Cohenac56cff2011-09-28 20:45:37 -0700153 mFolderName = (FolderEditText) findViewById(R.id.folder_name);
154 mFolderName.setFolder(this);
Adam Cohenea0818d2011-09-30 20:06:58 -0700155 mFolderName.setOnFocusChangeListener(this);
Adam Cohen76fc0852011-06-17 13:26:23 -0700156
157 // We find out how tall the text view wants to be (it is set to wrap_content), so that
158 // we can allocate the appropriate amount of space for it.
159 int measureSpec = MeasureSpec.UNSPECIFIED;
160 mFolderName.measure(measureSpec, measureSpec);
161 mFolderNameHeight = mFolderName.getMeasuredHeight();
162
163 // We disable action mode for now since it messes up the view on phones
164 mFolderName.setCustomSelectionActionModeCallback(mActionModeCallback);
Adam Cohen76fc0852011-06-17 13:26:23 -0700165 mFolderName.setOnEditorActionListener(this);
Adam Cohen4ef610f2011-06-21 22:37:36 -0700166 mFolderName.setSelectAllOnFocus(true);
Adam Cohen7a14d0b2011-06-24 11:36:35 -0700167 mFolderName.setInputType(mFolderName.getInputType() |
168 InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS | InputType.TYPE_TEXT_FLAG_CAP_WORDS);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800169 }
Adam Cohen2801caf2011-05-13 20:57:39 -0700170
Adam Cohen76fc0852011-06-17 13:26:23 -0700171 private ActionMode.Callback mActionModeCallback = new ActionMode.Callback() {
172 public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
173 return false;
174 }
175
176 public boolean onCreateActionMode(ActionMode mode, Menu menu) {
177 return false;
178 }
179
180 public void onDestroyActionMode(ActionMode mode) {
181 }
182
183 public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
184 return false;
185 }
186 };
187
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800188 public void onClick(View v) {
Adam Cohendf2cc412011-04-27 16:56:57 -0700189 Object tag = v.getTag();
190 if (tag instanceof ShortcutInfo) {
191 // refactor this code from Folder
192 ShortcutInfo item = (ShortcutInfo) tag;
193 int[] pos = new int[2];
194 v.getLocationOnScreen(pos);
195 item.intent.setSourceBounds(new Rect(pos[0], pos[1],
196 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Winson Chungc7450e32012-04-17 17:34:08 -0700197
198 mLauncher.startActivitySafely(v, item.intent, item);
Adam Cohendf2cc412011-04-27 16:56:57 -0700199 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800200 }
201
202 public boolean onLongClick(View v) {
Adam Cohendf2cc412011-04-27 16:56:57 -0700203 Object tag = v.getTag();
204 if (tag instanceof ShortcutInfo) {
Adam Cohendf2cc412011-04-27 16:56:57 -0700205 ShortcutInfo item = (ShortcutInfo) tag;
206 if (!v.isInTouchMode()) {
207 return false;
208 }
209
Winson Chung7d7541e2011-09-16 20:14:36 -0700210 mLauncher.dismissFolderCling(null);
211
Adam Cohendf2cc412011-04-27 16:56:57 -0700212 mLauncher.getWorkspace().onDragStartedWithItem(v);
Adam Cohenac8c8762011-07-13 11:15:27 -0700213 mLauncher.getWorkspace().beginDragShared(v, this);
Adam Cohenbadf71e2011-05-26 19:08:29 -0700214 mIconDrawable = ((TextView) v).getCompoundDrawables()[1];
Adam Cohen76078c42011-06-09 15:06:52 -0700215
216 mCurrentDragInfo = item;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700217 mEmptyCell[0] = item.cellX;
218 mEmptyCell[1] = item.cellY;
219 mCurrentDragView = v;
Adam Cohenfc53cd22011-07-20 15:45:11 -0700220
221 mContent.removeView(mCurrentDragView);
222 mInfo.remove(mCurrentDragInfo);
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700223 mDragInProgress = true;
Adam Cohen05e0f402011-08-01 12:12:49 -0700224 mItemAddedBackToSelfViaIcon = false;
Adam Cohendf2cc412011-04-27 16:56:57 -0700225 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800226 return true;
227 }
228
Adam Cohen76fc0852011-06-17 13:26:23 -0700229 public boolean isEditingName() {
230 return mIsEditingName;
231 }
232
233 public void startEditingFolderName() {
Adam Cohena65beee2011-06-27 21:32:23 -0700234 mFolderName.setHint("");
Adam Cohen76fc0852011-06-17 13:26:23 -0700235 mIsEditingName = true;
236 }
237
238 public void dismissEditingName() {
239 mInputMethodManager.hideSoftInputFromWindow(getWindowToken(), 0);
240 doneEditingFolderName(true);
241 }
242
243 public void doneEditingFolderName(boolean commit) {
Adam Cohena65beee2011-06-27 21:32:23 -0700244 mFolderName.setHint(sHintText);
Adam Cohen1df26a32011-08-12 16:15:23 -0700245 // Convert to a string here to ensure that no other state associated with the text field
246 // gets saved.
Adam Cohen3371da02011-10-25 21:38:29 -0700247 String newTitle = mFolderName.getText().toString();
248 mInfo.setTitle(newTitle);
Adam Cohen76fc0852011-06-17 13:26:23 -0700249 LauncherModel.updateItemInDatabase(mLauncher, mInfo);
Adam Cohenac56cff2011-09-28 20:45:37 -0700250
Adam Cohen3371da02011-10-25 21:38:29 -0700251 if (commit) {
252 sendCustomAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED,
253 String.format(mContext.getString(R.string.folder_renamed), newTitle));
254 }
Adam Cohenac56cff2011-09-28 20:45:37 -0700255 // In order to clear the focus from the text field, we set the focus on ourself. This
256 // ensures that every time the field is clicked, focus is gained, giving reliable behavior.
257 requestFocus();
258
Adam Cohene601a432011-07-26 21:51:30 -0700259 Selection.setSelection((Spannable) mFolderName.getText(), 0, 0);
Adam Cohen76fc0852011-06-17 13:26:23 -0700260 mIsEditingName = false;
261 }
262
263 public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
264 if (actionId == EditorInfo.IME_ACTION_DONE) {
265 dismissEditingName();
266 return true;
267 }
268 return false;
269 }
270
271 public View getEditTextRegion() {
272 return mFolderName;
273 }
274
Adam Cohenbadf71e2011-05-26 19:08:29 -0700275 public Drawable getDragDrawable() {
276 return mIconDrawable;
277 }
278
Adam Cohen0c872ba2011-05-05 10:34:16 -0700279 /**
280 * We need to handle touch events to prevent them from falling through to the workspace below.
281 */
282 @Override
283 public boolean onTouchEvent(MotionEvent ev) {
284 return true;
285 }
286
Joe Onorato00acb122009-08-04 16:04:30 -0400287 public void setDragController(DragController dragController) {
288 mDragController = dragController;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800289 }
290
Adam Cohen2801caf2011-05-13 20:57:39 -0700291 void setFolderIcon(FolderIcon icon) {
292 mFolderIcon = icon;
293 }
294
Adam Cohen3371da02011-10-25 21:38:29 -0700295 @Override
296 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
297 // When the folder gets focus, we don't want to announce the list of items.
298 return true;
299 }
300
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800301 /**
302 * @return the FolderInfo object associated with this folder
303 */
304 FolderInfo getInfo() {
305 return mInfo;
306 }
307
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800308 void bind(FolderInfo info) {
309 mInfo = info;
Adam Cohendf2cc412011-04-27 16:56:57 -0700310 ArrayList<ShortcutInfo> children = info.contents;
Adam Cohenc508b2d2011-06-28 14:41:44 -0700311 ArrayList<ShortcutInfo> overflow = new ArrayList<ShortcutInfo>();
Adam Cohen7c693212011-05-18 15:26:57 -0700312 setupContentForNumItems(children.size());
Adam Cohen0057bbc2011-08-12 18:30:51 -0700313 int count = 0;
Adam Cohendf2cc412011-04-27 16:56:57 -0700314 for (int i = 0; i < children.size(); i++) {
315 ShortcutInfo child = (ShortcutInfo) children.get(i);
Adam Cohenc508b2d2011-06-28 14:41:44 -0700316 if (!createAndAddShortcut(child)) {
317 overflow.add(child);
Adam Cohen0057bbc2011-08-12 18:30:51 -0700318 } else {
319 count++;
Adam Cohenc508b2d2011-06-28 14:41:44 -0700320 }
Adam Cohendf2cc412011-04-27 16:56:57 -0700321 }
Adam Cohenc508b2d2011-06-28 14:41:44 -0700322
Adam Cohen0057bbc2011-08-12 18:30:51 -0700323 // We rearrange the items in case there are any empty gaps
324 setupContentForNumItems(count);
325
Adam Cohenc508b2d2011-06-28 14:41:44 -0700326 // If our folder has too many items we prune them from the list. This is an issue
327 // when upgrading from the old Folders implementation which could contain an unlimited
328 // number of items.
329 for (ShortcutInfo item: overflow) {
330 mInfo.remove(item);
331 LauncherModel.deleteItemFromDatabase(mLauncher, item);
332 }
333
Adam Cohen4dbe6d92011-05-18 17:14:15 -0700334 mItemsInvalidated = true;
Adam Cohenac56cff2011-09-28 20:45:37 -0700335 updateTextViewFocus();
Adam Cohena9cf38f2011-05-02 15:36:58 -0700336 mInfo.addListener(this);
Adam Cohen4ef610f2011-06-21 22:37:36 -0700337
Adam Cohenafb01ee2011-06-23 15:38:03 -0700338 if (!sDefaultFolderName.contentEquals(mInfo.title)) {
Adam Cohen4ef610f2011-06-21 22:37:36 -0700339 mFolderName.setText(mInfo.title);
340 } else {
341 mFolderName.setText("");
342 }
Adam Cohendf2cc412011-04-27 16:56:57 -0700343 }
344
345 /**
346 * Creates a new UserFolder, inflated from R.layout.user_folder.
347 *
348 * @param context The application's context.
349 *
350 * @return A new UserFolder.
351 */
352 static Folder fromXml(Context context) {
353 return (Folder) LayoutInflater.from(context).inflate(R.layout.user_folder, null);
354 }
355
356 /**
357 * This method is intended to make the UserFolder to be visually identical in size and position
358 * to its associated FolderIcon. This allows for a seamless transition into the expanded state.
359 */
360 private void positionAndSizeAsIcon() {
Adam Cohen3e8f8112011-07-02 18:03:00 -0700361 if (!(getParent() instanceof DragLayer)) return;
Adam Cohen662b5982011-12-13 17:45:21 -0800362 setScaleX(0.8f);
363 setScaleY(0.8f);
364 setAlpha(0f);
Adam Cohendf2cc412011-04-27 16:56:57 -0700365 mState = STATE_SMALL;
366 }
367
368 public void animateOpen() {
Adam Cohen3e8f8112011-07-02 18:03:00 -0700369 positionAndSizeAsIcon();
370
Adam Cohen8e776a62011-06-28 18:10:06 -0700371 if (!(getParent() instanceof DragLayer)) return;
Adam Cohen2801caf2011-05-13 20:57:39 -0700372 centerAboutIcon();
Adam Cohen662b5982011-12-13 17:45:21 -0800373 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1);
374 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.0f);
375 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.0f);
376 ObjectAnimator oa = ObjectAnimator.ofPropertyValuesHolder(this, alpha, scaleX, scaleY);
Adam Cohen6441de02011-12-14 14:25:32 -0800377
Adam Cohen2801caf2011-05-13 20:57:39 -0700378 oa.addListener(new AnimatorListenerAdapter() {
Adam Cohendf2cc412011-04-27 16:56:57 -0700379 @Override
380 public void onAnimationStart(Animator animation) {
Adam Cohen3371da02011-10-25 21:38:29 -0700381 sendCustomAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED,
382 String.format(mContext.getString(R.string.folder_opened),
383 mContent.getCountX(), mContent.getCountY()));
Adam Cohendf2cc412011-04-27 16:56:57 -0700384 mState = STATE_ANIMATING;
385 }
386 @Override
387 public void onAnimationEnd(Animator animation) {
Adam Cohen2801caf2011-05-13 20:57:39 -0700388 mState = STATE_OPEN;
Winson Chung7a74ac92011-09-20 17:43:51 -0700389
390 Cling cling = mLauncher.showFirstRunFoldersCling();
391 if (cling != null) {
392 cling.bringToFront();
393 }
Adam Cohenac56cff2011-09-28 20:45:37 -0700394 setFocusOnFirstChild();
Adam Cohendf2cc412011-04-27 16:56:57 -0700395 }
396 });
Adam Cohen2801caf2011-05-13 20:57:39 -0700397 oa.setDuration(mExpandDuration);
398 oa.start();
Adam Cohendf2cc412011-04-27 16:56:57 -0700399 }
400
Adam Cohen3371da02011-10-25 21:38:29 -0700401 private void sendCustomAccessibilityEvent(int type, String text) {
402 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
403 AccessibilityEvent event = AccessibilityEvent.obtain(type);
404 onInitializeAccessibilityEvent(event);
405 event.getText().add(text);
406 AccessibilityManager.getInstance(mContext).sendAccessibilityEvent(event);
407 }
408 }
409
Adam Cohenac56cff2011-09-28 20:45:37 -0700410 private void setFocusOnFirstChild() {
411 View firstChild = mContent.getChildAt(0, 0);
412 if (firstChild != null) {
413 firstChild.requestFocus();
414 }
415 }
416
Adam Cohendf2cc412011-04-27 16:56:57 -0700417 public void animateClosed() {
Adam Cohen8e776a62011-06-28 18:10:06 -0700418 if (!(getParent() instanceof DragLayer)) return;
Adam Cohen662b5982011-12-13 17:45:21 -0800419 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0);
420 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 0.9f);
421 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 0.9f);
422 ObjectAnimator oa = ObjectAnimator.ofPropertyValuesHolder(this, alpha, scaleX, scaleY);
Adam Cohendf2cc412011-04-27 16:56:57 -0700423
Adam Cohen2801caf2011-05-13 20:57:39 -0700424 oa.addListener(new AnimatorListenerAdapter() {
Adam Cohendf2cc412011-04-27 16:56:57 -0700425 @Override
426 public void onAnimationEnd(Animator animation) {
Adam Cohen2801caf2011-05-13 20:57:39 -0700427 onCloseComplete();
Adam Cohen2801caf2011-05-13 20:57:39 -0700428 mState = STATE_SMALL;
Adam Cohendf2cc412011-04-27 16:56:57 -0700429 }
430 @Override
431 public void onAnimationStart(Animator animation) {
Adam Cohen3371da02011-10-25 21:38:29 -0700432 sendCustomAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED,
433 mContext.getString(R.string.folder_closed));
Adam Cohendf2cc412011-04-27 16:56:57 -0700434 mState = STATE_ANIMATING;
435 }
436 });
Adam Cohen2801caf2011-05-13 20:57:39 -0700437 oa.setDuration(mExpandDuration);
438 oa.start();
Adam Cohendf2cc412011-04-27 16:56:57 -0700439 }
440
441 void notifyDataSetChanged() {
442 // recreate all the children if the data set changes under us. We may want to do this more
443 // intelligently (ie just removing the views that should no longer exist)
444 mContent.removeAllViewsInLayout();
445 bind(mInfo);
446 }
447
Adam Cohencb3382b2011-05-24 14:07:08 -0700448 public boolean acceptDrop(DragObject d) {
449 final ItemInfo item = (ItemInfo) d.dragInfo;
Adam Cohendf2cc412011-04-27 16:56:57 -0700450 final int itemType = item.itemType;
Adam Cohen2801caf2011-05-13 20:57:39 -0700451 return ((itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION ||
452 itemType == LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT) &&
453 !isFull());
Adam Cohendf2cc412011-04-27 16:56:57 -0700454 }
455
Adam Cohendf2cc412011-04-27 16:56:57 -0700456 protected boolean findAndSetEmptyCells(ShortcutInfo item) {
457 int[] emptyCell = new int[2];
458 if (mContent.findCellForSpan(emptyCell, item.spanX, item.spanY)) {
459 item.cellX = emptyCell[0];
460 item.cellY = emptyCell[1];
Adam Cohendf2cc412011-04-27 16:56:57 -0700461 return true;
462 } else {
463 return false;
464 }
465 }
466
Adam Cohenc508b2d2011-06-28 14:41:44 -0700467 protected boolean createAndAddShortcut(ShortcutInfo item) {
Adam Cohendf2cc412011-04-27 16:56:57 -0700468 final TextView textView =
Adam Cohene87b9242011-06-29 14:01:26 -0700469 (TextView) mInflater.inflate(R.layout.application, this, false);
Adam Cohendf2cc412011-04-27 16:56:57 -0700470 textView.setCompoundDrawablesWithIntrinsicBounds(null,
471 new FastBitmapDrawable(item.getIcon(mIconCache)), null, null);
472 textView.setText(item.title);
473 textView.setTag(item);
474
475 textView.setOnClickListener(this);
476 textView.setOnLongClickListener(this);
477
Adam Cohenc508b2d2011-06-28 14:41:44 -0700478 // We need to check here to verify that the given item's location isn't already occupied
479 // by another item. If it is, we need to find the next available slot and assign
480 // it that position. This is an issue when upgrading from the old Folders implementation
481 // which could contain an unlimited number of items.
Adam Cohen0057bbc2011-08-12 18:30:51 -0700482 if (mContent.getChildAt(item.cellX, item.cellY) != null || item.cellX < 0 || item.cellY < 0
483 || item.cellX >= mContent.getCountX() || item.cellY >= mContent.getCountY()) {
Adam Cohenc508b2d2011-06-28 14:41:44 -0700484 if (!findAndSetEmptyCells(item)) {
485 return false;
486 }
487 }
488
Adam Cohendf2cc412011-04-27 16:56:57 -0700489 CellLayout.LayoutParams lp =
490 new CellLayout.LayoutParams(item.cellX, item.cellY, item.spanX, item.spanY);
491 boolean insert = false;
Adam Cohenac56cff2011-09-28 20:45:37 -0700492 textView.setOnKeyListener(new FolderKeyEventListener());
Adam Cohendf2cc412011-04-27 16:56:57 -0700493 mContent.addViewToCellLayout(textView, insert ? 0 : -1, (int)item.id, lp, true);
Adam Cohenc508b2d2011-06-28 14:41:44 -0700494 return true;
Adam Cohendf2cc412011-04-27 16:56:57 -0700495 }
496
Adam Cohencb3382b2011-05-24 14:07:08 -0700497 public void onDragEnter(DragObject d) {
Adam Cohenbfbfd262011-06-13 16:55:12 -0700498 mPreviousTargetCell[0] = -1;
499 mPreviousTargetCell[1] = -1;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700500 mOnExitAlarm.cancelAlarm();
501 }
502
503 OnAlarmListener mReorderAlarmListener = new OnAlarmListener() {
504 public void onAlarm(Alarm alarm) {
505 realTimeReorder(mEmptyCell, mTargetCell);
506 }
507 };
508
509 boolean readingOrderGreaterThan(int[] v1, int[] v2) {
510 if (v1[1] > v2[1] || (v1[1] == v2[1] && v1[0] > v2[0])) {
511 return true;
512 } else {
513 return false;
514 }
515 }
516
517 private void realTimeReorder(int[] empty, int[] target) {
518 boolean wrap;
519 int startX;
520 int endX;
521 int startY;
Adam Cohen76fc0852011-06-17 13:26:23 -0700522 int delay = 0;
523 float delayAmount = 30;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700524 if (readingOrderGreaterThan(target, empty)) {
525 wrap = empty[0] >= mContent.getCountX() - 1;
526 startY = wrap ? empty[1] + 1 : empty[1];
527 for (int y = startY; y <= target[1]; y++) {
528 startX = y == empty[1] ? empty[0] + 1 : 0;
529 endX = y < target[1] ? mContent.getCountX() - 1 : target[0];
530 for (int x = startX; x <= endX; x++) {
531 View v = mContent.getChildAt(x,y);
532 if (mContent.animateChildToPosition(v, empty[0], empty[1],
Adam Cohen482ed822012-03-02 14:15:13 -0800533 REORDER_ANIMATION_DURATION, delay, true, true)) {
Adam Cohenbfbfd262011-06-13 16:55:12 -0700534 empty[0] = x;
535 empty[1] = y;
Adam Cohen76fc0852011-06-17 13:26:23 -0700536 delay += delayAmount;
537 delayAmount *= 0.9;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700538 }
539 }
540 }
541 } else {
542 wrap = empty[0] == 0;
543 startY = wrap ? empty[1] - 1 : empty[1];
544 for (int y = startY; y >= target[1]; y--) {
545 startX = y == empty[1] ? empty[0] - 1 : mContent.getCountX() - 1;
546 endX = y > target[1] ? 0 : target[0];
547 for (int x = startX; x >= endX; x--) {
548 View v = mContent.getChildAt(x,y);
549 if (mContent.animateChildToPosition(v, empty[0], empty[1],
Adam Cohen482ed822012-03-02 14:15:13 -0800550 REORDER_ANIMATION_DURATION, delay, true, true)) {
Adam Cohenbfbfd262011-06-13 16:55:12 -0700551 empty[0] = x;
552 empty[1] = y;
Adam Cohen76fc0852011-06-17 13:26:23 -0700553 delay += delayAmount;
554 delayAmount *= 0.9;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700555 }
556 }
557 }
558 }
Adam Cohendf2cc412011-04-27 16:56:57 -0700559 }
560
Adam Cohencb3382b2011-05-24 14:07:08 -0700561 public void onDragOver(DragObject d) {
Adam Cohenbfbfd262011-06-13 16:55:12 -0700562 float[] r = getDragViewVisualCenter(d.x, d.y, d.xOffset, d.yOffset, d.dragView, null);
563 mTargetCell = mContent.findNearestArea((int) r[0], (int) r[1], 1, 1, mTargetCell);
564
565 if (mTargetCell[0] != mPreviousTargetCell[0] || mTargetCell[1] != mPreviousTargetCell[1]) {
566 mReorderAlarm.cancelAlarm();
567 mReorderAlarm.setOnAlarmListener(mReorderAlarmListener);
568 mReorderAlarm.setAlarm(150);
569 mPreviousTargetCell[0] = mTargetCell[0];
570 mPreviousTargetCell[1] = mTargetCell[1];
571 }
Adam Cohendf2cc412011-04-27 16:56:57 -0700572 }
573
Adam Cohenbfbfd262011-06-13 16:55:12 -0700574 // This is used to compute the visual center of the dragView. The idea is that
575 // the visual center represents the user's interpretation of where the item is, and hence
576 // is the appropriate point to use when determining drop location.
577 private float[] getDragViewVisualCenter(int x, int y, int xOffset, int yOffset,
578 DragView dragView, float[] recycle) {
579 float res[];
580 if (recycle == null) {
581 res = new float[2];
582 } else {
583 res = recycle;
584 }
585
586 // These represent the visual top and left of drag view if a dragRect was provided.
587 // If a dragRect was not provided, then they correspond to the actual view left and
588 // top, as the dragRect is in that case taken to be the entire dragView.
589 // R.dimen.dragViewOffsetY.
590 int left = x - xOffset;
591 int top = y - yOffset;
592
593 // In order to find the visual center, we shift by half the dragRect
594 res[0] = left + dragView.getDragRegion().width() / 2;
595 res[1] = top + dragView.getDragRegion().height() / 2;
596
597 return res;
598 }
599
600 OnAlarmListener mOnExitAlarmListener = new OnAlarmListener() {
601 public void onAlarm(Alarm alarm) {
Adam Cohen3e8f8112011-07-02 18:03:00 -0700602 completeDragExit();
Adam Cohenbfbfd262011-06-13 16:55:12 -0700603 }
604 };
605
Adam Cohen95bb8002011-07-03 23:40:28 -0700606 public void completeDragExit() {
Adam Cohen3e8f8112011-07-02 18:03:00 -0700607 mLauncher.closeFolder();
608 mCurrentDragInfo = null;
609 mCurrentDragView = null;
610 mSuppressOnAdd = false;
611 mRearrangeOnClose = true;
612 }
613
Adam Cohencb3382b2011-05-24 14:07:08 -0700614 public void onDragExit(DragObject d) {
Adam Cohenbfbfd262011-06-13 16:55:12 -0700615 // We only close the folder if this is a true drag exit, ie. not because a drop
616 // has occurred above the folder.
617 if (!d.dragComplete) {
618 mOnExitAlarm.setOnAlarmListener(mOnExitAlarmListener);
619 mOnExitAlarm.setAlarm(ON_EXIT_CLOSE_DELAY);
620 }
621 mReorderAlarm.cancelAlarm();
Adam Cohen2801caf2011-05-13 20:57:39 -0700622 }
623
Winson Chunga48487a2012-03-20 16:19:37 -0700624 public void onDropCompleted(View target, DragObject d, boolean isFlingToDelete,
625 boolean success) {
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700626 if (success) {
Adam Cohen05e0f402011-08-01 12:12:49 -0700627 if (mDeleteFolderOnDropCompleted && !mItemAddedBackToSelfViaIcon) {
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700628 replaceFolderWithFinalItem();
629 }
630 } else {
631 // The drag failed, we need to return the item to the folder
632 mFolderIcon.onDrop(d);
633
634 // We're going to trigger a "closeFolder" which may occur before this item has
635 // been added back to the folder -- this could cause the folder to be deleted
636 if (mOnExitAlarm.alarmPending()) {
637 mSuppressFolderDeletion = true;
638 }
639 }
640
641 if (target != this) {
642 if (mOnExitAlarm.alarmPending()) {
643 mOnExitAlarm.cancelAlarm();
644 completeDragExit();
645 }
646 }
647 mDeleteFolderOnDropCompleted = false;
648 mDragInProgress = false;
Adam Cohen05e0f402011-08-01 12:12:49 -0700649 mItemAddedBackToSelfViaIcon = false;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700650 mCurrentDragInfo = null;
651 mCurrentDragView = null;
652 mSuppressOnAdd = false;
Adam Cohen4045eb72011-10-06 11:44:26 -0700653
654 // Reordering may have occured, and we need to save the new item locations. We do this once
655 // at the end to prevent unnecessary database operations.
656 updateItemLocationsInDatabase();
657 }
658
Winson Chunga48487a2012-03-20 16:19:37 -0700659 @Override
Winson Chung043f2af2012-03-01 16:09:54 -0800660 public boolean supportsFlingToDelete() {
661 return true;
662 }
663
Winson Chunga48487a2012-03-20 16:19:37 -0700664 public void onFlingToDelete(DragObject d, int x, int y, PointF vec) {
665 // Do nothing
666 }
667
668 @Override
669 public void onFlingToDeleteCompleted() {
670 // Do nothing
671 }
672
Adam Cohen4045eb72011-10-06 11:44:26 -0700673 private void updateItemLocationsInDatabase() {
674 ArrayList<View> list = getItemsInReadingOrder();
675 for (int i = 0; i < list.size(); i++) {
676 View v = list.get(i);
677 ItemInfo info = (ItemInfo) v.getTag();
678 LauncherModel.moveItemInDatabase(mLauncher, info, mInfo.id, 0,
679 info.cellX, info.cellY);
680 }
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700681 }
Adam Cohen228da5a2011-07-27 22:23:47 -0700682
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700683 public void notifyDrop() {
684 if (mDragInProgress) {
Adam Cohen05e0f402011-08-01 12:12:49 -0700685 mItemAddedBackToSelfViaIcon = true;
Adam Cohen76078c42011-06-09 15:06:52 -0700686 }
Adam Cohendf2cc412011-04-27 16:56:57 -0700687 }
688
689 public boolean isDropEnabled() {
690 return true;
691 }
692
Adam Cohencb3382b2011-05-24 14:07:08 -0700693 public DropTarget getDropTargetDelegate(DragObject d) {
Adam Cohendf2cc412011-04-27 16:56:57 -0700694 return null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800695 }
Adam Cohena9cf38f2011-05-02 15:36:58 -0700696
Adam Cohen4045eb72011-10-06 11:44:26 -0700697 private void setupContentDimensions(int count) {
Adam Cohen2801caf2011-05-13 20:57:39 -0700698 ArrayList<View> list = getItemsInReadingOrder();
699
700 int countX = mContent.getCountX();
701 int countY = mContent.getCountY();
Adam Cohen7c693212011-05-18 15:26:57 -0700702 boolean done = false;
Adam Cohen2801caf2011-05-13 20:57:39 -0700703
Adam Cohen7c693212011-05-18 15:26:57 -0700704 while (!done) {
705 int oldCountX = countX;
706 int oldCountY = countY;
707 if (countX * countY < count) {
708 // Current grid is too small, expand it
Adam Cohen78dc83e2011-11-15 17:10:00 -0800709 if ((countX <= countY || countY == mMaxCountY) && countX < mMaxCountX) {
Adam Cohen7c693212011-05-18 15:26:57 -0700710 countX++;
711 } else if (countY < mMaxCountY) {
712 countY++;
713 }
714 if (countY == 0) countY++;
715 } else if ((countY - 1) * countX >= count && countY >= countX) {
716 countY = Math.max(0, countY - 1);
717 } else if ((countX - 1) * countY >= count) {
718 countX = Math.max(0, countX - 1);
Adam Cohen2801caf2011-05-13 20:57:39 -0700719 }
Adam Cohen7c693212011-05-18 15:26:57 -0700720 done = countX == oldCountX && countY == oldCountY;
Adam Cohen2801caf2011-05-13 20:57:39 -0700721 }
Adam Cohen7c693212011-05-18 15:26:57 -0700722 mContent.setGridSize(countX, countY);
Adam Cohen2801caf2011-05-13 20:57:39 -0700723 arrangeChildren(list);
724 }
725
726 public boolean isFull() {
Adam Cohen78dc83e2011-11-15 17:10:00 -0800727 return getItemCount() >= mMaxNumItems;
Adam Cohen2801caf2011-05-13 20:57:39 -0700728 }
729
730 private void centerAboutIcon() {
Adam Cohen8e776a62011-06-28 18:10:06 -0700731 DragLayer.LayoutParams lp = (DragLayer.LayoutParams) getLayoutParams();
Adam Cohen2801caf2011-05-13 20:57:39 -0700732
733 int width = getPaddingLeft() + getPaddingRight() + mContent.getDesiredWidth();
Adam Cohenf4bb1cd2011-07-22 14:36:03 -0700734 int height = getPaddingTop() + getPaddingBottom() + mContent.getDesiredHeight()
735 + mFolderNameHeight;
Adam Cohen8e776a62011-06-28 18:10:06 -0700736 DragLayer parent = (DragLayer) mLauncher.findViewById(R.id.drag_layer);
Adam Cohen2801caf2011-05-13 20:57:39 -0700737
Adam Cohen8e776a62011-06-28 18:10:06 -0700738 parent.getDescendantRectRelativeToSelf(mFolderIcon, mTempRect);
739
740 int centerX = mTempRect.centerX();
741 int centerY = mTempRect.centerY();
Adam Cohen2801caf2011-05-13 20:57:39 -0700742 int centeredLeft = centerX - width / 2;
743 int centeredTop = centerY - height / 2;
744
Adam Cohen7cc1bc42011-12-13 21:28:43 -0800745 int currentPage = mLauncher.getWorkspace().getCurrentPage();
746 // In case the workspace is scrolling, we need to use the final scroll to compute
747 // the folders bounds.
748 mLauncher.getWorkspace().setFinalScrollForPageChange(currentPage);
Adam Cohen35e7e642011-07-17 14:47:18 -0700749 // We first fetch the currently visible CellLayoutChildren
Adam Cohen7cc1bc42011-12-13 21:28:43 -0800750 CellLayout currentLayout = (CellLayout) mLauncher.getWorkspace().getChildAt(currentPage);
Michael Jurkaa52570f2012-03-20 03:18:20 -0700751 ShortcutAndWidgetContainer boundingLayout = currentLayout.getShortcutsAndWidgets();
Adam Cohen35e7e642011-07-17 14:47:18 -0700752 Rect bounds = new Rect();
753 parent.getDescendantRectRelativeToSelf(boundingLayout, bounds);
Adam Cohen7cc1bc42011-12-13 21:28:43 -0800754 // We reset the workspaces scroll
755 mLauncher.getWorkspace().resetFinalScrollForPageChange(currentPage);
Adam Cohen2801caf2011-05-13 20:57:39 -0700756
Adam Cohen35e7e642011-07-17 14:47:18 -0700757 // We need to bound the folder to the currently visible CellLayoutChildren
758 int left = Math.min(Math.max(bounds.left, centeredLeft),
759 bounds.left + bounds.width() - width);
760 int top = Math.min(Math.max(bounds.top, centeredTop),
761 bounds.top + bounds.height() - height);
762 // If the folder doesn't fit within the bounds, center it about the desired bounds
763 if (width >= bounds.width()) {
764 left = bounds.left + (bounds.width() - width) / 2;
Adam Cohen0e4857c2011-06-30 17:05:14 -0700765 }
Adam Cohen35e7e642011-07-17 14:47:18 -0700766 if (height >= bounds.height()) {
767 top = bounds.top + (bounds.height() - height) / 2;
Adam Cohen0e4857c2011-06-30 17:05:14 -0700768 }
Adam Cohen2801caf2011-05-13 20:57:39 -0700769
770 int folderPivotX = width / 2 + (centeredLeft - left);
771 int folderPivotY = height / 2 + (centeredTop - top);
772 setPivotX(folderPivotX);
773 setPivotY(folderPivotY);
774 int folderIconPivotX = (int) (mFolderIcon.getMeasuredWidth() *
775 (1.0f * folderPivotX / width));
776 int folderIconPivotY = (int) (mFolderIcon.getMeasuredHeight() *
777 (1.0f * folderPivotY / height));
778 mFolderIcon.setPivotX(folderIconPivotX);
779 mFolderIcon.setPivotY(folderIconPivotY);
780
Adam Cohen662b5982011-12-13 17:45:21 -0800781 lp.width = width;
782 lp.height = height;
783 lp.x = left;
784 lp.y = top;
Adam Cohen2801caf2011-05-13 20:57:39 -0700785 }
786
787 private void setupContentForNumItems(int count) {
Adam Cohen4045eb72011-10-06 11:44:26 -0700788 setupContentDimensions(count);
Adam Cohen2801caf2011-05-13 20:57:39 -0700789
Adam Cohen8e776a62011-06-28 18:10:06 -0700790 DragLayer.LayoutParams lp = (DragLayer.LayoutParams) getLayoutParams();
Adam Cohen2801caf2011-05-13 20:57:39 -0700791 if (lp == null) {
Adam Cohen8e776a62011-06-28 18:10:06 -0700792 lp = new DragLayer.LayoutParams(0, 0);
793 lp.customPosition = true;
Adam Cohen2801caf2011-05-13 20:57:39 -0700794 setLayoutParams(lp);
795 }
796 centerAboutIcon();
797 }
798
Adam Cohen234c4cd2011-07-17 21:03:04 -0700799 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
800 int width = getPaddingLeft() + getPaddingRight() + mContent.getDesiredWidth();
Adam Cohenf4bb1cd2011-07-22 14:36:03 -0700801 int height = getPaddingTop() + getPaddingBottom() + mContent.getDesiredHeight()
802 + mFolderNameHeight;
Adam Cohen234c4cd2011-07-17 21:03:04 -0700803
804 int contentWidthSpec = MeasureSpec.makeMeasureSpec(mContent.getDesiredWidth(),
805 MeasureSpec.EXACTLY);
806 int contentHeightSpec = MeasureSpec.makeMeasureSpec(mContent.getDesiredHeight(),
807 MeasureSpec.EXACTLY);
808 mContent.measure(contentWidthSpec, contentHeightSpec);
809
810 mFolderName.measure(contentWidthSpec,
811 MeasureSpec.makeMeasureSpec(mFolderNameHeight, MeasureSpec.EXACTLY));
812 setMeasuredDimension(width, height);
813 }
814
Adam Cohen2801caf2011-05-13 20:57:39 -0700815 private void arrangeChildren(ArrayList<View> list) {
816 int[] vacant = new int[2];
817 if (list == null) {
818 list = getItemsInReadingOrder();
819 }
820 mContent.removeAllViews();
821
822 for (int i = 0; i < list.size(); i++) {
823 View v = list.get(i);
824 mContent.getVacantCell(vacant, 1, 1);
825 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) v.getLayoutParams();
826 lp.cellX = vacant[0];
827 lp.cellY = vacant[1];
828 ItemInfo info = (ItemInfo) v.getTag();
Adam Cohen2792a332011-09-26 21:09:47 -0700829 if (info.cellX != vacant[0] || info.cellY != vacant[1]) {
830 info.cellX = vacant[0];
831 info.cellY = vacant[1];
832 LauncherModel.addOrMoveItemInDatabase(mLauncher, info, mInfo.id, 0,
833 info.cellX, info.cellY);
834 }
Adam Cohen2801caf2011-05-13 20:57:39 -0700835 boolean insert = false;
836 mContent.addViewToCellLayout(v, insert ? 0 : -1, (int)info.id, lp, true);
837 }
Adam Cohen7c693212011-05-18 15:26:57 -0700838 mItemsInvalidated = true;
Adam Cohen2801caf2011-05-13 20:57:39 -0700839 }
840
Adam Cohena9cf38f2011-05-02 15:36:58 -0700841 public int getItemCount() {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700842 return mContent.getShortcutsAndWidgets().getChildCount();
Adam Cohena9cf38f2011-05-02 15:36:58 -0700843 }
844
845 public View getItemAt(int index) {
Michael Jurkaa52570f2012-03-20 03:18:20 -0700846 return mContent.getShortcutsAndWidgets().getChildAt(index);
Adam Cohena9cf38f2011-05-02 15:36:58 -0700847 }
848
Adam Cohen2801caf2011-05-13 20:57:39 -0700849 private void onCloseComplete() {
Adam Cohen05e0f402011-08-01 12:12:49 -0700850 DragLayer parent = (DragLayer) getParent();
Adam Cohen4554ee12011-08-03 16:13:21 -0700851 parent.removeView(this);
852 mDragController.removeDropTarget((DropTarget) this);
Adam Cohen05e0f402011-08-01 12:12:49 -0700853 clearFocus();
Adam Cohenac56cff2011-09-28 20:45:37 -0700854 mFolderIcon.requestFocus();
Adam Cohen05e0f402011-08-01 12:12:49 -0700855
Adam Cohen2801caf2011-05-13 20:57:39 -0700856 if (mRearrangeOnClose) {
857 setupContentForNumItems(getItemCount());
858 mRearrangeOnClose = false;
859 }
Adam Cohenafb01ee2011-06-23 15:38:03 -0700860 if (getItemCount() <= 1) {
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700861 if (!mDragInProgress && !mSuppressFolderDeletion) {
862 replaceFolderWithFinalItem();
863 } else if (mDragInProgress) {
864 mDeleteFolderOnDropCompleted = true;
865 }
Adam Cohenafb01ee2011-06-23 15:38:03 -0700866 }
Adam Cohen67bd9cc2011-07-29 14:07:04 -0700867 mSuppressFolderDeletion = false;
Adam Cohenafb01ee2011-06-23 15:38:03 -0700868 }
869
870 private void replaceFolderWithFinalItem() {
871 ItemInfo finalItem = null;
872
873 if (getItemCount() == 1) {
874 finalItem = mInfo.contents.get(0);
875 }
876
877 // Remove the folder completely
Winson Chung3d503fb2011-07-13 17:25:49 -0700878 CellLayout cellLayout = mLauncher.getCellLayout(mInfo.container, mInfo.screen);
Adam Cohenafb01ee2011-06-23 15:38:03 -0700879 cellLayout.removeView(mFolderIcon);
880 if (mFolderIcon instanceof DropTarget) {
881 mDragController.removeDropTarget((DropTarget) mFolderIcon);
882 }
883 mLauncher.removeFolder(mInfo);
884
885 if (finalItem != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -0700886 LauncherModel.addOrMoveItemInDatabase(mLauncher, finalItem, mInfo.container,
887 mInfo.screen, mInfo.cellX, mInfo.cellY);
Adam Cohenafb01ee2011-06-23 15:38:03 -0700888 }
Adam Cohen716b51e2011-06-30 12:09:54 -0700889 LauncherModel.deleteItemFromDatabase(mLauncher, mInfo);
Adam Cohenafb01ee2011-06-23 15:38:03 -0700890
891 // Add the last remaining child to the workspace in place of the folder
892 if (finalItem != null) {
893 View child = mLauncher.createShortcut(R.layout.application, cellLayout,
894 (ShortcutInfo) finalItem);
895
Winson Chung3d503fb2011-07-13 17:25:49 -0700896 mLauncher.getWorkspace().addInScreen(child, mInfo.container, mInfo.screen, mInfo.cellX,
897 mInfo.cellY, mInfo.spanX, mInfo.spanY);
Adam Cohenafb01ee2011-06-23 15:38:03 -0700898 }
Adam Cohen2801caf2011-05-13 20:57:39 -0700899 }
900
Adam Cohenac56cff2011-09-28 20:45:37 -0700901 // This method keeps track of the last item in the folder for the purposes
902 // of keyboard focus
903 private void updateTextViewFocus() {
904 View lastChild = getItemAt(getItemCount() - 1);
905 getItemAt(getItemCount() - 1);
906 if (lastChild != null) {
907 mFolderName.setNextFocusDownId(lastChild.getId());
908 mFolderName.setNextFocusRightId(lastChild.getId());
909 mFolderName.setNextFocusLeftId(lastChild.getId());
910 mFolderName.setNextFocusUpId(lastChild.getId());
911 }
912 }
913
Adam Cohenbfbfd262011-06-13 16:55:12 -0700914 public void onDrop(DragObject d) {
915 ShortcutInfo item;
916 if (d.dragInfo instanceof ApplicationInfo) {
917 // Came from all apps -- make a copy
918 item = ((ApplicationInfo) d.dragInfo).makeShortcut();
919 item.spanX = 1;
920 item.spanY = 1;
921 } else {
922 item = (ShortcutInfo) d.dragInfo;
923 }
Adam Cohen05e0f402011-08-01 12:12:49 -0700924 // Dragged from self onto self, currently this is the only path possible, however
925 // we keep this as a distinct code path.
Adam Cohenbfbfd262011-06-13 16:55:12 -0700926 if (item == mCurrentDragInfo) {
927 ShortcutInfo si = (ShortcutInfo) mCurrentDragView.getTag();
928 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) mCurrentDragView.getLayoutParams();
929 si.cellX = lp.cellX = mEmptyCell[0];
930 si.cellX = lp.cellY = mEmptyCell[1];
931 mContent.addViewToCellLayout(mCurrentDragView, -1, (int)item.id, lp, true);
Adam Cohenfc53cd22011-07-20 15:45:11 -0700932 if (d.dragView.hasDrawn()) {
933 mLauncher.getDragLayer().animateViewIntoPosition(d.dragView, mCurrentDragView);
934 } else {
935 mCurrentDragView.setVisibility(VISIBLE);
936 }
Adam Cohene9166b22011-07-08 17:11:11 -0700937 mItemsInvalidated = true;
Adam Cohen4045eb72011-10-06 11:44:26 -0700938 setupContentDimensions(getItemCount());
Adam Cohen716b51e2011-06-30 12:09:54 -0700939 mSuppressOnAdd = true;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700940 }
941 mInfo.add(item);
942 }
943
944 public void onAdd(ShortcutInfo item) {
945 mItemsInvalidated = true;
Adam Cohen05e0f402011-08-01 12:12:49 -0700946 // If the item was dropped onto this open folder, we have done the work associated
947 // with adding the item to the folder, as indicated by mSuppressOnAdd being set
Adam Cohenbfbfd262011-06-13 16:55:12 -0700948 if (mSuppressOnAdd) return;
949 if (!findAndSetEmptyCells(item)) {
950 // The current layout is full, can we expand it?
951 setupContentForNumItems(getItemCount() + 1);
952 findAndSetEmptyCells(item);
953 }
954 createAndAddShortcut(item);
955 LauncherModel.addOrMoveItemInDatabase(
956 mLauncher, item, mInfo.id, 0, item.cellX, item.cellY);
957 }
958
Adam Cohena9cf38f2011-05-02 15:36:58 -0700959 public void onRemove(ShortcutInfo item) {
Adam Cohen7c693212011-05-18 15:26:57 -0700960 mItemsInvalidated = true;
Adam Cohen05e0f402011-08-01 12:12:49 -0700961 // If this item is being dragged from this open folder, we have already handled
962 // the work associated with removing the item, so we don't have to do anything here.
Adam Cohenbfbfd262011-06-13 16:55:12 -0700963 if (item == mCurrentDragInfo) return;
Adam Cohendf1e4e82011-06-24 15:57:39 -0700964 View v = getViewForInfo(item);
Adam Cohena9cf38f2011-05-02 15:36:58 -0700965 mContent.removeView(v);
Adam Cohen2801caf2011-05-13 20:57:39 -0700966 if (mState == STATE_ANIMATING) {
967 mRearrangeOnClose = true;
968 } else {
969 setupContentForNumItems(getItemCount());
970 }
Adam Cohenafb01ee2011-06-23 15:38:03 -0700971 if (getItemCount() <= 1) {
972 replaceFolderWithFinalItem();
973 }
Adam Cohena9cf38f2011-05-02 15:36:58 -0700974 }
Adam Cohen7c693212011-05-18 15:26:57 -0700975
Adam Cohendf1e4e82011-06-24 15:57:39 -0700976 private View getViewForInfo(ShortcutInfo item) {
977 for (int j = 0; j < mContent.getCountY(); j++) {
978 for (int i = 0; i < mContent.getCountX(); i++) {
979 View v = mContent.getChildAt(i, j);
980 if (v.getTag() == item) {
981 return v;
982 }
983 }
984 }
985 return null;
986 }
987
Adam Cohen76078c42011-06-09 15:06:52 -0700988 public void onItemsChanged() {
Adam Cohenac56cff2011-09-28 20:45:37 -0700989 updateTextViewFocus();
Adam Cohen76078c42011-06-09 15:06:52 -0700990 }
Adam Cohenac56cff2011-09-28 20:45:37 -0700991
Adam Cohen76fc0852011-06-17 13:26:23 -0700992 public void onTitleChanged(CharSequence title) {
993 }
Adam Cohen76078c42011-06-09 15:06:52 -0700994
Adam Cohen7c693212011-05-18 15:26:57 -0700995 public ArrayList<View> getItemsInReadingOrder() {
Adam Cohen76078c42011-06-09 15:06:52 -0700996 return getItemsInReadingOrder(true);
997 }
998
999 public ArrayList<View> getItemsInReadingOrder(boolean includeCurrentDragItem) {
Adam Cohen7c693212011-05-18 15:26:57 -07001000 if (mItemsInvalidated) {
1001 mItemsInReadingOrder.clear();
1002 for (int j = 0; j < mContent.getCountY(); j++) {
1003 for (int i = 0; i < mContent.getCountX(); i++) {
1004 View v = mContent.getChildAt(i, j);
1005 if (v != null) {
Adam Cohen76078c42011-06-09 15:06:52 -07001006 ShortcutInfo info = (ShortcutInfo) v.getTag();
1007 if (info != mCurrentDragInfo || includeCurrentDragItem) {
1008 mItemsInReadingOrder.add(v);
1009 }
Adam Cohen7c693212011-05-18 15:26:57 -07001010 }
1011 }
1012 }
1013 mItemsInvalidated = false;
1014 }
1015 return mItemsInReadingOrder;
1016 }
Adam Cohen8dfcba42011-07-07 16:38:18 -07001017
1018 public void getLocationInDragLayer(int[] loc) {
1019 mLauncher.getDragLayer().getLocationInDragLayer(this, loc);
1020 }
Adam Cohenea0818d2011-09-30 20:06:58 -07001021
1022 public void onFocusChange(View v, boolean hasFocus) {
1023 if (v == mFolderName && hasFocus) {
1024 startEditingFolderName();
1025 }
1026 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001027}