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:com.android.mail.compose.ComposeActivity.java

protected void sendOrSave(final boolean save, final boolean showToast) {
    // Check if user is a monkey. Monkeys can compose and hit send
    // button but are not allowed to send anything off the device.
    if (ActivityManager.isUserAMonkey()) {
        return;//w ww .  j  a va  2  s  .c  om
    }

    final SendOrSaveCallback callback = new SendOrSaveCallback() {
        @Override
        public void initializeSendOrSave() {
            final Intent i = new Intent(ComposeActivity.this, EmptyService.class);

            // API 16+ allows for setClipData. For pre-16 we are going to open the fds
            // on the main thread.
            if (Utils.isRunningJellybeanOrLater()) {
                // Grant the READ permission for the attachments to the service so that
                // as long as the service stays alive we won't hit PermissionExceptions.
                final ClipDescription desc = new ClipDescription("attachment_uris",
                        new String[] { ClipDescription.MIMETYPE_TEXT_URILIST });
                ClipData clipData = null;
                for (Attachment a : mAttachmentsView.getAttachments()) {
                    if (a != null && !Utils.isEmpty(a.contentUri)) {
                        final ClipData.Item uriItem = new ClipData.Item(a.contentUri);
                        if (clipData == null) {
                            clipData = new ClipData(desc, uriItem);
                        } else {
                            clipData.addItem(uriItem);
                        }
                    }
                }
                i.setClipData(clipData);
                i.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
            }

            synchronized (PENDING_SEND_OR_SAVE_TASKS_NUM) {
                if (PENDING_SEND_OR_SAVE_TASKS_NUM.getAndAdd(1) == 0) {
                    // Start service so we won't be killed if this app is
                    // put in the background.
                    startService(i);
                }
            }
            if (sTestSendOrSaveCallback != null) {
                sTestSendOrSaveCallback.initializeSendOrSave();
            }
        }

        @Override
        public void notifyMessageIdAllocated(SendOrSaveMessage sendOrSaveMessage, Message message) {
            synchronized (mDraftLock) {
                mDraftId = message.id;
                mDraft = message;
                if (sRequestMessageIdMap != null) {
                    sRequestMessageIdMap.put(sendOrSaveMessage.mRequestId, mDraftId);
                }
                // Cache request message map, in case the process is killed
                saveRequestMap();
            }
            if (sTestSendOrSaveCallback != null) {
                sTestSendOrSaveCallback.notifyMessageIdAllocated(sendOrSaveMessage, message);
            }
        }

        @Override
        public long getMessageId() {
            synchronized (mDraftLock) {
                return mDraftId;
            }
        }

        @Override
        public void sendOrSaveFinished(SendOrSaveMessage message, boolean success) {
            // Update the last sent from account.
            if (mAccount != null) {
                MailAppProvider.getInstance().setLastSentFromAccount(mAccount.uri.toString());
            }
            if (success) {
                // Successfully sent or saved so reset change markers
                discardChanges();
            } else {
                // A failure happened with saving/sending the draft
                // TODO(pwestbro): add a better string that should be used
                // when failing to send or save
                Toast.makeText(ComposeActivity.this, R.string.send_failed, Toast.LENGTH_SHORT).show();
            }

            synchronized (PENDING_SEND_OR_SAVE_TASKS_NUM) {
                if (PENDING_SEND_OR_SAVE_TASKS_NUM.addAndGet(-1) == 0) {
                    // Stop service so we can be killed.
                    stopService(new Intent(ComposeActivity.this, EmptyService.class));
                }
            }
            if (sTestSendOrSaveCallback != null) {
                sTestSendOrSaveCallback.sendOrSaveFinished(message, success);
            }
        }
    };
    setAccount(mReplyFromAccount.account);

    final Spanned body = removeComposingSpans(mBodyView.getText());
    callback.initializeSendOrSave();

    // For pre-JB we need to open the fds on the main thread
    final Bundle attachmentFds;
    if (!Utils.isRunningJellybeanOrLater()) {
        attachmentFds = initializeAttachmentFds(this, mAttachmentsView.getAttachments());
    } else {
        attachmentFds = null;
    }

    // Generate a unique message id for this request
    mRequestId = sRandom.nextInt();
    SEND_SAVE_TASK_HANDLER.post(new Runnable() {
        @Override
        public void run() {
            final Message msg = createMessage(mReplyFromAccount, mRefMessage, getMode(), body);
            sendOrSaveInternal(ComposeActivity.this, mRequestId, mReplyFromAccount, mDraftAccount, msg,
                    mRefMessage, mQuotedTextView.getQuotedTextIfIncluded(), callback, save, mComposeMode,
                    mExtraValues, attachmentFds);
        }
    });

    // Don't display the toast if the user is just changing the orientation,
    // but we still need to save the draft to the cursor because this is how we restore
    // the attachments when the configuration change completes.
    if (showToast && (getChangingConfigurations() & ActivityInfo.CONFIG_ORIENTATION) == 0) {
        Toast.makeText(this, save ? R.string.message_saved : R.string.sending_message, Toast.LENGTH_LONG)
                .show();
    }

    // Need to update variables here because the send or save completes
    // asynchronously even though the toast shows right away.
    discardChanges();
    updateSaveUi();

    // If we are sending, finish the activity
    if (!save) {
        finish();
    }
}