Example usage for android.view DragEvent getX

List of usage examples for android.view DragEvent getX

Introduction

In this page you can find the example usage for android.view DragEvent getX.

Prototype

public float getX() 

Source Link

Document

Gets the X coordinate of the drag point.

Usage

From source file:com.android.systemui.qs.QSDragPanel.java

@Override
public boolean onDrag(View v, DragEvent event) {
    final DragTileRecord targetTile = (DragTileRecord) getRecord(v);
    boolean originatingTileEvent = mDraggingRecord != null && v == mDraggingRecord.tileView;

    final int dragRecordIndex = mRecords.indexOf(mDraggingRecord);
    boolean dragRecordAttached = dragRecordIndex != -1;
    switch (event.getAction()) {
    case DragEvent.ACTION_DRAG_STARTED:
        if (DEBUG_DRAG) {
            Log.v(TAG, "ACTION_DRAG_STARTED on view: " + v);
        }/* w  ww . ja  v a2s.  c o  m*/

        if (originatingTileEvent) {
            if (DEBUG_DRAG) {
                Log.v(TAG, "ACTION_DRAG_STARTED on target view.");
            }
            mRestored = false;
            mQsPanelTop.setDropIcon(R.drawable.ic_qs_tile_delete_disable, R.color.qs_tile_trash_normal_tint);
        }

        break;

    case DragEvent.ACTION_DRAG_ENTERED:
        if (DEBUG_DRAG) {
            if (targetTile != null) {
                Log.v(TAG, "ACTION_DRAG_ENTERED on view with tile: " + targetTile);
            } else {
                Log.v(TAG, "ACTION_DRAG_ENTERED on view: " + v);
            }
        }
        mLocationHits = 0;
        mMovedByLocation = false;

        if (v == mQsPanelTop) {
            int icon, color;
            if (mDraggingRecord.tile instanceof EditTile) {
                // use a different warning, user can't erase this one
                icon = R.drawable.ic_qs_tile_delete_disable_avd;
                color = R.color.qs_tile_trash_delete_tint_warning;
            } else {
                icon = R.drawable.ic_qs_tile_delete_disable;
                color = R.color.qs_tile_trash_delete_tint;
            }

            mQsPanelTop.setDropIcon(icon, color);
        }

        if (!originatingTileEvent && v != getDropTarget() && targetTile != null) {
            if (DEBUG_DRAG) {
                Log.e(TAG, "entered tile " + targetTile);
            }
            if (mCurrentlyAnimating.isEmpty() && !mViewPager.isFakeDragging() && !dragRecordAttached) {
                mMovedByLocation = true;
                shiftTiles(targetTile, true);
            } else {
                if (DEBUG_DRAG) {
                    Log.w(TAG, "ignoring action enter for animating tiles and fake drags");
                }
            }
        }

        break;
    case DragEvent.ACTION_DRAG_ENDED:
        if (DEBUG_DRAG) {
            Log.v(TAG, "ACTION_DRAG_ENDED on view: " + v + "(tile: " + targetTile + "), result: "
                    + event.getResult());
        }
        if (originatingTileEvent && !event.getResult()) {
            // view pager probably ate the event
            restoreDraggingTilePosition(v, null);
        }

        break;

    case DragEvent.ACTION_DROP:
        if (DEBUG_DRAG) {
            Log.v(TAG, "ACTION_DROP, event loc: " + event.getX() + ", " + event.getY() + " + with tile: "
                    + targetTile + " and view: " + v);
        }
        mLastTouchLocationX = event.getX();
        mLastTouchLocationY = event.getY();

        if (isDropTargetEvent(event, v)) {
            if (DEBUG_DRAG) {
                Log.d(TAG, "dropping on delete target!!");
            }
            if (mDraggingRecord.tile instanceof EditTile) {
                final QSTileView editTileView = mDraggingRecord.tileView;

                mQsPanelTop.toast(R.string.quick_settings_cannot_delete_edit_tile);
                restoreDraggingTilePosition(v, new Runnable() {
                    @Override
                    public void run() {
                        // move edit tile to the back
                        final TileRecord editTile = getRecord(editTileView);
                        if (mRecords.remove(editTile)) {
                            // we depend on mHost.setTiles() placing it on the end
                            persistRecords();
                        }
                    }
                });
                break;
            } else if (mDraggingRecord.tile instanceof CustomQSTile) {
                ((CustomQSTile) mDraggingRecord.tile).setUserRemoved(true);
                final String spec = mHost.getSpec(mDraggingRecord.tile);
                restoreDraggingTilePosition(v, new Runnable() {
                    @Override
                    public void run() {
                        // it might get added back later by the app, but that's ok,
                        // we just want to reset its position after it has been removed.
                        mHost.remove(spec);
                    }
                });
            } else {
                mRestored = true;
                removeDraggingRecord();
            }
        } else {
            restoreDraggingTilePosition(v, null);
        }
        break;

    case DragEvent.ACTION_DRAG_EXITED:
        if (DEBUG_DRAG) {
            if (targetTile != null) {
                Log.v(TAG, "ACTION_DRAG_EXITED on view with tile: " + targetTile);
            } else {
                Log.v(TAG, "ACTION_DRAG_EXITED on view: " + v);
            }
        }

        if (v == mQsPanelTop) {
            mQsPanelTop.setDropIcon(R.drawable.ic_qs_tile_delete_disable, R.color.qs_tile_trash_normal_tint);
        }

        if (originatingTileEvent && mCurrentlyAnimating.isEmpty() && !mViewPager.isFakeDragging()
                && dragRecordAttached && mLastLeftShift == -1) {

            if (DEBUG_DRAG) {
                Log.v(TAG, "target: " + targetTile + ", hit mLastRightShift: " + mLastRightShift
                        + ", mLastLeftShift: " + mLastLeftShift + ", dragRecordIndex: " + dragRecordIndex);
            }

            // move tiles back
            shiftTiles(mDraggingRecord, false);
            break;
        }
        // fall through so exit events can trigger a left shift
    case DragEvent.ACTION_DRAG_LOCATION:
        mLastTouchLocationX = event.getX();
        mLastTouchLocationY = event.getY();

        // do nothing if we're animating tiles
        if (mCurrentlyAnimating.isEmpty() && !mViewPager.isFakeDragging()) {
            if (v == mViewPager) {
                // do we need to change pages?
                int x = (int) event.getX();
                int width = mViewPager.getWidth();
                int scrollPadding = (int) (width * QSViewPager.SCROLL_PERCENT);
                if (x < scrollPadding) {
                    if (mViewPager.canScrollHorizontally(-1)) {
                        mViewPager.animatePagerTransition(false);
                        return true;
                    }
                } else if (x > width - scrollPadding) {
                    if (mViewPager.canScrollHorizontally(1)) {
                        mViewPager.animatePagerTransition(true);
                        return true;
                    }
                }
            }
            if (DEBUG_DRAG) {
                Log.v(TAG, "location hit:// target: " + targetTile + ", hit mLastRightShift: " + mLastRightShift
                        + ", mLastLeftShift: " + mLastLeftShift + ", dragRecordIndex: " + dragRecordIndex
                        + ", originatingTileEvent: " + originatingTileEvent + ", mLocationHits: "
                        + mLocationHits + ", mMovedByLocation: " + mMovedByLocation);
            }

            if (v != getDropTarget() && targetTile != null && !dragRecordAttached) {
                // dragging around on another tile
                if (mLocationHits++ == 30) {
                    if (DEBUG_DRAG) {
                        Log.w(TAG, "shifting right due to location hits.");
                    }
                    // add dragging tile to current page
                    shiftTiles(targetTile, true);
                    mMovedByLocation = true;
                } else {
                    mLocationHits++;
                }
            } else if (mLastRightShift != -1 // right has shifted recently
                    && mLastLeftShift == -1 // -1 means its attached
                    && dragRecordIndex == mLastRightShift && !originatingTileEvent
                    && !mMovedByLocation /* helps avoid continuous shifting */) {
                // check if the location is on another tile/view
                // that is not the last drag index, shift back left to revert back and
                // potentially get ready for shifting right
                if (DEBUG_DRAG) {
                    Log.w(TAG, "conditions met to reverse!!!! shifting left. <<<<<<<");
                }
                shiftTiles((DragTileRecord) mRecords.get(mLastRightShift), false);
                mMovedByLocation = true;
            }

        } else {
            if (DEBUG_DRAG) {
                Log.i(TAG, "ignoring location event because things are animating, size: "
                        + mCurrentlyAnimating.size());
            }
        }
        break;

    default:
        Log.w(TAG, "unhandled event");
        return false;
    }
    return true;
}

From source file:app.umitems.greenclock.widget.sgv.StaggeredGridView.java

@Override
public boolean dispatchDragEvent(DragEvent event) {
    if (!isDragReorderingSupported()) {
        // If the consumer of this StaggeredGridView has not registered a ReorderListener,
        // don't bother handling drag events.
        return super.dispatchDragEvent(event);
    }/*w w w. j ava2  s.co  m*/

    switch (event.getAction()) {
    case DragEvent.ACTION_DRAG_STARTED:
        // Per bug 7071594, we won't be able to catch this event in onDragEvent,
        // so we'll handle the event as it is being dispatched on the way down.
        if (mReorderHelper.hasReorderListener() && mIsDragReorderingEnabled) {
            final View child = getChildAtCoordinate(mTouchDownForDragStartX, mTouchDownForDragStartY);
            if (child != null) {
                // Child can be null if the touch point is not on a child view, but is
                // still within the bounds of this StaggeredGridView (i.e., margins
                // between cells).
                startDragging(child, mTouchDownForDragStartX, mTouchDownForDragStartY);
                // We must return true in order to continue getting future
                // {@link DragEvent}s.
                return true;
            }
        }
        // Be sure to return a value here instead of calling super.dispatchDragEvent()
        // which will unnecessarily dispatch to all the children (since the
        // {@link StaggeredGridView} handles all drag events for our purposes)
        return false;

    case DragEvent.ACTION_DROP:
    case DragEvent.ACTION_DRAG_ENDED:
        if (mDragState == ReorderUtils.DRAG_STATE_DRAGGING) {
            handleDrop((int) event.getX(), (int) event.getY());
        }

        // Return early here to avoid calling super.dispatchDragEvent() which dispatches to
        // children (since this view already can handle all drag events). The super call
        // can also cause a NPE if the view hierarchy changed in the middle of a drag
        // and the {@link DragEvent} gets nulled out. This is a workaround for
        // a framework bug: 8298439.
        // Since the {@link StaggeredGridView} handles all drag events for our purposes,
        // just manually fire the drag event to ourselves.
        return onDragEvent(event);
    }

    // In all other cases, default to the superclass implementation. We need this so that
    // the drag/drop framework will fire off {@link #onDragEvent(DragEvent ev)} calls to us.
    return super.dispatchDragEvent(event);
}