Example usage for android.content ClipData addItem

List of usage examples for android.content ClipData addItem

Introduction

In this page you can find the example usage for android.content ClipData addItem.

Prototype

public void addItem(Item item) 

Source Link

Document

Add a new Item to the overall ClipData container.

Usage

From source file:Main.java

/**
 * Restore {@link ClipData} from String/*from  w  w w. jav a2  s. c o  m*/
 * @param s
 * @return ClipData instance
 */
public static ClipData clipDataFromString(CharSequence s) {
    ClipData cd = null;
    if (s != null) {
        StringBuilder sb = new StringBuilder(s);
        CharSequence prefix = extract(sb);
        if (prefix != null) {
            ClipDescription desc = clipDescriptionFromString(sb);
            int n = extractInt(sb);
            ClipData.Item[] items = new ClipData.Item[n];
            for (int i = 0; i < n; i++) {
                items[i] = itemFromString(sb);
            }
            if (n > 0) {
                cd = new ClipData(desc, items[0]);
            }
            for (int i = 1; i < n; i++) {
                cd.addItem(items[i]);
            }
        }
    }
    return cd;
}

From source file:com.home.young.filepicker.AbstractFilePickerActivity.java

@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
@Override/*  w ww  .j  a v a 2 s .com*/
public void onFilesPicked(@NonNull final List<Uri> files) {
    Intent i = new Intent();
    i.putExtra(EXTRA_ALLOW_MULTIPLE, true);

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
        ClipData clip = null;
        for (Uri file : files) {
            if (clip == null) {
                clip = new ClipData("Paths", new String[] {}, new ClipData.Item(file));
            } else {
                clip.addItem(new ClipData.Item(file));
            }
        }
        i.setClipData(clip);
    } else {
        ArrayList<String> paths = new ArrayList<>();
        for (Uri file : files) {
            paths.add(file.toString());
        }
        i.putStringArrayListExtra(EXTRA_PATHS, paths);
    }

    setResult(Activity.RESULT_OK, i);
    finish();
}

From source file:cn.tycoon.lighttrans.fileManager.AbstractFilePickerActivity.java

@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
@Override//from w ww.ja va 2s.  c  o  m
public void onFilesPicked(final List<Uri> files) {
    Intent i = new Intent();
    i.putExtra(EXTRA_ALLOW_MULTIPLE, true);

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
        ClipData clip = null;
        for (Uri file : files) {
            if (clip == null) {
                clip = new ClipData("Paths", new String[] {}, new ClipData.Item(file));
            } else {
                clip.addItem(new ClipData.Item(file));
            }
        }
        i.setClipData(clip);
    } else {
        ArrayList<String> paths = new ArrayList<>();
        for (Uri file : files) {
            paths.add(file.toString());
        }
        i.putStringArrayListExtra(EXTRA_PATHS, paths);
    }

    setResult(Activity.RESULT_OK, i);
    finish();
}

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  a2 s  .c o  m*/
        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.launcher.silverfish.HomeScreenFragment.java

@SuppressWarnings("deprecation")
private void updateShortcuts() {
    int count = appsList.size();
    int size = (int) Math.ceil(Math.sqrt(count));
    shortcutLayout.removeAllViews();/*  ww  w  .j  av a  2  s .co  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 ww  w. ja va2 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.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;

            }
        });
    }
}

From source file:com.android.messaging.ui.UIIntentsImpl.java

/**
 * Get an intent which takes you to a conversation
 *///  w ww .  jav  a 2s  . c o m
private Intent getConversationActivityIntent(final Context context, final String conversationId,
        final MessageData draft, final boolean withCustomTransition) {
    final Intent intent = new Intent(context, ConversationActivity.class);

    // Always try to reuse the same ConversationActivity in the current task so that we don't
    // have two conversation activities in the back stack.
    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

    // Otherwise we're starting a new conversation
    if (conversationId != null) {
        intent.putExtra(UI_INTENT_EXTRA_CONVERSATION_ID, conversationId);
    }
    if (draft != null) {
        intent.putExtra(UI_INTENT_EXTRA_DRAFT_DATA, draft);

        // If draft attachments came from an external content provider via a share intent, we
        // need to propagate the URI permissions through to ConversationActivity. This requires
        // putting the URIs into the ClipData (setData also works, but accepts only one URI).
        ClipData clipData = null;
        for (final MessagePartData partData : draft.getParts()) {
            if (partData.isAttachment()) {
                final Uri uri = partData.getContentUri();
                if (clipData == null) {
                    clipData = ClipData.newRawUri("Attachments", uri);
                } else {
                    clipData.addItem(new ClipData.Item(uri));
                }
            }
        }
        if (clipData != null) {
            intent.setClipData(clipData);
            intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
        }
    }
    if (withCustomTransition) {
        intent.putExtra(UI_INTENT_EXTRA_WITH_CUSTOM_TRANSITION, true);
    }

    if (!(context instanceof Activity)) {
        // If the caller supplies an application context, and not an activity context, we must
        // include this flag
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    }
    return intent;
}

From source file:cn.edu.wyu.documentviewer.DocumentsActivity.java

private void onFinished(Uri... uris) {
    Log.d(TAG, "onFinished() " + Arrays.toString(uris));

    final Intent intent = new Intent();
    if (uris.length == 1) {
        intent.setData(uris[0]);//  w w  w.  j a va2 s  . c  o  m
    } else if (uris.length > 1) {
        final ClipData clipData = new ClipData(null, mState.acceptMimes, new ClipData.Item(uris[0]));
        for (int i = 1; i < uris.length; i++) {
            clipData.addItem(new ClipData.Item(uris[i]));
        }
        intent.setClipData(clipData);
    }

    if (mState.action == ACTION_GET_CONTENT) {
        intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
    } else {
        intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION
                | Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION);
    }

    setResult(Activity.RESULT_OK, intent);
    finish();
}

From source file:dev.dworks.apps.anexplorer.DocumentsActivity.java

private void onFinished(Uri... uris) {
    Log.d(TAG, "onFinished() " + Arrays.toString(uris));

    final Intent intent = new Intent();
    if (uris.length == 1) {
        intent.setData(uris[0]);/* ww w.j a va 2 s . c o  m*/
    } else if (uris.length > 1) {
        final ClipData clipData = new ClipData(null, mState.acceptMimes, new ClipData.Item(uris[0]));
        for (int i = 1; i < uris.length; i++) {
            clipData.addItem(new ClipData.Item(uris[i]));
        }
        if (Utils.hasJellyBean()) {
            intent.setClipData(clipData);
        } else {
            intent.setData(uris[0]);
        }
    }

    if (mState.action == ACTION_GET_CONTENT || mState.action == ACTION_BROWSE) {
        intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
    } else {
        intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
    }

    setResult(Activity.RESULT_OK, intent);
    finish();
}

From source file:com.tct.mail.browse.ConversationItemView.java

/**
 * Begin drag mode. Keep the conversation selected (NOT toggle selection) and start drag.
 *///from w w w. j av a2 s . c  o  m
private boolean beginDragMode() {
    if (mLastTouchX < 0 || mLastTouchY < 0 || mSelectedConversationSet == null) {
        return false;
    }
    // If this is already checked, don't bother unchecking it!
    if (!mSelected) {
        toggleSelectedState();
    }

    // Clip data has form: [conversations_uri, conversationId1,
    // maxMessageId1, label1, conversationId2, maxMessageId2, label2, ...]
    final int count = mSelectedConversationSet.size();
    String description = Utils.formatPlural(mContext, R.plurals.move_conversation, count);

    final ClipData data = ClipData.newUri(mContext.getContentResolver(), description,
            Conversation.MOVE_CONVERSATIONS_URI);
    for (Conversation conversation : mSelectedConversationSet.values()) {
        data.addItem(new Item(String.valueOf(conversation.position)));
    }
    // Protect against non-existent views: only happens for monkeys
    final int width = this.getWidth();
    final int height = this.getHeight();
    final boolean isDimensionNegative = (width < 0) || (height < 0);
    if (isDimensionNegative) {
        LogUtils.e(LOG_TAG, "ConversationItemView: dimension is negative: " + "width=%d, height=%d", width,
                height);
        return false;
    }
    mActivity.startDragMode();
    // Start drag mode
    startDrag(data, new ShadowBuilder(this, count, mLastTouchX, mLastTouchY), null, 0);

    return true;
}