Folder.java revision 2ecf995e0d2d55eb71d03f7230ca87270872d1a3
1/*
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
17package com.android.launcher2;
18
19import android.animation.Animator;
20import android.animation.AnimatorListenerAdapter;
21import android.animation.ObjectAnimator;
22import android.animation.PropertyValuesHolder;
23import android.content.Context;
24import android.content.res.Resources;
25import android.graphics.PointF;
26import android.graphics.Rect;
27import android.graphics.drawable.Drawable;
28import android.text.InputType;
29import android.text.Selection;
30import android.text.Spannable;
31import android.util.AttributeSet;
32import android.util.Log;
33import android.view.ActionMode;
34import android.view.KeyEvent;
35import android.view.LayoutInflater;
36import android.view.Menu;
37import android.view.MenuItem;
38import android.view.MotionEvent;
39import android.view.View;
40import android.view.accessibility.AccessibilityEvent;
41import android.view.accessibility.AccessibilityManager;
42import android.view.inputmethod.EditorInfo;
43import android.view.inputmethod.InputMethodManager;
44import android.widget.LinearLayout;
45import android.widget.TextView;
46
47import com.android.launcher.R;
48import com.android.launcher2.FolderInfo.FolderListener;
49
50import java.util.ArrayList;
51import java.util.Collections;
52import java.util.Comparator;
53
54/**
55 * Represents a set of icons chosen by the user or generated by the system.
56 */
57public class Folder extends LinearLayout implements DragSource, View.OnClickListener,
58        View.OnLongClickListener, DropTarget, FolderListener, TextView.OnEditorActionListener,
59        View.OnFocusChangeListener {
60    private static final String TAG = "Launcher.Folder";
61
62    protected DragController mDragController;
63    protected Launcher mLauncher;
64    protected FolderInfo mInfo;
65
66    static final int STATE_NONE = -1;
67    static final int STATE_SMALL = 0;
68    static final int STATE_ANIMATING = 1;
69    static final int STATE_OPEN = 2;
70
71    private int mExpandDuration;
72    protected CellLayout mContent;
73    private final LayoutInflater mInflater;
74    private final IconCache mIconCache;
75    private int mState = STATE_NONE;
76    private static final int REORDER_ANIMATION_DURATION = 230;
77    private static final int ON_EXIT_CLOSE_DELAY = 800;
78    private boolean mRearrangeOnClose = false;
79    private FolderIcon mFolderIcon;
80    private int mMaxCountX;
81    private int mMaxCountY;
82    private int mMaxNumItems;
83    private ArrayList<View> mItemsInReadingOrder = new ArrayList<View>();
84    private Drawable mIconDrawable;
85    boolean mItemsInvalidated = false;
86    private ShortcutInfo mCurrentDragInfo;
87    private View mCurrentDragView;
88    boolean mSuppressOnAdd = false;
89    private int[] mTargetCell = new int[2];
90    private int[] mPreviousTargetCell = new int[2];
91    private int[] mEmptyCell = new int[2];
92    private Alarm mReorderAlarm = new Alarm();
93    private Alarm mOnExitAlarm = new Alarm();
94    private int mFolderNameHeight;
95    private Rect mTempRect = new Rect();
96    private boolean mDragInProgress = false;
97    private boolean mDeleteFolderOnDropCompleted = false;
98    private boolean mSuppressFolderDeletion = false;
99    private boolean mItemAddedBackToSelfViaIcon = false;
100    FolderEditText mFolderName;
101    private float mFolderIconPivotX;
102    private float mFolderIconPivotY;
103
104    private boolean mIsEditingName = false;
105    private InputMethodManager mInputMethodManager;
106
107    private static String sDefaultFolderName;
108    private static String sHintText;
109    private ObjectAnimator mOpenCloseAnimator;
110
111    private boolean mDestroyed;
112
113    /**
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);
122        mInflater = LayoutInflater.from(context);
123        mIconCache = ((LauncherApplication)context.getApplicationContext()).getIconCache();
124
125        Resources res = getResources();
126        mMaxCountX = res.getInteger(R.integer.folder_max_count_x);
127        mMaxCountY = res.getInteger(R.integer.folder_max_count_y);
128        mMaxNumItems = res.getInteger(R.integer.folder_max_num_items);
129        if (mMaxCountX < 0 || mMaxCountY < 0 || mMaxNumItems < 0) {
130            mMaxCountX = LauncherModel.getCellCountX();
131            mMaxCountY = LauncherModel.getCellCountY();
132            mMaxNumItems = mMaxCountX * mMaxCountY;
133        }
134
135        mInputMethodManager = (InputMethodManager)
136                getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
137
138        mExpandDuration = res.getInteger(R.integer.config_folderAnimDuration);
139
140        if (sDefaultFolderName == null) {
141            sDefaultFolderName = res.getString(R.string.folder_name);
142        }
143        if (sHintText == null) {
144            sHintText = res.getString(R.string.folder_hint_text);
145        }
146        mLauncher = (Launcher) context;
147        // We need this view to be focusable in touch mode so that when text editing of the folder
148        // name is complete, we have something to focus on, thus hiding the cursor and giving
149        // reliable behvior when clicking the text field (since it will always gain focus on click).
150        setFocusableInTouchMode(true);
151    }
152
153    @Override
154    protected void onFinishInflate() {
155        super.onFinishInflate();
156        mContent = (CellLayout) findViewById(R.id.folder_content);
157        mContent.setGridSize(0, 0);
158        mContent.getShortcutsAndWidgets().setMotionEventSplittingEnabled(false);
159        mFolderName = (FolderEditText) findViewById(R.id.folder_name);
160        mFolderName.setFolder(this);
161        mFolderName.setOnFocusChangeListener(this);
162
163        // We find out how tall the text view wants to be (it is set to wrap_content), so that
164        // we can allocate the appropriate amount of space for it.
165        int measureSpec = MeasureSpec.UNSPECIFIED;
166        mFolderName.measure(measureSpec, measureSpec);
167        mFolderNameHeight = mFolderName.getMeasuredHeight();
168
169        // We disable action mode for now since it messes up the view on phones
170        mFolderName.setCustomSelectionActionModeCallback(mActionModeCallback);
171        mFolderName.setOnEditorActionListener(this);
172        mFolderName.setSelectAllOnFocus(true);
173        mFolderName.setInputType(mFolderName.getInputType() |
174                InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS | InputType.TYPE_TEXT_FLAG_CAP_WORDS);
175    }
176
177    private ActionMode.Callback mActionModeCallback = new ActionMode.Callback() {
178        public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
179            return false;
180        }
181
182        public boolean onCreateActionMode(ActionMode mode, Menu menu) {
183            return false;
184        }
185
186        public void onDestroyActionMode(ActionMode mode) {
187        }
188
189        public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
190            return false;
191        }
192    };
193
194    public void onClick(View v) {
195        Object tag = v.getTag();
196        if (tag instanceof ShortcutInfo) {
197            // refactor this code from Folder
198            ShortcutInfo item = (ShortcutInfo) tag;
199            int[] pos = new int[2];
200            v.getLocationOnScreen(pos);
201            item.intent.setSourceBounds(new Rect(pos[0], pos[1],
202                    pos[0] + v.getWidth(), pos[1] + v.getHeight()));
203
204            mLauncher.startActivitySafely(v, item.intent, item);
205        }
206    }
207
208    public boolean onLongClick(View v) {
209        // Return if global dragging is not enabled
210        if (!mLauncher.isDraggingEnabled()) return true;
211
212        Object tag = v.getTag();
213        if (tag instanceof ShortcutInfo) {
214            ShortcutInfo item = (ShortcutInfo) tag;
215            if (!v.isInTouchMode()) {
216                return false;
217            }
218
219            mLauncher.dismissFolderCling(null);
220
221            mLauncher.getWorkspace().onDragStartedWithItem(v);
222            mLauncher.getWorkspace().beginDragShared(v, this);
223            mIconDrawable = ((TextView) v).getCompoundDrawables()[1];
224
225            mCurrentDragInfo = item;
226            mEmptyCell[0] = item.cellX;
227            mEmptyCell[1] = item.cellY;
228            mCurrentDragView = v;
229
230            mContent.removeView(mCurrentDragView);
231            mInfo.remove(mCurrentDragInfo);
232            mDragInProgress = true;
233            mItemAddedBackToSelfViaIcon = false;
234        }
235        return true;
236    }
237
238    public boolean isEditingName() {
239        return mIsEditingName;
240    }
241
242    public void startEditingFolderName() {
243        mFolderName.setHint("");
244        mIsEditingName = true;
245    }
246
247    public void dismissEditingName() {
248        mInputMethodManager.hideSoftInputFromWindow(getWindowToken(), 0);
249        doneEditingFolderName(true);
250    }
251
252    public void doneEditingFolderName(boolean commit) {
253        mFolderName.setHint(sHintText);
254        // Convert to a string here to ensure that no other state associated with the text field
255        // gets saved.
256        String newTitle = mFolderName.getText().toString();
257        mInfo.setTitle(newTitle);
258        LauncherModel.updateItemInDatabase(mLauncher, mInfo);
259
260        if (commit) {
261            sendCustomAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED,
262                    String.format(getContext().getString(R.string.folder_renamed), newTitle));
263        }
264        // In order to clear the focus from the text field, we set the focus on ourself. This
265        // ensures that every time the field is clicked, focus is gained, giving reliable behavior.
266        requestFocus();
267
268        Selection.setSelection((Spannable) mFolderName.getText(), 0, 0);
269        mIsEditingName = false;
270    }
271
272    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
273        if (actionId == EditorInfo.IME_ACTION_DONE) {
274            dismissEditingName();
275            return true;
276        }
277        return false;
278    }
279
280    public View getEditTextRegion() {
281        return mFolderName;
282    }
283
284    public Drawable getDragDrawable() {
285        return mIconDrawable;
286    }
287
288    /**
289     * We need to handle touch events to prevent them from falling through to the workspace below.
290     */
291    @Override
292    public boolean onTouchEvent(MotionEvent ev) {
293        return true;
294    }
295
296    public void setDragController(DragController dragController) {
297        mDragController = dragController;
298    }
299
300    void setFolderIcon(FolderIcon icon) {
301        mFolderIcon = icon;
302    }
303
304    @Override
305    public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
306        // When the folder gets focus, we don't want to announce the list of items.
307        return true;
308    }
309
310    /**
311     * @return the FolderInfo object associated with this folder
312     */
313    FolderInfo getInfo() {
314        return mInfo;
315    }
316
317    private class GridComparator implements Comparator<ShortcutInfo> {
318        int mNumCols;
319        public GridComparator(int numCols) {
320            mNumCols = numCols;
321        }
322
323        @Override
324        public int compare(ShortcutInfo lhs, ShortcutInfo rhs) {
325            int lhIndex = lhs.cellY * mNumCols + lhs.cellX;
326            int rhIndex = rhs.cellY * mNumCols + rhs.cellX;
327            return (lhIndex - rhIndex);
328        }
329    }
330
331    private void placeInReadingOrder(ArrayList<ShortcutInfo> items) {
332        int maxX = 0;
333        int count = items.size();
334        for (int i = 0; i < count; i++) {
335            ShortcutInfo item = items.get(i);
336            if (item.cellX > maxX) {
337                maxX = item.cellX;
338            }
339        }
340
341        GridComparator gridComparator = new GridComparator(maxX + 1);
342        Collections.sort(items, gridComparator);
343        final int countX = mContent.getCountX();
344        for (int i = 0; i < count; i++) {
345            int x = i % countX;
346            int y = i / countX;
347            ShortcutInfo item = items.get(i);
348            item.cellX = x;
349            item.cellY = y;
350        }
351    }
352
353    void bind(FolderInfo info) {
354        mInfo = info;
355        ArrayList<ShortcutInfo> children = info.contents;
356        ArrayList<ShortcutInfo> overflow = new ArrayList<ShortcutInfo>();
357        setupContentForNumItems(children.size());
358        placeInReadingOrder(children);
359        int count = 0;
360        for (int i = 0; i < children.size(); i++) {
361            ShortcutInfo child = (ShortcutInfo) children.get(i);
362            if (!createAndAddShortcut(child)) {
363                overflow.add(child);
364            } else {
365                count++;
366            }
367        }
368
369        // We rearrange the items in case there are any empty gaps
370        setupContentForNumItems(count);
371
372        // If our folder has too many items we prune them from the list. This is an issue
373        // when upgrading from the old Folders implementation which could contain an unlimited
374        // number of items.
375        for (ShortcutInfo item: overflow) {
376            mInfo.remove(item);
377            LauncherModel.deleteItemFromDatabase(mLauncher, item);
378        }
379
380        mItemsInvalidated = true;
381        updateTextViewFocus();
382        mInfo.addListener(this);
383
384        if (!sDefaultFolderName.contentEquals(mInfo.title)) {
385            mFolderName.setText(mInfo.title);
386        } else {
387            mFolderName.setText("");
388        }
389        updateItemLocationsInDatabase();
390    }
391
392    /**
393     * Creates a new UserFolder, inflated from R.layout.user_folder.
394     *
395     * @param context The application's context.
396     *
397     * @return A new UserFolder.
398     */
399    static Folder fromXml(Context context) {
400        return (Folder) LayoutInflater.from(context).inflate(R.layout.user_folder, null);
401    }
402
403    /**
404     * This method is intended to make the UserFolder to be visually identical in size and position
405     * to its associated FolderIcon. This allows for a seamless transition into the expanded state.
406     */
407    private void positionAndSizeAsIcon() {
408        if (!(getParent() instanceof DragLayer)) return;
409        setScaleX(0.8f);
410        setScaleY(0.8f);
411        setAlpha(0f);
412        mState = STATE_SMALL;
413    }
414
415    public void animateOpen() {
416        positionAndSizeAsIcon();
417
418        if (!(getParent() instanceof DragLayer)) return;
419        centerAboutIcon();
420        PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1);
421        PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.0f);
422        PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.0f);
423        final ObjectAnimator oa = mOpenCloseAnimator =
424            LauncherAnimUtils.ofPropertyValuesHolder(this, alpha, scaleX, scaleY);
425
426        oa.addListener(new AnimatorListenerAdapter() {
427            @Override
428            public void onAnimationStart(Animator animation) {
429                sendCustomAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED,
430                        String.format(getContext().getString(R.string.folder_opened),
431                        mContent.getCountX(), mContent.getCountY()));
432                mState = STATE_ANIMATING;
433            }
434            @Override
435            public void onAnimationEnd(Animator animation) {
436                mState = STATE_OPEN;
437                setLayerType(LAYER_TYPE_NONE, null);
438                Cling cling = mLauncher.showFirstRunFoldersCling();
439                if (cling != null) {
440                    cling.bringToFront();
441                }
442                setFocusOnFirstChild();
443            }
444        });
445        oa.setDuration(mExpandDuration);
446        setLayerType(LAYER_TYPE_HARDWARE, null);
447        buildLayer();
448        post(new Runnable() {
449            public void run() {
450                // Check if the animator changed in the meantime
451                if (oa != mOpenCloseAnimator)
452                    return;
453                oa.start();
454            }
455        });
456    }
457
458    private void sendCustomAccessibilityEvent(int type, String text) {
459        AccessibilityManager accessibilityManager = (AccessibilityManager)
460                getContext().getSystemService(Context.ACCESSIBILITY_SERVICE);
461        if (accessibilityManager.isEnabled()) {
462            AccessibilityEvent event = AccessibilityEvent.obtain(type);
463            onInitializeAccessibilityEvent(event);
464            event.getText().add(text);
465            accessibilityManager.sendAccessibilityEvent(event);
466        }
467    }
468
469    private void setFocusOnFirstChild() {
470        View firstChild = mContent.getChildAt(0, 0);
471        if (firstChild != null) {
472            firstChild.requestFocus();
473        }
474    }
475
476    public void animateClosed() {
477        if (!(getParent() instanceof DragLayer)) return;
478        PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0);
479        PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 0.9f);
480        PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 0.9f);
481        final ObjectAnimator oa = mOpenCloseAnimator =
482                LauncherAnimUtils.ofPropertyValuesHolder(this, alpha, scaleX, scaleY);
483
484        oa.addListener(new AnimatorListenerAdapter() {
485            @Override
486            public void onAnimationEnd(Animator animation) {
487                onCloseComplete();
488                setLayerType(LAYER_TYPE_NONE, null);
489                mState = STATE_SMALL;
490            }
491            @Override
492            public void onAnimationStart(Animator animation) {
493                sendCustomAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED,
494                        getContext().getString(R.string.folder_closed));
495                mState = STATE_ANIMATING;
496            }
497        });
498        oa.setDuration(mExpandDuration);
499        setLayerType(LAYER_TYPE_HARDWARE, null);
500        buildLayer();
501        post(new Runnable() {
502            public void run() {
503                // Check if the animator changed in the meantime
504                if (oa != mOpenCloseAnimator)
505                    return;
506                oa.start();
507            }
508        });
509    }
510
511    void notifyDataSetChanged() {
512        // recreate all the children if the data set changes under us. We may want to do this more
513        // intelligently (ie just removing the views that should no longer exist)
514        mContent.removeAllViewsInLayout();
515        bind(mInfo);
516    }
517
518    public boolean acceptDrop(DragObject d) {
519        final ItemInfo item = (ItemInfo) d.dragInfo;
520        final int itemType = item.itemType;
521        return ((itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION ||
522                    itemType == LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT) &&
523                    !isFull());
524    }
525
526    protected boolean findAndSetEmptyCells(ShortcutInfo item) {
527        int[] emptyCell = new int[2];
528        if (mContent.findCellForSpan(emptyCell, item.spanX, item.spanY)) {
529            item.cellX = emptyCell[0];
530            item.cellY = emptyCell[1];
531            return true;
532        } else {
533            return false;
534        }
535    }
536
537    protected boolean createAndAddShortcut(ShortcutInfo item) {
538        final TextView textView =
539            (TextView) mInflater.inflate(R.layout.application, this, false);
540        textView.setCompoundDrawablesWithIntrinsicBounds(null,
541                new FastBitmapDrawable(item.getIcon(mIconCache)), null, null);
542        textView.setText(item.title);
543        textView.setTag(item);
544
545        textView.setOnClickListener(this);
546        textView.setOnLongClickListener(this);
547
548        // We need to check here to verify that the given item's location isn't already occupied
549        // by another item.
550        if (mContent.getChildAt(item.cellX, item.cellY) != null || item.cellX < 0 || item.cellY < 0
551                || item.cellX >= mContent.getCountX() || item.cellY >= mContent.getCountY()) {
552            // This shouldn't happen, log it.
553            Log.e(TAG, "Folder order not properly persisted during bind");
554            if (!findAndSetEmptyCells(item)) {
555                return false;
556            }
557        }
558
559        CellLayout.LayoutParams lp =
560            new CellLayout.LayoutParams(item.cellX, item.cellY, item.spanX, item.spanY);
561        boolean insert = false;
562        textView.setOnKeyListener(new FolderKeyEventListener());
563        mContent.addViewToCellLayout(textView, insert ? 0 : -1, (int)item.id, lp, true);
564        return true;
565    }
566
567    public void onDragEnter(DragObject d) {
568        mPreviousTargetCell[0] = -1;
569        mPreviousTargetCell[1] = -1;
570        mOnExitAlarm.cancelAlarm();
571    }
572
573    OnAlarmListener mReorderAlarmListener = new OnAlarmListener() {
574        public void onAlarm(Alarm alarm) {
575            realTimeReorder(mEmptyCell, mTargetCell);
576        }
577    };
578
579    boolean readingOrderGreaterThan(int[] v1, int[] v2) {
580        if (v1[1] > v2[1] || (v1[1] == v2[1] && v1[0] > v2[0])) {
581            return true;
582        } else {
583            return false;
584        }
585    }
586
587    private void realTimeReorder(int[] empty, int[] target) {
588        boolean wrap;
589        int startX;
590        int endX;
591        int startY;
592        int delay = 0;
593        float delayAmount = 30;
594        if (readingOrderGreaterThan(target, empty)) {
595            wrap = empty[0] >= mContent.getCountX() - 1;
596            startY = wrap ? empty[1] + 1 : empty[1];
597            for (int y = startY; y <= target[1]; y++) {
598                startX = y == empty[1] ? empty[0] + 1 : 0;
599                endX = y < target[1] ? mContent.getCountX() - 1 : target[0];
600                for (int x = startX; x <= endX; x++) {
601                    View v = mContent.getChildAt(x,y);
602                    if (mContent.animateChildToPosition(v, empty[0], empty[1],
603                            REORDER_ANIMATION_DURATION, delay, true, true)) {
604                        empty[0] = x;
605                        empty[1] = y;
606                        delay += delayAmount;
607                        delayAmount *= 0.9;
608                    }
609                }
610            }
611        } else {
612            wrap = empty[0] == 0;
613            startY = wrap ? empty[1] - 1 : empty[1];
614            for (int y = startY; y >= target[1]; y--) {
615                startX = y == empty[1] ? empty[0] - 1 : mContent.getCountX() - 1;
616                endX = y > target[1] ? 0 : target[0];
617                for (int x = startX; x >= endX; x--) {
618                    View v = mContent.getChildAt(x,y);
619                    if (mContent.animateChildToPosition(v, empty[0], empty[1],
620                            REORDER_ANIMATION_DURATION, delay, true, true)) {
621                        empty[0] = x;
622                        empty[1] = y;
623                        delay += delayAmount;
624                        delayAmount *= 0.9;
625                    }
626                }
627            }
628        }
629    }
630
631    public void onDragOver(DragObject d) {
632        float[] r = getDragViewVisualCenter(d.x, d.y, d.xOffset, d.yOffset, d.dragView, null);
633        mTargetCell = mContent.findNearestArea((int) r[0], (int) r[1], 1, 1, mTargetCell);
634
635        if (mTargetCell[0] != mPreviousTargetCell[0] || mTargetCell[1] != mPreviousTargetCell[1]) {
636            mReorderAlarm.cancelAlarm();
637            mReorderAlarm.setOnAlarmListener(mReorderAlarmListener);
638            mReorderAlarm.setAlarm(150);
639            mPreviousTargetCell[0] = mTargetCell[0];
640            mPreviousTargetCell[1] = mTargetCell[1];
641        }
642    }
643
644    // This is used to compute the visual center of the dragView. The idea is that
645    // the visual center represents the user's interpretation of where the item is, and hence
646    // is the appropriate point to use when determining drop location.
647    private float[] getDragViewVisualCenter(int x, int y, int xOffset, int yOffset,
648            DragView dragView, float[] recycle) {
649        float res[];
650        if (recycle == null) {
651            res = new float[2];
652        } else {
653            res = recycle;
654        }
655
656        // These represent the visual top and left of drag view if a dragRect was provided.
657        // If a dragRect was not provided, then they correspond to the actual view left and
658        // top, as the dragRect is in that case taken to be the entire dragView.
659        // R.dimen.dragViewOffsetY.
660        int left = x - xOffset;
661        int top = y - yOffset;
662
663        // In order to find the visual center, we shift by half the dragRect
664        res[0] = left + dragView.getDragRegion().width() / 2;
665        res[1] = top + dragView.getDragRegion().height() / 2;
666
667        return res;
668    }
669
670    OnAlarmListener mOnExitAlarmListener = new OnAlarmListener() {
671        public void onAlarm(Alarm alarm) {
672            completeDragExit();
673        }
674    };
675
676    public void completeDragExit() {
677        mLauncher.closeFolder();
678        mCurrentDragInfo = null;
679        mCurrentDragView = null;
680        mSuppressOnAdd = false;
681        mRearrangeOnClose = true;
682    }
683
684    public void onDragExit(DragObject d) {
685        // We only close the folder if this is a true drag exit, ie. not because a drop
686        // has occurred above the folder.
687        if (!d.dragComplete) {
688            mOnExitAlarm.setOnAlarmListener(mOnExitAlarmListener);
689            mOnExitAlarm.setAlarm(ON_EXIT_CLOSE_DELAY);
690        }
691        mReorderAlarm.cancelAlarm();
692    }
693
694    public void onDropCompleted(View target, DragObject d, boolean isFlingToDelete,
695            boolean success) {
696        if (success) {
697            if (mDeleteFolderOnDropCompleted && !mItemAddedBackToSelfViaIcon) {
698                replaceFolderWithFinalItem();
699            }
700        } else {
701            // The drag failed, we need to return the item to the folder
702            mFolderIcon.onDrop(d);
703
704            // We're going to trigger a "closeFolder" which may occur before this item has
705            // been added back to the folder -- this could cause the folder to be deleted
706            if (mOnExitAlarm.alarmPending()) {
707                mSuppressFolderDeletion = true;
708            }
709        }
710
711        if (target != this) {
712            if (mOnExitAlarm.alarmPending()) {
713                mOnExitAlarm.cancelAlarm();
714                completeDragExit();
715            }
716        }
717        mDeleteFolderOnDropCompleted = false;
718        mDragInProgress = false;
719        mItemAddedBackToSelfViaIcon = false;
720        mCurrentDragInfo = null;
721        mCurrentDragView = null;
722        mSuppressOnAdd = false;
723
724        // Reordering may have occured, and we need to save the new item locations. We do this once
725        // at the end to prevent unnecessary database operations.
726        updateItemLocationsInDatabase();
727    }
728
729    @Override
730    public boolean supportsFlingToDelete() {
731        return true;
732    }
733
734    public void onFlingToDelete(DragObject d, int x, int y, PointF vec) {
735        // Do nothing
736    }
737
738    @Override
739    public void onFlingToDeleteCompleted() {
740        // Do nothing
741    }
742
743    private void updateItemLocationsInDatabase() {
744        ArrayList<View> list = getItemsInReadingOrder();
745        for (int i = 0; i < list.size(); i++) {
746            View v = list.get(i);
747            ItemInfo info = (ItemInfo) v.getTag();
748            LauncherModel.moveItemInDatabase(mLauncher, info, mInfo.id, 0,
749                        info.cellX, info.cellY);
750        }
751    }
752
753    public void notifyDrop() {
754        if (mDragInProgress) {
755            mItemAddedBackToSelfViaIcon = true;
756        }
757    }
758
759    public boolean isDropEnabled() {
760        return true;
761    }
762
763    public DropTarget getDropTargetDelegate(DragObject d) {
764        return null;
765    }
766
767    private void setupContentDimensions(int count) {
768        ArrayList<View> list = getItemsInReadingOrder();
769
770        int countX = mContent.getCountX();
771        int countY = mContent.getCountY();
772        boolean done = false;
773
774        while (!done) {
775            int oldCountX = countX;
776            int oldCountY = countY;
777            if (countX * countY < count) {
778                // Current grid is too small, expand it
779                if ((countX <= countY || countY == mMaxCountY) && countX < mMaxCountX) {
780                    countX++;
781                } else if (countY < mMaxCountY) {
782                    countY++;
783                }
784                if (countY == 0) countY++;
785            } else if ((countY - 1) * countX >= count && countY >= countX) {
786                countY = Math.max(0, countY - 1);
787            } else if ((countX - 1) * countY >= count) {
788                countX = Math.max(0, countX - 1);
789            }
790            done = countX == oldCountX && countY == oldCountY;
791        }
792        mContent.setGridSize(countX, countY);
793        arrangeChildren(list);
794    }
795
796    public boolean isFull() {
797        return getItemCount() >= mMaxNumItems;
798    }
799
800    private void centerAboutIcon() {
801        DragLayer.LayoutParams lp = (DragLayer.LayoutParams) getLayoutParams();
802
803        int width = getPaddingLeft() + getPaddingRight() + mContent.getDesiredWidth();
804        int height = getPaddingTop() + getPaddingBottom() + mContent.getDesiredHeight()
805                + mFolderNameHeight;
806        DragLayer parent = (DragLayer) mLauncher.findViewById(R.id.drag_layer);
807
808        parent.getDescendantRectRelativeToSelf(mFolderIcon, mTempRect);
809
810        int centerX = mTempRect.centerX();
811        int centerY = mTempRect.centerY();
812        int centeredLeft = centerX - width / 2;
813        int centeredTop = centerY - height / 2;
814
815        int currentPage = mLauncher.getWorkspace().getCurrentPage();
816        // In case the workspace is scrolling, we need to use the final scroll to compute
817        // the folders bounds.
818        mLauncher.getWorkspace().setFinalScrollForPageChange(currentPage);
819        // We first fetch the currently visible CellLayoutChildren
820        CellLayout currentLayout = (CellLayout) mLauncher.getWorkspace().getChildAt(currentPage);
821        ShortcutAndWidgetContainer boundingLayout = currentLayout.getShortcutsAndWidgets();
822        Rect bounds = new Rect();
823        parent.getDescendantRectRelativeToSelf(boundingLayout, bounds);
824        // We reset the workspaces scroll
825        mLauncher.getWorkspace().resetFinalScrollForPageChange(currentPage);
826
827        // We need to bound the folder to the currently visible CellLayoutChildren
828        int left = Math.min(Math.max(bounds.left, centeredLeft),
829                bounds.left + bounds.width() - width);
830        int top = Math.min(Math.max(bounds.top, centeredTop),
831                bounds.top + bounds.height() - height);
832        // If the folder doesn't fit within the bounds, center it about the desired bounds
833        if (width >= bounds.width()) {
834            left = bounds.left + (bounds.width() - width) / 2;
835        }
836        if (height >= bounds.height()) {
837            top = bounds.top + (bounds.height() - height) / 2;
838        }
839
840        int folderPivotX = width / 2 + (centeredLeft - left);
841        int folderPivotY = height / 2 + (centeredTop - top);
842        setPivotX(folderPivotX);
843        setPivotY(folderPivotY);
844        mFolderIconPivotX = (int) (mFolderIcon.getMeasuredWidth() *
845                (1.0f * folderPivotX / width));
846        mFolderIconPivotY = (int) (mFolderIcon.getMeasuredHeight() *
847                (1.0f * folderPivotY / height));
848
849        lp.width = width;
850        lp.height = height;
851        lp.x = left;
852        lp.y = top;
853    }
854
855    float getPivotXForIconAnimation() {
856        return mFolderIconPivotX;
857    }
858    float getPivotYForIconAnimation() {
859        return mFolderIconPivotY;
860    }
861
862    private void setupContentForNumItems(int count) {
863        setupContentDimensions(count);
864
865        DragLayer.LayoutParams lp = (DragLayer.LayoutParams) getLayoutParams();
866        if (lp == null) {
867            lp = new DragLayer.LayoutParams(0, 0);
868            lp.customPosition = true;
869            setLayoutParams(lp);
870        }
871        centerAboutIcon();
872    }
873
874    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
875        int width = getPaddingLeft() + getPaddingRight() + mContent.getDesiredWidth();
876        int height = getPaddingTop() + getPaddingBottom() + mContent.getDesiredHeight()
877                + mFolderNameHeight;
878
879        int contentWidthSpec = MeasureSpec.makeMeasureSpec(mContent.getDesiredWidth(),
880                MeasureSpec.EXACTLY);
881        int contentHeightSpec = MeasureSpec.makeMeasureSpec(mContent.getDesiredHeight(),
882                MeasureSpec.EXACTLY);
883        mContent.measure(contentWidthSpec, contentHeightSpec);
884
885        mFolderName.measure(contentWidthSpec,
886                MeasureSpec.makeMeasureSpec(mFolderNameHeight, MeasureSpec.EXACTLY));
887        setMeasuredDimension(width, height);
888    }
889
890    private void arrangeChildren(ArrayList<View> list) {
891        int[] vacant = new int[2];
892        if (list == null) {
893            list = getItemsInReadingOrder();
894        }
895        mContent.removeAllViews();
896
897        for (int i = 0; i < list.size(); i++) {
898            View v = list.get(i);
899            mContent.getVacantCell(vacant, 1, 1);
900            CellLayout.LayoutParams lp = (CellLayout.LayoutParams) v.getLayoutParams();
901            lp.cellX = vacant[0];
902            lp.cellY = vacant[1];
903            ItemInfo info = (ItemInfo) v.getTag();
904            if (info.cellX != vacant[0] || info.cellY != vacant[1]) {
905                info.cellX = vacant[0];
906                info.cellY = vacant[1];
907                LauncherModel.addOrMoveItemInDatabase(mLauncher, info, mInfo.id, 0,
908                        info.cellX, info.cellY);
909            }
910            boolean insert = false;
911            mContent.addViewToCellLayout(v, insert ? 0 : -1, (int)info.id, lp, true);
912        }
913        mItemsInvalidated = true;
914    }
915
916    public int getItemCount() {
917        return mContent.getShortcutsAndWidgets().getChildCount();
918    }
919
920    public View getItemAt(int index) {
921        return mContent.getShortcutsAndWidgets().getChildAt(index);
922    }
923
924    private void onCloseComplete() {
925        DragLayer parent = (DragLayer) getParent();
926        if (parent != null) {
927            parent.removeView(this);
928        }
929        mDragController.removeDropTarget((DropTarget) this);
930        clearFocus();
931        mFolderIcon.requestFocus();
932
933        if (mRearrangeOnClose) {
934            setupContentForNumItems(getItemCount());
935            mRearrangeOnClose = false;
936        }
937        if (getItemCount() <= 1) {
938            if (!mDragInProgress && !mSuppressFolderDeletion) {
939                replaceFolderWithFinalItem();
940            } else if (mDragInProgress) {
941                mDeleteFolderOnDropCompleted = true;
942            }
943        }
944        mSuppressFolderDeletion = false;
945    }
946
947    private void replaceFolderWithFinalItem() {
948        // Add the last remaining child to the workspace in place of the folder
949        Runnable onCompleteRunnable = new Runnable() {
950            @Override
951            public void run() {
952                CellLayout cellLayout = mLauncher.getCellLayout(mInfo.container, mInfo.screen);
953
954                if (getItemCount() <= 1) {
955                    // Remove the folder
956                    LauncherModel.deleteItemFromDatabase(mLauncher, mInfo);
957                    cellLayout.removeView(mFolderIcon);
958                    if (mFolderIcon instanceof DropTarget) {
959                        mDragController.removeDropTarget((DropTarget) mFolderIcon);
960                    }
961                    mLauncher.removeFolder(mInfo);
962                }
963
964                // Move the item from the folder to the workspace, in the position of the folder
965                if (getItemCount() == 1) {
966                    ShortcutInfo finalItem = mInfo.contents.get(0);
967
968                    final View child = mLauncher.createShortcut(R.layout.application, cellLayout,
969                            finalItem);
970                    LauncherModel.addOrMoveItemInDatabase(mLauncher, finalItem, mInfo.container,
971                            mInfo.screen, mInfo.cellX, mInfo.cellY);
972                    mLauncher.getWorkspace().addInScreen(child, mInfo.container, mInfo.screen,
973                            mInfo.cellX, mInfo.cellY, mInfo.spanX, mInfo.spanY);
974                }
975
976            }
977        };
978        View finalChild = getItemAt(0);
979        if (finalChild != null) {
980            mFolderIcon.performDestroyAnimation(finalChild, onCompleteRunnable);
981        }
982        mDestroyed = true;
983    }
984
985    boolean isDestroyed() {
986        return mDestroyed;
987    }
988
989    // This method keeps track of the last item in the folder for the purposes
990    // of keyboard focus
991    private void updateTextViewFocus() {
992        View lastChild = getItemAt(getItemCount() - 1);
993        getItemAt(getItemCount() - 1);
994        if (lastChild != null) {
995            mFolderName.setNextFocusDownId(lastChild.getId());
996            mFolderName.setNextFocusRightId(lastChild.getId());
997            mFolderName.setNextFocusLeftId(lastChild.getId());
998            mFolderName.setNextFocusUpId(lastChild.getId());
999        }
1000    }
1001
1002    public void onDrop(DragObject d) {
1003        ShortcutInfo item;
1004        if (d.dragInfo instanceof ApplicationInfo) {
1005            // Came from all apps -- make a copy
1006            item = ((ApplicationInfo) d.dragInfo).makeShortcut();
1007            item.spanX = 1;
1008            item.spanY = 1;
1009        } else {
1010            item = (ShortcutInfo) d.dragInfo;
1011        }
1012        // Dragged from self onto self, currently this is the only path possible, however
1013        // we keep this as a distinct code path.
1014        if (item == mCurrentDragInfo) {
1015            ShortcutInfo si = (ShortcutInfo) mCurrentDragView.getTag();
1016            CellLayout.LayoutParams lp = (CellLayout.LayoutParams) mCurrentDragView.getLayoutParams();
1017            si.cellX = lp.cellX = mEmptyCell[0];
1018            si.cellX = lp.cellY = mEmptyCell[1];
1019            mContent.addViewToCellLayout(mCurrentDragView, -1, (int)item.id, lp, true);
1020            if (d.dragView.hasDrawn()) {
1021                mLauncher.getDragLayer().animateViewIntoPosition(d.dragView, mCurrentDragView);
1022            } else {
1023                d.deferDragViewCleanupPostAnimation = false;
1024                mCurrentDragView.setVisibility(VISIBLE);
1025            }
1026            mItemsInvalidated = true;
1027            setupContentDimensions(getItemCount());
1028            mSuppressOnAdd = true;
1029        }
1030        mInfo.add(item);
1031    }
1032
1033    public void onAdd(ShortcutInfo item) {
1034        mItemsInvalidated = true;
1035        // If the item was dropped onto this open folder, we have done the work associated
1036        // with adding the item to the folder, as indicated by mSuppressOnAdd being set
1037        if (mSuppressOnAdd) return;
1038        if (!findAndSetEmptyCells(item)) {
1039            // The current layout is full, can we expand it?
1040            setupContentForNumItems(getItemCount() + 1);
1041            findAndSetEmptyCells(item);
1042        }
1043        createAndAddShortcut(item);
1044        LauncherModel.addOrMoveItemInDatabase(
1045                mLauncher, item, mInfo.id, 0, item.cellX, item.cellY);
1046    }
1047
1048    public void onRemove(ShortcutInfo item) {
1049        mItemsInvalidated = true;
1050        // If this item is being dragged from this open folder, we have already handled
1051        // the work associated with removing the item, so we don't have to do anything here.
1052        if (item == mCurrentDragInfo) return;
1053        View v = getViewForInfo(item);
1054        mContent.removeView(v);
1055        if (mState == STATE_ANIMATING) {
1056            mRearrangeOnClose = true;
1057        } else {
1058            setupContentForNumItems(getItemCount());
1059        }
1060        if (getItemCount() <= 1) {
1061            replaceFolderWithFinalItem();
1062        }
1063    }
1064
1065    private View getViewForInfo(ShortcutInfo item) {
1066        for (int j = 0; j < mContent.getCountY(); j++) {
1067            for (int i = 0; i < mContent.getCountX(); i++) {
1068                View v = mContent.getChildAt(i, j);
1069                if (v.getTag() == item) {
1070                    return v;
1071                }
1072            }
1073        }
1074        return null;
1075    }
1076
1077    public void onItemsChanged() {
1078        updateTextViewFocus();
1079    }
1080
1081    public void onTitleChanged(CharSequence title) {
1082    }
1083
1084    public ArrayList<View> getItemsInReadingOrder() {
1085        return getItemsInReadingOrder(true);
1086    }
1087
1088    public ArrayList<View> getItemsInReadingOrder(boolean includeCurrentDragItem) {
1089        if (mItemsInvalidated) {
1090            mItemsInReadingOrder.clear();
1091            for (int j = 0; j < mContent.getCountY(); j++) {
1092                for (int i = 0; i < mContent.getCountX(); i++) {
1093                    View v = mContent.getChildAt(i, j);
1094                    if (v != null) {
1095                        ShortcutInfo info = (ShortcutInfo) v.getTag();
1096                        if (info != mCurrentDragInfo || includeCurrentDragItem) {
1097                            mItemsInReadingOrder.add(v);
1098                        }
1099                    }
1100                }
1101            }
1102            mItemsInvalidated = false;
1103        }
1104        return mItemsInReadingOrder;
1105    }
1106
1107    public void getLocationInDragLayer(int[] loc) {
1108        mLauncher.getDragLayer().getLocationInDragLayer(this, loc);
1109    }
1110
1111    public void onFocusChange(View v, boolean hasFocus) {
1112        if (v == mFolderName && hasFocus) {
1113            startEditingFolderName();
1114        }
1115    }
1116}
1117