Example usage for android.view View startDragAndDrop

List of usage examples for android.view View startDragAndDrop

Introduction

In this page you can find the example usage for android.view View startDragAndDrop.

Prototype

public final boolean startDragAndDrop(ClipData data, DragShadowBuilder shadowBuilder, Object myLocalState,
        int flags) 

Source Link

Document

Starts a drag and drop operation.

Usage

From source file:com.launcher.silverfish.AppDrawerTabFragment.java

@SuppressWarnings("deprecation")
private void setClickListeners(View view, final String appName, final int appIndex) {

    // Start a drag action when icon is long clicked
    view.setOnLongClickListener(new View.OnLongClickListener() {
        @Override/*w ww  . ja  v a 2s  . c  om*/
        public boolean onLongClick(View view) {

            // Add data to the clipboard
            String[] mime_type = { ClipDescription.MIMETYPE_TEXT_PLAIN };
            ClipData data = new ClipData(Constants.DRAG_APP_MOVE, mime_type, new ClipData.Item(appName));
            data.addItem(new ClipData.Item(Integer.toString(appIndex)));
            data.addItem(new ClipData.Item(getTag()));

            // The drag shadow is simply the app's  icon
            View.DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(
                    view.findViewById(R.id.item_app_icon));

            // "This method was deprecated in API level 24. Use startDragAndDrop()
            // for newer platform versions."
            if (Build.VERSION.SDK_INT < 24) {
                view.startDrag(data, shadowBuilder, view, 0);
            } else {
                view.startDragAndDrop(data, shadowBuilder, view, 0);
            }
            return true;

        }
    });

    // Start the app activity when icon is clicked.
    view.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent i = mPacMan.getLaunchIntentForPackage(appName);
            startActivity(i);
        }
    });
}

From source file:com.example.android.dragsource.DragSourceFragment.java

private void setUpDraggableImage(ImageView imageView, final Uri imageUri) {

    // Set up a listener that starts the drag and drop event with flags and extra data.
    DragStartHelper.OnDragStartListener listener = new DragStartHelper.OnDragStartListener() {
        @Override//from  w  w  w .j a va 2s . co m
        public boolean onDragStart(View view, final DragStartHelper helper) {
            Log.d(TAG, "Drag start event received from helper.");

            // Use a DragShadowBuilder
            View.DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(view) {
                @Override
                public void onProvideShadowMetrics(Point shadowSize, Point shadowTouchPoint) {
                    super.onProvideShadowMetrics(shadowSize, shadowTouchPoint);
                    // Notify the DragStartHelper of point where the view was touched.
                    helper.getTouchPosition(shadowTouchPoint);
                    Log.d(TAG, "View was touched at: " + shadowTouchPoint);
                }
            };

            // Set up the flags for the drag event.
            // Enable drag and drop across apps (global)
            // and require read permissions for this URI.
            int flags = View.DRAG_FLAG_GLOBAL | View.DRAG_FLAG_GLOBAL_URI_READ;

            // Add an optional clip description that that contains an extra String that is
            // read out by the target app.
            final ClipDescription clipDescription = new ClipDescription("",
                    new String[] { getContext().getContentResolver().getType(imageUri) });
            // Extras are stored within a PersistableBundle.
            PersistableBundle extras = new PersistableBundle(1);
            // Add a String that the target app will display.
            extras.putString(EXTRA_IMAGE_INFO, "Drag Started at " + new Date());
            clipDescription.setExtras(extras);

            // The ClipData object describes the object that is being dragged and dropped.
            final ClipData clipData = new ClipData(clipDescription, new ClipData.Item(imageUri));

            Log.d(TAG, "Created ClipDescription. Starting drag and drop.");
            // Start the drag and drop event.
            return view.startDragAndDrop(clipData, shadowBuilder, null, flags);

        }

    };

    // Use the DragStartHelper to detect drag and drop events and use the OnDragStartListener
    // defined above to start the event when it has been detected.
    DragStartHelper helper = new DragStartHelper(imageView, listener);
    helper.attach();
    Log.d(TAG, "DragStartHelper attached to view.");
}

From source file:com.launcher.silverfish.HomeScreenFragment.java

@SuppressWarnings("deprecation")
private void updateShortcuts() {
    int count = appsList.size();
    int size = (int) Math.ceil(Math.sqrt(count));
    shortcutLayout.removeAllViews();//from w ww . j  a v a2 s  .c o  m

    if (size == 0) {
        size = 1;
    }

    // Redraw the layout
    shortcutLayout.setSize(size);
    shortcutLayout.requestLayout();
    shortcutLayout.invalidate();

    for (int i = 0; i < appsList.size(); i++) {
        final AppDetail app = appsList.get(i);
        View convertView = getActivity().getLayoutInflater().inflate(R.layout.shortcut_item, null);

        // load the app icon in an async task
        ImageView im = (ImageView) convertView.findViewById(R.id.item_app_icon);
        Utils.loadAppIconAsync(mPacMan, app.name.toString(), im);

        TextView tv = (TextView) convertView.findViewById(R.id.item_app_label);
        tv.setText(app.label);
        shortcutLayout.addView(convertView);

        convertView.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent event) {
                switch (MotionEventCompat.getActionMasked(event)) {
                case MotionEvent.ACTION_DOWN:
                    updateTouchDown(event);
                    break;

                case MotionEvent.ACTION_MOVE:
                    tryConsumeSwipe(event);
                    break;

                case MotionEvent.ACTION_UP:
                    // We only want to launch the activity if the touch was not consumed yet!
                    if (!touchConsumed) {
                        Intent i = mPacMan.getLaunchIntentForPackage(app.name.toString());
                        startActivity(i);
                    }
                    break;
                }

                return touchConsumed;
            }
        });

        // start a drag when an app has been long clicked
        final long appId = app.id;
        final int appIndex = i;
        convertView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View view) {
                String[] mime_types = { ClipDescription.MIMETYPE_TEXT_PLAIN };
                ClipData data = new ClipData(Constants.DRAG_SHORTCUT_REMOVAL, mime_types,
                        new ClipData.Item(Long.toString(appId)));

                data.addItem(new ClipData.Item(Integer.toString(appIndex)));

                View.DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(
                        view.findViewById(R.id.item_app_icon));

                // "This method was deprecated in API level 24. Use startDragAndDrop()
                // for newer platform versions."
                if (Build.VERSION.SDK_INT < 24) {
                    view.startDrag(data, shadowBuilder, view, 0);
                } else {
                    view.startDragAndDrop(data, shadowBuilder, view, 0);
                }

                // Show removal indicator
                FrameLayout rem_ind = (FrameLayout) rootView.findViewById(R.id.remove_indicator);
                rem_ind.setVisibility(View.VISIBLE);
                AlphaAnimation animation = new AlphaAnimation(0.0f, 1.0f);
                animation.setDuration(500);
                rem_ind.startAnimation(animation);
                return true;

            }
        });
    }
}

From source file:com.launcher.silverfish.launcher.homescreen.HomeScreenFragment.java

@SuppressWarnings("deprecation")
private void updateShortcuts() {
    int count = appsList.size();
    int size = (int) Math.ceil(Math.sqrt(count));
    shortcutLayout.removeAllViews();/*from   w w w .j  a  v  a2s  . c  o  m*/

    if (size == 0) {
        size = 1;
    }

    // Redraw the layout
    shortcutLayout.setSize(size);
    shortcutLayout.requestLayout();
    shortcutLayout.invalidate();

    for (int i = 0; i < appsList.size(); i++) {
        final AppDetail app = appsList.get(i);
        View convertView = getActivity().getLayoutInflater().inflate(R.layout.shortcut_item, null);

        // load the app icon in an async task
        ImageView im = (ImageView) convertView.findViewById(R.id.item_app_icon);
        Utils.loadAppIconAsync(mPacMan, app.packageName.toString(), im);

        TextView tv = (TextView) convertView.findViewById(R.id.item_app_label);
        tv.setText(app.label);
        shortcutLayout.addView(convertView);

        convertView.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent event) {
                switch (MotionEventCompat.getActionMasked(event)) {
                case MotionEvent.ACTION_DOWN:
                    updateTouchDown(event);
                    break;

                case MotionEvent.ACTION_MOVE:
                    tryConsumeSwipe(event);
                    break;

                case MotionEvent.ACTION_UP:
                    // We only want to launch the activity if the touch was not consumed yet!
                    if (!touchConsumed) {
                        Intent i = mPacMan.getLaunchIntentForPackage(app.packageName.toString());
                        if (i != null) {
                            // Sanity check (application may have been uninstalled)
                            // TODO Remove it from the database
                            startActivity(i);
                        } else {
                            Toast.makeText(getContext(), R.string.application_not_installed, Toast.LENGTH_SHORT)
                                    .show();
                        }
                    }
                    break;
                }

                return touchConsumed;
            }
        });

        // start a drag when an app has been long clicked
        final long appId = app.id;
        final int appIndex = i;
        convertView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View view) {
                String[] mime_types = { ClipDescription.MIMETYPE_TEXT_PLAIN };
                ClipData data = new ClipData(Constants.DRAG_SHORTCUT_REMOVAL, mime_types,
                        new ClipData.Item(Long.toString(appId)));

                data.addItem(new ClipData.Item(Integer.toString(appIndex)));

                View.DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(
                        view.findViewById(R.id.item_app_icon));

                // "This method was deprecated in API level 24. Use startDragAndDrop()
                // for newer platform versions."
                if (Build.VERSION.SDK_INT < 24) {
                    view.startDrag(data, shadowBuilder, view, 0);
                } else {
                    view.startDragAndDrop(data, shadowBuilder, view, 0);
                }

                // Show removal indicator
                FrameLayout rem_ind = (FrameLayout) rootView.findViewById(R.id.remove_indicator);
                rem_ind.setVisibility(View.VISIBLE);
                AlphaAnimation animation = new AlphaAnimation(0.0f, 1.0f);
                animation.setDuration(500);
                rem_ind.startAnimation(animation);
                return true;

            }
        });
    }
}