blob: 60c7ed49949686a3bc40b8c213164299f2c3752a [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;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080030import android.util.AttributeSet;
Adam Cohen76fc0852011-06-17 13:26:23 -070031import android.view.ActionMode;
32import android.view.KeyEvent;
Adam Cohendf2cc412011-04-27 16:56:57 -070033import android.view.LayoutInflater;
Adam Cohen76fc0852011-06-17 13:26:23 -070034import android.view.Menu;
35import android.view.MenuItem;
Adam Cohen0c872ba2011-05-05 10:34:16 -070036import android.view.MotionEvent;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080037import android.view.View;
38import android.view.View.OnClickListener;
Adam Cohen2801caf2011-05-13 20:57:39 -070039import android.view.animation.AccelerateInterpolator;
40import android.view.animation.DecelerateInterpolator;
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.AdapterView;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080044import android.widget.LinearLayout;
Adam Cohendf2cc412011-04-27 16:56:57 -070045import android.widget.TextView;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080046import android.widget.AdapterView.OnItemClickListener;
47import android.widget.AdapterView.OnItemLongClickListener;
48
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 */
57public class Folder extends LinearLayout implements DragSource, OnItemLongClickListener,
Adam Cohen76fc0852011-06-17 13:26:23 -070058 OnItemClickListener, OnClickListener, View.OnLongClickListener, DropTarget, FolderListener,
59 TextView.OnEditorActionListener {
The Android Open Source Project31dd5032009-03-03 19:32:27 -080060
Joe Onorato00acb122009-08-04 16:04:30 -040061 protected DragController mDragController;
Adam Cohendf2cc412011-04-27 16:56:57 -070062
The Android Open Source Project31dd5032009-03-03 19:32:27 -080063 protected Launcher mLauncher;
64
The Android Open Source Project31dd5032009-03-03 19:32:27 -080065 protected FolderInfo mInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080066
Adam Cohendf2cc412011-04-27 16:56:57 -070067 private static final String TAG = "Launcher.Folder";
68
69 static final int STATE_NONE = -1;
70 static final int STATE_SMALL = 0;
71 static final int STATE_ANIMATING = 1;
72 static final int STATE_OPEN = 2;
73
74 private int mExpandDuration;
75 protected CellLayout mContent;
76 private final LayoutInflater mInflater;
77 private final IconCache mIconCache;
78 private int mState = STATE_NONE;
Adam Cohena9cf38f2011-05-02 15:36:58 -070079 private int[] mDragItemPosition = new int[2];
Adam Cohen2801caf2011-05-13 20:57:39 -070080 private static final int FULL_GROW = 0;
81 private static final int PARTIAL_GROW = 1;
Adam Cohenbfbfd262011-06-13 16:55:12 -070082 private static final int REORDER_ANIMATION_DURATION = 230;
83 private static final int ON_EXIT_CLOSE_DELAY = 800;
Adam Cohen2801caf2011-05-13 20:57:39 -070084 private int mMode = PARTIAL_GROW;
85 private boolean mRearrangeOnClose = false;
86 private FolderIcon mFolderIcon;
87 private int mMaxCountX;
88 private int mMaxCountY;
89 private Rect mNewSize = new Rect();
Adam Cohen7c693212011-05-18 15:26:57 -070090 private ArrayList<View> mItemsInReadingOrder = new ArrayList<View>();
Adam Cohenbadf71e2011-05-26 19:08:29 -070091 private Drawable mIconDrawable;
Adam Cohen7c693212011-05-18 15:26:57 -070092 boolean mItemsInvalidated = false;
Adam Cohenbfbfd262011-06-13 16:55:12 -070093 private ShortcutInfo mCurrentDragInfo;
94 private View mCurrentDragView;
95 boolean mSuppressOnAdd = false;
96 private int[] mTargetCell = new int[2];
97 private int[] mPreviousTargetCell = new int[2];
98 private int[] mEmptyCell = new int[2];
99 private Alarm mReorderAlarm = new Alarm();
100 private Alarm mOnExitAlarm = new Alarm();
Adam Cohen76fc0852011-06-17 13:26:23 -0700101 private TextView mFolderName;
102 private int mFolderNameHeight;
Adam Cohen4ef610f2011-06-21 22:37:36 -0700103 private static String sDefaultFolderName;
104 private Rect mHitRect = new Rect();
Adam Cohen76fc0852011-06-17 13:26:23 -0700105
106 private boolean mIsEditingName = false;
107 private InputMethodManager mInputMethodManager;
Adam Cohendf2cc412011-04-27 16:56:57 -0700108
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800109 /**
110 * Used to inflate the Workspace from XML.
111 *
112 * @param context The application's context.
113 * @param attrs The attribtues set containing the Workspace's customization values.
114 */
115 public Folder(Context context, AttributeSet attrs) {
116 super(context, attrs);
117 setAlwaysDrawnWithCacheEnabled(false);
Adam Cohendf2cc412011-04-27 16:56:57 -0700118 mInflater = LayoutInflater.from(context);
119 mIconCache = ((LauncherApplication)context.getApplicationContext()).getIconCache();
Adam Cohen2801caf2011-05-13 20:57:39 -0700120 mMaxCountX = LauncherModel.getCellCountX() - 1;
121 mMaxCountY = LauncherModel.getCellCountY() - 1;
Adam Cohen76fc0852011-06-17 13:26:23 -0700122
123 mInputMethodManager = (InputMethodManager)
124 mContext.getSystemService(Context.INPUT_METHOD_SERVICE);
125
126 Resources res = getResources();
127 mExpandDuration = res.getInteger(R.integer.config_folderAnimDuration);
Adam Cohen4ef610f2011-06-21 22:37:36 -0700128
129 if (sDefaultFolderName == null) {
130 sDefaultFolderName = res.getString(R.string.folder_name);
131 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800132 }
133
134 @Override
135 protected void onFinishInflate() {
136 super.onFinishInflate();
Adam Cohendf2cc412011-04-27 16:56:57 -0700137 mContent = (CellLayout) findViewById(R.id.folder_content);
Adam Cohen2801caf2011-05-13 20:57:39 -0700138 mContent.setGridSize(0, 0);
139 mContent.enableHardwareLayers();
Adam Cohen76fc0852011-06-17 13:26:23 -0700140 mFolderName = (TextView) findViewById(R.id.folder_name);
141
142 // We find out how tall the text view wants to be (it is set to wrap_content), so that
143 // we can allocate the appropriate amount of space for it.
144 int measureSpec = MeasureSpec.UNSPECIFIED;
145 mFolderName.measure(measureSpec, measureSpec);
146 mFolderNameHeight = mFolderName.getMeasuredHeight();
147
148 // We disable action mode for now since it messes up the view on phones
149 mFolderName.setCustomSelectionActionModeCallback(mActionModeCallback);
150 mFolderName.setCursorVisible(false);
151 mFolderName.setOnEditorActionListener(this);
Adam Cohen4ef610f2011-06-21 22:37:36 -0700152 mFolderName.setSelectAllOnFocus(true);
Adam Cohen7a14d0b2011-06-24 11:36:35 -0700153 mFolderName.setInputType(mFolderName.getInputType() |
154 InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS | InputType.TYPE_TEXT_FLAG_CAP_WORDS);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800155 }
Adam Cohen2801caf2011-05-13 20:57:39 -0700156
Adam Cohen76fc0852011-06-17 13:26:23 -0700157 private ActionMode.Callback mActionModeCallback = new ActionMode.Callback() {
158 public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
159 return false;
160 }
161
162 public boolean onCreateActionMode(ActionMode mode, Menu menu) {
163 return false;
164 }
165
166 public void onDestroyActionMode(ActionMode mode) {
167 }
168
169 public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
170 return false;
171 }
172 };
173
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800174 public void onItemClick(AdapterView parent, View v, int position, long id) {
Joe Onorato0589f0f2010-02-08 13:44:00 -0800175 ShortcutInfo app = (ShortcutInfo) parent.getItemAtPosition(position);
Romain Guyfb5411e2010-02-24 10:04:17 -0800176 int[] pos = new int[2];
177 v.getLocationOnScreen(pos);
178 app.intent.setSourceBounds(new Rect(pos[0], pos[1],
179 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Joe Onoratof984e852010-03-25 09:47:45 -0700180 mLauncher.startActivitySafely(app.intent, app);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800181 }
182
183 public void onClick(View v) {
Adam Cohendf2cc412011-04-27 16:56:57 -0700184 Object tag = v.getTag();
185 if (tag instanceof ShortcutInfo) {
186 // refactor this code from Folder
187 ShortcutInfo item = (ShortcutInfo) tag;
188 int[] pos = new int[2];
189 v.getLocationOnScreen(pos);
190 item.intent.setSourceBounds(new Rect(pos[0], pos[1],
191 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
192 mLauncher.startActivitySafely(item.intent, item);
Adam Cohendf2cc412011-04-27 16:56:57 -0700193 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800194 }
195
Adam Cohen76fc0852011-06-17 13:26:23 -0700196 public boolean onInterceptTouchEvent(MotionEvent ev) {
197 if (ev.getAction() == MotionEvent.ACTION_DOWN) {
198 mFolderName.getHitRect(mHitRect);
199 if (mHitRect.contains((int) ev.getX(), (int) ev.getY()) && !mIsEditingName) {
200 startEditingFolderName();
201 }
202 }
203 return false;
204 }
205
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800206 public boolean onLongClick(View v) {
Adam Cohendf2cc412011-04-27 16:56:57 -0700207 Object tag = v.getTag();
208 if (tag instanceof ShortcutInfo) {
Adam Cohendf2cc412011-04-27 16:56:57 -0700209 ShortcutInfo item = (ShortcutInfo) tag;
210 if (!v.isInTouchMode()) {
211 return false;
212 }
213
214 mLauncher.getWorkspace().onDragStartedWithItem(v);
215 mDragController.startDrag(v, this, item, DragController.DRAG_ACTION_COPY);
Adam Cohena9cf38f2011-05-02 15:36:58 -0700216 mDragItemPosition[0] = item.cellX;
217 mDragItemPosition[1] = item.cellY;
Adam Cohenbadf71e2011-05-26 19:08:29 -0700218 mIconDrawable = ((TextView) v).getCompoundDrawables()[1];
Adam Cohen76078c42011-06-09 15:06:52 -0700219
220 mCurrentDragInfo = item;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700221 mEmptyCell[0] = item.cellX;
222 mEmptyCell[1] = item.cellY;
223 mCurrentDragView = v;
224 mContent.removeView(mCurrentDragView);
225 mInfo.remove(item);
Adam Cohendf2cc412011-04-27 16:56:57 -0700226 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800227 return true;
228 }
229
Adam Cohen76fc0852011-06-17 13:26:23 -0700230 public boolean isEditingName() {
231 return mIsEditingName;
232 }
233
234 public void startEditingFolderName() {
235 mFolderName.setCursorVisible(true);
236 mIsEditingName = true;
237 }
238
239 public void dismissEditingName() {
240 mInputMethodManager.hideSoftInputFromWindow(getWindowToken(), 0);
241 doneEditingFolderName(true);
242 }
243
244 public void doneEditingFolderName(boolean commit) {
245 mInfo.setTitle(mFolderName.getText());
246 LauncherModel.updateItemInDatabase(mLauncher, mInfo);
247 mFolderName.setCursorVisible(false);
248 mFolderName.clearFocus();
249 mIsEditingName = false;
250 }
251
252 public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
253 if (actionId == EditorInfo.IME_ACTION_DONE) {
254 dismissEditingName();
255 return true;
256 }
257 return false;
258 }
259
260 public View getEditTextRegion() {
261 return mFolderName;
262 }
263
Adam Cohenbadf71e2011-05-26 19:08:29 -0700264 public Drawable getDragDrawable() {
265 return mIconDrawable;
266 }
267
Adam Cohen0c872ba2011-05-05 10:34:16 -0700268 /**
269 * We need to handle touch events to prevent them from falling through to the workspace below.
270 */
271 @Override
272 public boolean onTouchEvent(MotionEvent ev) {
273 return true;
274 }
275
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800276 public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
277 if (!view.isInTouchMode()) {
278 return false;
279 }
280
Joe Onorato0589f0f2010-02-08 13:44:00 -0800281 ShortcutInfo app = (ShortcutInfo) parent.getItemAtPosition(position);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800282
Joe Onorato00acb122009-08-04 16:04:30 -0400283 mDragController.startDrag(view, this, app, DragController.DRAG_ACTION_COPY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800284 mLauncher.closeFolder(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800285 return true;
286 }
287
Joe Onorato00acb122009-08-04 16:04:30 -0400288 public void setDragController(DragController dragController) {
289 mDragController = dragController;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800290 }
291
Patrick Dubroya669d792010-11-23 14:40:33 -0800292 public void onDragViewVisible() {
293 }
294
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800295 void setLauncher(Launcher launcher) {
296 mLauncher = launcher;
297 }
Adam Cohen2801caf2011-05-13 20:57:39 -0700298
299 void setFolderIcon(FolderIcon icon) {
300 mFolderIcon = icon;
301 }
302
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800303 /**
304 * @return the FolderInfo object associated with this folder
305 */
306 FolderInfo getInfo() {
307 return mInfo;
308 }
309
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800310 void onOpen() {
Adam Cohendf2cc412011-04-27 16:56:57 -0700311 // When the folder opens, we need to refresh the GridView's selection by
312 // forcing a layout
313 // TODO: find out if this is still necessary
314 mContent.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800315 }
316
317 void onClose() {
Adam Cohen76fc0852011-06-17 13:26:23 -0700318 CellLayoutChildren clc = (CellLayoutChildren) getParent();
319 final CellLayout cellLayout = (CellLayout) clc.getParent();
320 cellLayout.removeViewWithoutMarkingCells(Folder.this);
321 clearFocus();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800322 }
323
324 void bind(FolderInfo info) {
325 mInfo = info;
Adam Cohendf2cc412011-04-27 16:56:57 -0700326 ArrayList<ShortcutInfo> children = info.contents;
Adam Cohen7c693212011-05-18 15:26:57 -0700327 setupContentForNumItems(children.size());
Adam Cohendf2cc412011-04-27 16:56:57 -0700328 for (int i = 0; i < children.size(); i++) {
329 ShortcutInfo child = (ShortcutInfo) children.get(i);
Adam Cohen7c693212011-05-18 15:26:57 -0700330 createAndAddShortcut(child);
Adam Cohendf2cc412011-04-27 16:56:57 -0700331 }
Adam Cohen4dbe6d92011-05-18 17:14:15 -0700332 mItemsInvalidated = true;
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() {
358 if (!(getParent() instanceof CellLayoutChildren)) return;
359
Adam Cohen2801caf2011-05-13 20:57:39 -0700360 CellLayout.LayoutParams iconLp = (CellLayout.LayoutParams) mFolderIcon.getLayoutParams();
Adam Cohendf2cc412011-04-27 16:56:57 -0700361 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) getLayoutParams();
362
Adam Cohen2801caf2011-05-13 20:57:39 -0700363 if (mMode == PARTIAL_GROW) {
364 setScaleX(0.8f);
365 setScaleY(0.8f);
366 setAlpha(0f);
367 } else {
368 lp.width = iconLp.width;
369 lp.height = iconLp.height;
370 lp.x = iconLp.x;
371 lp.y = iconLp.y;
372 mContent.setAlpha(0);
373 }
Adam Cohendf2cc412011-04-27 16:56:57 -0700374 mState = STATE_SMALL;
375 }
376
377 public void animateOpen() {
378 if (mState != STATE_SMALL) {
379 positionAndSizeAsIcon();
380 }
381 if (!(getParent() instanceof CellLayoutChildren)) return;
382
Adam Cohen2801caf2011-05-13 20:57:39 -0700383 ObjectAnimator oa;
Adam Cohendf2cc412011-04-27 16:56:57 -0700384 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) getLayoutParams();
385
Adam Cohen2801caf2011-05-13 20:57:39 -0700386 centerAboutIcon();
387 if (mMode == PARTIAL_GROW) {
388 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1);
389 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.0f);
390 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.0f);
391 oa = ObjectAnimator.ofPropertyValuesHolder(this, alpha, scaleX, scaleY);
392 } else {
393 PropertyValuesHolder width = PropertyValuesHolder.ofInt("width", mNewSize.width());
394 PropertyValuesHolder height = PropertyValuesHolder.ofInt("height", mNewSize.height());
395 PropertyValuesHolder x = PropertyValuesHolder.ofInt("x", mNewSize.left);
396 PropertyValuesHolder y = PropertyValuesHolder.ofInt("y", mNewSize.top);
397 oa = ObjectAnimator.ofPropertyValuesHolder(lp, width, height, x, y);
398 oa.addUpdateListener(new AnimatorUpdateListener() {
399 public void onAnimationUpdate(ValueAnimator animation) {
400 requestLayout();
401 }
402 });
Adam Cohendf2cc412011-04-27 16:56:57 -0700403
Adam Cohen2801caf2011-05-13 20:57:39 -0700404 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1.0f);
405 ObjectAnimator alphaOa = ObjectAnimator.ofPropertyValuesHolder(mContent, alpha);
406 alphaOa.setDuration(mExpandDuration);
407 alphaOa.setInterpolator(new AccelerateInterpolator(2.0f));
408 alphaOa.start();
409 }
Adam Cohendf2cc412011-04-27 16:56:57 -0700410
Adam Cohen2801caf2011-05-13 20:57:39 -0700411 oa.addListener(new AnimatorListenerAdapter() {
Adam Cohendf2cc412011-04-27 16:56:57 -0700412 @Override
413 public void onAnimationStart(Animator animation) {
414 mState = STATE_ANIMATING;
415 }
416 @Override
417 public void onAnimationEnd(Animator animation) {
Adam Cohen2801caf2011-05-13 20:57:39 -0700418 mState = STATE_OPEN;
Adam Cohendf2cc412011-04-27 16:56:57 -0700419 }
420 });
Adam Cohen2801caf2011-05-13 20:57:39 -0700421 oa.setDuration(mExpandDuration);
422 oa.start();
Adam Cohendf2cc412011-04-27 16:56:57 -0700423 }
424
425 public void animateClosed() {
426 if (!(getParent() instanceof CellLayoutChildren)) return;
427
Adam Cohen2801caf2011-05-13 20:57:39 -0700428 ObjectAnimator oa;
Adam Cohen2801caf2011-05-13 20:57:39 -0700429 if (mMode == PARTIAL_GROW) {
430 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0);
431 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 0.9f);
432 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 0.9f);
433 oa = ObjectAnimator.ofPropertyValuesHolder(this, alpha, scaleX, scaleY);
434 } else {
435 CellLayout.LayoutParams iconLp = (CellLayout.LayoutParams) mFolderIcon.getLayoutParams();
436 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) getLayoutParams();
Adam Cohendf2cc412011-04-27 16:56:57 -0700437
Adam Cohen2801caf2011-05-13 20:57:39 -0700438 PropertyValuesHolder width = PropertyValuesHolder.ofInt("width", iconLp.width);
439 PropertyValuesHolder height = PropertyValuesHolder.ofInt("height", iconLp.height);
440 PropertyValuesHolder x = PropertyValuesHolder.ofInt("x",iconLp.x);
441 PropertyValuesHolder y = PropertyValuesHolder.ofInt("y", iconLp.y);
442 oa = ObjectAnimator.ofPropertyValuesHolder(lp, width, height, x, y);
443 oa.addUpdateListener(new AnimatorUpdateListener() {
444 public void onAnimationUpdate(ValueAnimator animation) {
445 requestLayout();
446 }
447 });
Adam Cohendf2cc412011-04-27 16:56:57 -0700448
Adam Cohen2801caf2011-05-13 20:57:39 -0700449 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0f);
450 ObjectAnimator alphaOa = ObjectAnimator.ofPropertyValuesHolder(mContent, alpha);
451 alphaOa.setDuration(mExpandDuration);
452 alphaOa.setInterpolator(new DecelerateInterpolator(2.0f));
453 alphaOa.start();
454 }
Adam Cohendf2cc412011-04-27 16:56:57 -0700455
Adam Cohen2801caf2011-05-13 20:57:39 -0700456 oa.addListener(new AnimatorListenerAdapter() {
Adam Cohendf2cc412011-04-27 16:56:57 -0700457 @Override
458 public void onAnimationEnd(Animator animation) {
Adam Cohen76fc0852011-06-17 13:26:23 -0700459 onClose();
Adam Cohen2801caf2011-05-13 20:57:39 -0700460 onCloseComplete();
Adam Cohen2801caf2011-05-13 20:57:39 -0700461 mState = STATE_SMALL;
Adam Cohendf2cc412011-04-27 16:56:57 -0700462 }
463 @Override
464 public void onAnimationStart(Animator animation) {
465 mState = STATE_ANIMATING;
466 }
467 });
Adam Cohen2801caf2011-05-13 20:57:39 -0700468 oa.setDuration(mExpandDuration);
469 oa.start();
Adam Cohendf2cc412011-04-27 16:56:57 -0700470 }
471
472 void notifyDataSetChanged() {
473 // recreate all the children if the data set changes under us. We may want to do this more
474 // intelligently (ie just removing the views that should no longer exist)
475 mContent.removeAllViewsInLayout();
476 bind(mInfo);
477 }
478
Adam Cohencb3382b2011-05-24 14:07:08 -0700479 public boolean acceptDrop(DragObject d) {
480 final ItemInfo item = (ItemInfo) d.dragInfo;
Adam Cohendf2cc412011-04-27 16:56:57 -0700481 final int itemType = item.itemType;
Adam Cohen2801caf2011-05-13 20:57:39 -0700482 return ((itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION ||
483 itemType == LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT) &&
484 !isFull());
Adam Cohendf2cc412011-04-27 16:56:57 -0700485 }
486
Adam Cohendf2cc412011-04-27 16:56:57 -0700487 protected boolean findAndSetEmptyCells(ShortcutInfo item) {
488 int[] emptyCell = new int[2];
489 if (mContent.findCellForSpan(emptyCell, item.spanX, item.spanY)) {
490 item.cellX = emptyCell[0];
491 item.cellY = emptyCell[1];
Adam Cohendf2cc412011-04-27 16:56:57 -0700492 return true;
493 } else {
494 return false;
495 }
496 }
497
498 protected void createAndAddShortcut(ShortcutInfo item) {
499 final TextView textView =
500 (TextView) mInflater.inflate(R.layout.application_boxed, this, false);
501 textView.setCompoundDrawablesWithIntrinsicBounds(null,
502 new FastBitmapDrawable(item.getIcon(mIconCache)), null, null);
503 textView.setText(item.title);
504 textView.setTag(item);
505
506 textView.setOnClickListener(this);
507 textView.setOnLongClickListener(this);
508
509 CellLayout.LayoutParams lp =
510 new CellLayout.LayoutParams(item.cellX, item.cellY, item.spanX, item.spanY);
511 boolean insert = false;
512 mContent.addViewToCellLayout(textView, insert ? 0 : -1, (int)item.id, lp, true);
513 }
514
Adam Cohencb3382b2011-05-24 14:07:08 -0700515 public void onDragEnter(DragObject d) {
Adam Cohenbfbfd262011-06-13 16:55:12 -0700516 mPreviousTargetCell[0] = -1;
517 mPreviousTargetCell[1] = -1;
Adam Cohen2801caf2011-05-13 20:57:39 -0700518 mContent.onDragEnter();
Adam Cohenbfbfd262011-06-13 16:55:12 -0700519 mOnExitAlarm.cancelAlarm();
520 }
521
522 OnAlarmListener mReorderAlarmListener = new OnAlarmListener() {
523 public void onAlarm(Alarm alarm) {
524 realTimeReorder(mEmptyCell, mTargetCell);
525 }
526 };
527
528 boolean readingOrderGreaterThan(int[] v1, int[] v2) {
529 if (v1[1] > v2[1] || (v1[1] == v2[1] && v1[0] > v2[0])) {
530 return true;
531 } else {
532 return false;
533 }
534 }
535
536 private void realTimeReorder(int[] empty, int[] target) {
537 boolean wrap;
538 int startX;
539 int endX;
540 int startY;
Adam Cohen76fc0852011-06-17 13:26:23 -0700541 int delay = 0;
542 float delayAmount = 30;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700543 if (readingOrderGreaterThan(target, empty)) {
544 wrap = empty[0] >= mContent.getCountX() - 1;
545 startY = wrap ? empty[1] + 1 : empty[1];
546 for (int y = startY; y <= target[1]; y++) {
547 startX = y == empty[1] ? empty[0] + 1 : 0;
548 endX = y < target[1] ? mContent.getCountX() - 1 : target[0];
549 for (int x = startX; x <= endX; x++) {
550 View v = mContent.getChildAt(x,y);
551 if (mContent.animateChildToPosition(v, empty[0], empty[1],
Adam Cohen76fc0852011-06-17 13:26:23 -0700552 REORDER_ANIMATION_DURATION, delay)) {
Adam Cohenbfbfd262011-06-13 16:55:12 -0700553 empty[0] = x;
554 empty[1] = y;
Adam Cohen76fc0852011-06-17 13:26:23 -0700555 delay += delayAmount;
556 delayAmount *= 0.9;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700557 }
558 }
559 }
560 } else {
561 wrap = empty[0] == 0;
562 startY = wrap ? empty[1] - 1 : empty[1];
563 for (int y = startY; y >= target[1]; y--) {
564 startX = y == empty[1] ? empty[0] - 1 : mContent.getCountX() - 1;
565 endX = y > target[1] ? 0 : target[0];
566 for (int x = startX; x >= endX; x--) {
567 View v = mContent.getChildAt(x,y);
568 if (mContent.animateChildToPosition(v, empty[0], empty[1],
Adam Cohen76fc0852011-06-17 13:26:23 -0700569 REORDER_ANIMATION_DURATION, delay)) {
Adam Cohenbfbfd262011-06-13 16:55:12 -0700570 empty[0] = x;
571 empty[1] = y;
Adam Cohen76fc0852011-06-17 13:26:23 -0700572 delay += delayAmount;
573 delayAmount *= 0.9;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700574 }
575 }
576 }
577 }
Adam Cohendf2cc412011-04-27 16:56:57 -0700578 }
579
Adam Cohencb3382b2011-05-24 14:07:08 -0700580 public void onDragOver(DragObject d) {
Adam Cohenbfbfd262011-06-13 16:55:12 -0700581 float[] r = getDragViewVisualCenter(d.x, d.y, d.xOffset, d.yOffset, d.dragView, null);
582 mTargetCell = mContent.findNearestArea((int) r[0], (int) r[1], 1, 1, mTargetCell);
583
584 if (mTargetCell[0] != mPreviousTargetCell[0] || mTargetCell[1] != mPreviousTargetCell[1]) {
585 mReorderAlarm.cancelAlarm();
586 mReorderAlarm.setOnAlarmListener(mReorderAlarmListener);
587 mReorderAlarm.setAlarm(150);
588 mPreviousTargetCell[0] = mTargetCell[0];
589 mPreviousTargetCell[1] = mTargetCell[1];
590 }
Adam Cohendf2cc412011-04-27 16:56:57 -0700591 }
592
Adam Cohenbfbfd262011-06-13 16:55:12 -0700593 // This is used to compute the visual center of the dragView. The idea is that
594 // the visual center represents the user's interpretation of where the item is, and hence
595 // is the appropriate point to use when determining drop location.
596 private float[] getDragViewVisualCenter(int x, int y, int xOffset, int yOffset,
597 DragView dragView, float[] recycle) {
598 float res[];
599 if (recycle == null) {
600 res = new float[2];
601 } else {
602 res = recycle;
603 }
604
605 // These represent the visual top and left of drag view if a dragRect was provided.
606 // If a dragRect was not provided, then they correspond to the actual view left and
607 // top, as the dragRect is in that case taken to be the entire dragView.
608 // R.dimen.dragViewOffsetY.
609 int left = x - xOffset;
610 int top = y - yOffset;
611
612 // In order to find the visual center, we shift by half the dragRect
613 res[0] = left + dragView.getDragRegion().width() / 2;
614 res[1] = top + dragView.getDragRegion().height() / 2;
615
616 return res;
617 }
618
619 OnAlarmListener mOnExitAlarmListener = new OnAlarmListener() {
620 public void onAlarm(Alarm alarm) {
Adam Cohen1d9af7d2011-06-22 15:26:58 -0700621 mLauncher.closeFolder();
Adam Cohenbfbfd262011-06-13 16:55:12 -0700622 mCurrentDragInfo = null;
623 mCurrentDragView = null;
624 mSuppressOnAdd = false;
625 mRearrangeOnClose = true;
626 }
627 };
628
Adam Cohencb3382b2011-05-24 14:07:08 -0700629 public void onDragExit(DragObject d) {
Adam Cohenbfbfd262011-06-13 16:55:12 -0700630 // We only close the folder if this is a true drag exit, ie. not because a drop
631 // has occurred above the folder.
632 if (!d.dragComplete) {
633 mOnExitAlarm.setOnAlarmListener(mOnExitAlarmListener);
634 mOnExitAlarm.setAlarm(ON_EXIT_CLOSE_DELAY);
635 }
636 mReorderAlarm.cancelAlarm();
Adam Cohen2801caf2011-05-13 20:57:39 -0700637 mContent.onDragExit();
638 }
639
Adam Cohenc0dcf592011-06-01 15:30:43 -0700640 public void onDropCompleted(View target, DragObject d, boolean success) {
Adam Cohenbfbfd262011-06-13 16:55:12 -0700641 mCurrentDragInfo = null;
642 mCurrentDragView = null;
643 mSuppressOnAdd = false;
Adam Cohen76078c42011-06-09 15:06:52 -0700644 if (!success) {
645 if (d.dragView != null) {
646 if (target instanceof CellLayout) {
647 // TODO: we should animate the item back to the folder in this case
648 }
649 }
Adam Cohenbfbfd262011-06-13 16:55:12 -0700650 // TODO: if the drag fails, we need to re-add the item
Adam Cohen76078c42011-06-09 15:06:52 -0700651 }
Adam Cohendf2cc412011-04-27 16:56:57 -0700652 }
653
654 public boolean isDropEnabled() {
655 return true;
656 }
657
Adam Cohencb3382b2011-05-24 14:07:08 -0700658 public DropTarget getDropTargetDelegate(DragObject d) {
Adam Cohendf2cc412011-04-27 16:56:57 -0700659 return null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800660 }
Adam Cohena9cf38f2011-05-02 15:36:58 -0700661
Adam Cohen2801caf2011-05-13 20:57:39 -0700662 private void setupContentDimension(int count) {
663 ArrayList<View> list = getItemsInReadingOrder();
664
665 int countX = mContent.getCountX();
666 int countY = mContent.getCountY();
Adam Cohen7c693212011-05-18 15:26:57 -0700667 boolean done = false;
Adam Cohen2801caf2011-05-13 20:57:39 -0700668
Adam Cohen7c693212011-05-18 15:26:57 -0700669 while (!done) {
670 int oldCountX = countX;
671 int oldCountY = countY;
672 if (countX * countY < count) {
673 // Current grid is too small, expand it
674 if (countX <= countY && countX < mMaxCountX) {
675 countX++;
676 } else if (countY < mMaxCountY) {
677 countY++;
678 }
679 if (countY == 0) countY++;
680 } else if ((countY - 1) * countX >= count && countY >= countX) {
681 countY = Math.max(0, countY - 1);
682 } else if ((countX - 1) * countY >= count) {
683 countX = Math.max(0, countX - 1);
Adam Cohen2801caf2011-05-13 20:57:39 -0700684 }
Adam Cohen7c693212011-05-18 15:26:57 -0700685 done = countX == oldCountX && countY == oldCountY;
Adam Cohen2801caf2011-05-13 20:57:39 -0700686 }
Adam Cohen7c693212011-05-18 15:26:57 -0700687 mContent.setGridSize(countX, countY);
Adam Cohen2801caf2011-05-13 20:57:39 -0700688 arrangeChildren(list);
689 }
690
691 public boolean isFull() {
692 return getItemCount() >= mMaxCountX * mMaxCountY;
693 }
694
695 private void centerAboutIcon() {
696 CellLayout.LayoutParams iconLp = (CellLayout.LayoutParams) mFolderIcon.getLayoutParams();
697 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) getLayoutParams();
698
699 int width = getPaddingLeft() + getPaddingRight() + mContent.getDesiredWidth();
Adam Cohen76fc0852011-06-17 13:26:23 -0700700 // Technically there is no padding at the bottom, but we add space equal to the padding
701 // and have to account for that here.
702 int height = getPaddingTop() + mContent.getDesiredHeight() + mFolderNameHeight;
Adam Cohen2801caf2011-05-13 20:57:39 -0700703
704 int centerX = iconLp.x + iconLp.width / 2;
705 int centerY = iconLp.y + iconLp.height / 2;
706 int centeredLeft = centerX - width / 2;
707 int centeredTop = centerY - height / 2;
708
709 CellLayoutChildren clc = (CellLayoutChildren) getParent();
710 int parentWidth = 0;
711 int parentHeight = 0;
712 if (clc != null) {
713 parentWidth = clc.getMeasuredWidth();
714 parentHeight = clc.getMeasuredHeight();
715 }
716
717 int left = Math.min(Math.max(0, centeredLeft), parentWidth - width);
718 int top = Math.min(Math.max(0, centeredTop), parentHeight - height);
719
720 int folderPivotX = width / 2 + (centeredLeft - left);
721 int folderPivotY = height / 2 + (centeredTop - top);
722 setPivotX(folderPivotX);
723 setPivotY(folderPivotY);
724 int folderIconPivotX = (int) (mFolderIcon.getMeasuredWidth() *
725 (1.0f * folderPivotX / width));
726 int folderIconPivotY = (int) (mFolderIcon.getMeasuredHeight() *
727 (1.0f * folderPivotY / height));
728 mFolderIcon.setPivotX(folderIconPivotX);
729 mFolderIcon.setPivotY(folderIconPivotY);
730
731 if (mMode == PARTIAL_GROW) {
732 lp.width = width;
733 lp.height = height;
734 lp.x = left;
735 lp.y = top;
736 } else {
737 mNewSize.set(left, top, left + width, top + height);
738 }
739 }
740
741 private void setupContentForNumItems(int count) {
Adam Cohen2801caf2011-05-13 20:57:39 -0700742 setupContentDimension(count);
743
744 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) getLayoutParams();
745 if (lp == null) {
746 lp = new CellLayout.LayoutParams(0, 0, -1, -1);
747 lp.isLockedToGrid = false;
748 setLayoutParams(lp);
749 }
750 centerAboutIcon();
751 }
752
753 private void arrangeChildren(ArrayList<View> list) {
754 int[] vacant = new int[2];
755 if (list == null) {
756 list = getItemsInReadingOrder();
757 }
758 mContent.removeAllViews();
759
760 for (int i = 0; i < list.size(); i++) {
761 View v = list.get(i);
762 mContent.getVacantCell(vacant, 1, 1);
763 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) v.getLayoutParams();
764 lp.cellX = vacant[0];
765 lp.cellY = vacant[1];
766 ItemInfo info = (ItemInfo) v.getTag();
767 info.cellX = vacant[0];
768 info.cellY = vacant[1];
769 boolean insert = false;
770 mContent.addViewToCellLayout(v, insert ? 0 : -1, (int)info.id, lp, true);
Adam Cohen7c693212011-05-18 15:26:57 -0700771 LauncherModel.addOrMoveItemInDatabase(mLauncher, info, mInfo.id, 0,
772 info.cellX, info.cellY);
Adam Cohen2801caf2011-05-13 20:57:39 -0700773 }
Adam Cohen7c693212011-05-18 15:26:57 -0700774 mItemsInvalidated = true;
Adam Cohen2801caf2011-05-13 20:57:39 -0700775 }
776
Adam Cohena9cf38f2011-05-02 15:36:58 -0700777 public int getItemCount() {
778 return mContent.getChildrenLayout().getChildCount();
779 }
780
781 public View getItemAt(int index) {
782 return mContent.getChildrenLayout().getChildAt(index);
783 }
784
Adam Cohen2801caf2011-05-13 20:57:39 -0700785 private void onCloseComplete() {
786 if (mRearrangeOnClose) {
787 setupContentForNumItems(getItemCount());
788 mRearrangeOnClose = false;
789 }
Adam Cohenafb01ee2011-06-23 15:38:03 -0700790 if (getItemCount() <= 1) {
791 replaceFolderWithFinalItem();
792 }
793 }
794
795 private void replaceFolderWithFinalItem() {
796 ItemInfo finalItem = null;
797
798 if (getItemCount() == 1) {
799 finalItem = mInfo.contents.get(0);
800 }
801
802 // Remove the folder completely
803 final CellLayout cellLayout = (CellLayout)
804 mLauncher.getWorkspace().getChildAt(mInfo.screen);
805 cellLayout.removeView(mFolderIcon);
806 if (mFolderIcon instanceof DropTarget) {
807 mDragController.removeDropTarget((DropTarget) mFolderIcon);
808 }
809 mLauncher.removeFolder(mInfo);
810
811 if (finalItem != null) {
812 LauncherModel.addOrMoveItemInDatabase(mLauncher, finalItem,
813 LauncherSettings.Favorites.CONTAINER_DESKTOP, mInfo.screen,
814 mInfo.cellX, mInfo.cellY);
815 }
816 LauncherModel.deleteFolderContentsFromDatabase(mLauncher, mInfo, true);
817
818 // Add the last remaining child to the workspace in place of the folder
819 if (finalItem != null) {
820 View child = mLauncher.createShortcut(R.layout.application, cellLayout,
821 (ShortcutInfo) finalItem);
822
823 mLauncher.getWorkspace().addInScreen(child, mInfo.screen, mInfo.cellX, mInfo.cellY,
824 mInfo.spanX, mInfo.spanY);
825 }
Adam Cohen2801caf2011-05-13 20:57:39 -0700826 }
827
Adam Cohenbfbfd262011-06-13 16:55:12 -0700828 public void onDrop(DragObject d) {
829 ShortcutInfo item;
830 if (d.dragInfo instanceof ApplicationInfo) {
831 // Came from all apps -- make a copy
832 item = ((ApplicationInfo) d.dragInfo).makeShortcut();
833 item.spanX = 1;
834 item.spanY = 1;
835 } else {
836 item = (ShortcutInfo) d.dragInfo;
837 }
838 // Dragged from self onto self
839 if (item == mCurrentDragInfo) {
840 ShortcutInfo si = (ShortcutInfo) mCurrentDragView.getTag();
841 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) mCurrentDragView.getLayoutParams();
842 si.cellX = lp.cellX = mEmptyCell[0];
843 si.cellX = lp.cellY = mEmptyCell[1];
844 mContent.addViewToCellLayout(mCurrentDragView, -1, (int)item.id, lp, true);
845 mSuppressOnAdd = true;
846 mItemsInvalidated = true;
847 setupContentDimension(getItemCount());
848 }
849 mInfo.add(item);
850 }
851
852 public void onAdd(ShortcutInfo item) {
853 mItemsInvalidated = true;
854 if (mSuppressOnAdd) return;
855 if (!findAndSetEmptyCells(item)) {
856 // The current layout is full, can we expand it?
857 setupContentForNumItems(getItemCount() + 1);
858 findAndSetEmptyCells(item);
859 }
860 createAndAddShortcut(item);
861 LauncherModel.addOrMoveItemInDatabase(
862 mLauncher, item, mInfo.id, 0, item.cellX, item.cellY);
863 }
864
Adam Cohena9cf38f2011-05-02 15:36:58 -0700865 public void onRemove(ShortcutInfo item) {
Adam Cohen7c693212011-05-18 15:26:57 -0700866 mItemsInvalidated = true;
Adam Cohenbfbfd262011-06-13 16:55:12 -0700867 if (item == mCurrentDragInfo) return;
Adam Cohena9cf38f2011-05-02 15:36:58 -0700868 View v = mContent.getChildAt(mDragItemPosition[0], mDragItemPosition[1]);
869 mContent.removeView(v);
Adam Cohen2801caf2011-05-13 20:57:39 -0700870 if (mState == STATE_ANIMATING) {
871 mRearrangeOnClose = true;
872 } else {
873 setupContentForNumItems(getItemCount());
874 }
Adam Cohenafb01ee2011-06-23 15:38:03 -0700875 if (getItemCount() <= 1) {
876 replaceFolderWithFinalItem();
877 }
Adam Cohena9cf38f2011-05-02 15:36:58 -0700878 }
Adam Cohen7c693212011-05-18 15:26:57 -0700879
Adam Cohen76078c42011-06-09 15:06:52 -0700880 public void onItemsChanged() {
881 }
Adam Cohen76fc0852011-06-17 13:26:23 -0700882 public void onTitleChanged(CharSequence title) {
883 }
Adam Cohen76078c42011-06-09 15:06:52 -0700884
Adam Cohen7c693212011-05-18 15:26:57 -0700885 public ArrayList<View> getItemsInReadingOrder() {
Adam Cohen76078c42011-06-09 15:06:52 -0700886 return getItemsInReadingOrder(true);
887 }
888
889 public ArrayList<View> getItemsInReadingOrder(boolean includeCurrentDragItem) {
Adam Cohen7c693212011-05-18 15:26:57 -0700890 if (mItemsInvalidated) {
891 mItemsInReadingOrder.clear();
892 for (int j = 0; j < mContent.getCountY(); j++) {
893 for (int i = 0; i < mContent.getCountX(); i++) {
894 View v = mContent.getChildAt(i, j);
895 if (v != null) {
Adam Cohen76078c42011-06-09 15:06:52 -0700896 ShortcutInfo info = (ShortcutInfo) v.getTag();
897 if (info != mCurrentDragInfo || includeCurrentDragItem) {
898 mItemsInReadingOrder.add(v);
899 }
Adam Cohen7c693212011-05-18 15:26:57 -0700900 }
901 }
902 }
903 mItemsInvalidated = false;
904 }
905 return mItemsInReadingOrder;
906 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800907}