Skip to content

Commit

Permalink
Support ACTION_SEND extras when handling ACTION_SENDTO
Browse files Browse the repository at this point in the history
Too many applications get this wrong and rely on the undocumented
behavior of the AOSP Email app and Gmail.

See https://code.google.com/p/android/issues/detail?id=30190
  • Loading branch information
cketti committed May 6, 2012
1 parent 5e3dbdc commit dc476eb
Showing 1 changed file with 43 additions and 25 deletions.
68 changes: 43 additions & 25 deletions src/com/fsck/k9/activity/MessageCompose.java
Expand Up @@ -759,22 +759,27 @@ private void initFromIntent(final Intent intent) {
}

/*
* Note: According to the documenation ACTION_VIEW and ACTION_SENDTO
* don't accept EXTRA_* parameters. Contrary to the AOSP Email application
* we don't accept those EXTRAs.
* Dear developer, if your application is using those EXTRAs you're doing
* it wrong! So go fix your program or get AOSP to change the documentation.
* Note: According to the documenation ACTION_VIEW and ACTION_SENDTO don't accept
* EXTRA_* parameters.
* And previously we didn't process these EXTRAs. But it looks like nobody bothers to
* read the official documentation and just copies wrong sample code that happens to
* work with the AOSP Email application. And because even big players get this wrong,
* we're now finally giving in and read the EXTRAs for ACTION_SENDTO (below).
*/
}
else if (Intent.ACTION_SEND.equals(action) || Intent.ACTION_SEND_MULTIPLE.equals(action)) {

if (Intent.ACTION_SEND.equals(action) || Intent.ACTION_SEND_MULTIPLE.equals(action) ||
Intent.ACTION_SENDTO.equals(action)) {

/*
* Note: Here we allow a slight deviation from the documentated behavior.
* EXTRA_TEXT is used as message body (if available) regardless of the MIME
* type of the intent. In addition one or multiple attachments can be added
* using EXTRA_STREAM.
*/
CharSequence text = intent.getCharSequenceExtra(Intent.EXTRA_TEXT);
if (text != null) {
// Only use EXTRA_TEXT if the body hasn't already been set by the mailto URI
if (text != null && mMessageContentView.getText().length() == 0) {
mMessageContentView.setText(text);
}

Expand All @@ -797,7 +802,8 @@ else if (Intent.ACTION_SEND.equals(action) || Intent.ACTION_SEND_MULTIPLE.equals
}

String subject = intent.getStringExtra(Intent.EXTRA_SUBJECT);
if (subject != null) {
// Only use EXTRA_SUBJECT if the subject hasn't already been set by the mailto URI
if (subject != null && mSubjectView.getText().length() == 0) {
mSubjectView.setText(subject);
}

Expand All @@ -806,16 +812,16 @@ else if (Intent.ACTION_SEND.equals(action) || Intent.ACTION_SEND_MULTIPLE.equals
String[] extraBcc = intent.getStringArrayExtra(Intent.EXTRA_BCC);

if (extraEmail != null) {
setRecipients(mToView, Arrays.asList(extraEmail));
addRecipients(mToView, Arrays.asList(extraEmail));
}

boolean ccOrBcc = false;
if (extraCc != null) {
ccOrBcc |= setRecipients(mCcView, Arrays.asList(extraCc));
ccOrBcc |= addRecipients(mCcView, Arrays.asList(extraCc));
}

if (extraBcc != null) {
ccOrBcc |= setRecipients(mBccView, Arrays.asList(extraBcc));
ccOrBcc |= addRecipients(mBccView, Arrays.asList(extraBcc));
}

if (ccOrBcc) {
Expand All @@ -825,19 +831,31 @@ else if (Intent.ACTION_SEND.equals(action) || Intent.ACTION_SEND_MULTIPLE.equals
}
}

private boolean setRecipients(TextView view, List<String> recipients) {
boolean recipientAdded = false;
if (recipients != null) {
StringBuilder addressList = new StringBuilder();
for (String recipient : recipients) {
addressList.append(recipient);
addressList.append(", ");
recipientAdded = true;
}
view.setText(addressList);
private boolean addRecipients(TextView view, List<String> recipients) {
if (recipients == null || recipients.size() == 0) {
return false;
}

StringBuilder addressList = new StringBuilder();

// Read current contents of the TextView
String text = view.getText().toString();
addressList.append(text);

// Add comma if necessary
if (text.length() != 0 && !(text.endsWith(", ") || text.endsWith(","))) {
addressList.append(", ");
}

// Add recipients
for (String recipient : recipients) {
addressList.append(recipient);
addressList.append(", ");
}

return recipientAdded;
view.setText(addressList);

return true;
}

private void initializeCrypto() {
Expand Down Expand Up @@ -3003,13 +3021,13 @@ private void initializeFromMailto(Uri mailtoUri) {
to = new ArrayList<String>(to);
to.add(0, recipient);
}
setRecipients(mToView, to);
addRecipients(mToView, to);

// Read carbon copy recipients from the "cc" parameter.
boolean ccOrBcc = setRecipients(mCcView, uri.getQueryParameters("cc"));
boolean ccOrBcc = addRecipients(mCcView, uri.getQueryParameters("cc"));

// Read blind carbon copy recipients from the "bcc" parameter.
ccOrBcc |= setRecipients(mBccView, uri.getQueryParameters("bcc"));
ccOrBcc |= addRecipients(mBccView, uri.getQueryParameters("bcc"));

if (ccOrBcc) {
// Display CC and BCC text fields if CC or BCC recipients were set by the intent.
Expand Down

0 comments on commit dc476eb

Please sign in to comment.