Search in sources :

Example 71 with Address

use of com.fsck.k9.mail.Address in project k-9 by k9mail.

the class MessageCompose method initFromIntent.

/**
 * Handle external intents that trigger the message compose activity.
 *
 * <p>
 * Supported external intents:
 * <ul>
 *   <li>{@link Intent#ACTION_VIEW}</li>
 *   <li>{@link Intent#ACTION_SENDTO}</li>
 *   <li>{@link Intent#ACTION_SEND}</li>
 *   <li>{@link Intent#ACTION_SEND_MULTIPLE}</li>
 * </ul>
 * </p>
 *
 * @param intent
 *         The (external) intent that started the activity.
 *
 * @return {@code true}, if this activity was started by an external intent. {@code false},
 *         otherwise.
 */
private boolean initFromIntent(final Intent intent) {
    boolean startedByExternalIntent = false;
    final String action = intent.getAction();
    if (Intent.ACTION_VIEW.equals(action) || Intent.ACTION_SENDTO.equals(action)) {
        /*
             * Someone has clicked a mailto: link. The address is in the URI.
             */
        if (intent.getData() != null) {
            Uri uri = intent.getData();
            if (MailTo.isMailTo(uri)) {
                MailTo mailTo = MailTo.parse(uri);
                initializeFromMailto(mailTo);
            }
        }
    /*
             * Note: According to the documentation 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 those actions (below).
             */
    }
    if (Intent.ACTION_SEND.equals(action) || Intent.ACTION_SEND_MULTIPLE.equals(action) || Intent.ACTION_SENDTO.equals(action) || Intent.ACTION_VIEW.equals(action)) {
        startedByExternalIntent = true;
        /*
             * Note: Here we allow a slight deviation from the documented 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);
        // Only use EXTRA_TEXT if the body hasn't already been set by the mailto URI
        if (text != null && messageContentView.getText().length() == 0) {
            messageContentView.setText(CrLfConverter.toLf(text));
        }
        String type = intent.getType();
        if (Intent.ACTION_SEND.equals(action)) {
            Uri stream = intent.getParcelableExtra(Intent.EXTRA_STREAM);
            if (stream != null) {
                attachmentPresenter.addExternalAttachment(stream, type);
            }
        } else {
            List<Parcelable> list = intent.getParcelableArrayListExtra(Intent.EXTRA_STREAM);
            if (list != null) {
                for (Parcelable parcelable : list) {
                    Uri stream = (Uri) parcelable;
                    if (stream != null) {
                        attachmentPresenter.addExternalAttachment(stream, type);
                    }
                }
            }
        }
        String subject = intent.getStringExtra(Intent.EXTRA_SUBJECT);
        // Only use EXTRA_SUBJECT if the subject hasn't already been set by the mailto URI
        if (subject != null && subjectView.getText().length() == 0) {
            subjectView.setText(subject);
        }
        recipientPresenter.initFromSendOrViewIntent(intent);
    }
    if (ACTION_AUTOCRYPT_PEER.equals(action)) {
        String trustId = intent.getStringExtra(OpenPgpApi.EXTRA_AUTOCRYPT_PEER_ID);
        if (trustId != null) {
            recipientPresenter.initFromTrustIdAction(trustId);
            startedByExternalIntent = true;
        }
    }
    return startedByExternalIntent;
}
Also used : Parcelable(android.os.Parcelable) Uri(android.net.Uri) MailTo(com.fsck.k9.helper.MailTo)

Example 72 with Address

use of com.fsck.k9.mail.Address in project k-9 by k9mail.

the class RecipientLoader method fillCryptoStatusData.

private void fillCryptoStatusData(Map<String, Recipient> recipientMap) {
    List<String> recipientList = new ArrayList<>(recipientMap.keySet());
    String[] recipientAddresses = recipientList.toArray(new String[recipientList.size()]);
    Cursor cursor;
    Uri queryUri = Uri.parse("content://" + cryptoProvider + ".provider.exported/autocrypt_status");
    try {
        cursor = contentResolver.query(queryUri, PROJECTION_CRYPTO_STATUS, null, recipientAddresses, null);
    } catch (SecurityException e) {
        // TODO escalate error to crypto status?
        return;
    }
    initializeCryptoStatusForAllRecipients(recipientMap);
    if (cursor == null) {
        return;
    }
    while (cursor.moveToNext()) {
        String email = cursor.getString(INDEX_EMAIL_ADDRESS);
        int uidStatus = cursor.getInt(INDEX_EMAIL_STATUS);
        int autocryptStatus = cursor.getInt(INDEX_AUTOCRYPT_STATUS);
        int effectiveStatus = uidStatus > autocryptStatus ? uidStatus : autocryptStatus;
        for (Address address : Address.parseUnencoded(email)) {
            String emailAddress = address.getAddress();
            if (recipientMap.containsKey(emailAddress)) {
                Recipient recipient = recipientMap.get(emailAddress);
                switch(effectiveStatus) {
                    case CRYPTO_PROVIDER_STATUS_UNTRUSTED:
                        {
                            if (recipient.getCryptoStatus() == RecipientCryptoStatus.UNAVAILABLE) {
                                recipient.setCryptoStatus(RecipientCryptoStatus.AVAILABLE_UNTRUSTED);
                            }
                            break;
                        }
                    case CRYPTO_PROVIDER_STATUS_TRUSTED:
                        {
                            if (recipient.getCryptoStatus() != RecipientCryptoStatus.AVAILABLE_TRUSTED) {
                                recipient.setCryptoStatus(RecipientCryptoStatus.AVAILABLE_TRUSTED);
                            }
                            break;
                        }
                }
            }
        }
    }
    cursor.close();
    if (observerKey != null) {
        observerKey = new ForceLoadContentObserver();
        contentResolver.registerContentObserver(queryUri, false, observerKey);
    }
}
Also used : Address(com.fsck.k9.mail.Address) ArrayList(java.util.ArrayList) Recipient(com.fsck.k9.view.RecipientSelectView.Recipient) EmptyCursor(com.fsck.k9.helper.EmptyCursor) Cursor(android.database.Cursor) Uri(android.net.Uri)

Example 73 with Address

use of com.fsck.k9.mail.Address in project k-9 by k9mail.

the class RecipientLoader method fillContactDataFromCryptoProvider.

private void fillContactDataFromCryptoProvider(String query, List<Recipient> recipients, Map<String, Recipient> recipientMap) {
    Cursor cursor;
    try {
        Uri queryUri = Uri.parse("content://" + cryptoProvider + ".provider.exported/autocrypt_status");
        cursor = contentResolver.query(queryUri, PROJECTION_CRYPTO_ADDRESSES, null, new String[] { "%" + query + "%" }, null);
        if (cursor == null) {
            return;
        }
    } catch (SecurityException e) {
        Timber.e(e, "Couldn't obtain recipients from crypto provider!");
        return;
    }
    while (cursor.moveToNext()) {
        String uid = cursor.getString(INDEX_USER_ID);
        Address[] addresses = Address.parseUnencoded(uid);
        for (Address address : addresses) {
            String emailAddress = address.getAddress();
            if (!isSupportedEmailAddress(emailAddress) || recipientMap.containsKey(emailAddress)) {
                continue;
            }
            Recipient recipient = new Recipient(address);
            recipients.add(recipient);
            recipientMap.put(emailAddress, recipient);
        }
    }
    cursor.close();
}
Also used : Address(com.fsck.k9.mail.Address) Recipient(com.fsck.k9.view.RecipientSelectView.Recipient) EmptyCursor(com.fsck.k9.helper.EmptyCursor) Cursor(android.database.Cursor) Uri(android.net.Uri)

Example 74 with Address

use of com.fsck.k9.mail.Address in project k-9 by k9mail.

the class RecipientLoader method fillContactDataFromCursor.

private void fillContactDataFromCursor(Cursor cursor, List<Recipient> recipients, Map<String, Recipient> recipientMap, @Nullable String prefilledName, @Nullable Integer maxRecipients) {
    while (cursor.moveToNext() && (maxRecipients == null || recipients.size() < maxRecipients)) {
        String name = prefilledName != null ? prefilledName : cursor.getString(INDEX_NAME);
        String email = cursor.getString(INDEX_EMAIL);
        // already exists? just skip then
        if (email == null || !isSupportedEmailAddress(email) || recipientMap.containsKey(email)) {
            // TODO We should probably merging contacts with the same email address
            continue;
        }
        long contactId = cursor.getLong(INDEX_CONTACT_ID);
        String lookupKey = cursor.getString(INDEX_LOOKUP_KEY);
        int timesContacted = cursor.getInt(INDEX_TIMES_CONTACTED);
        String sortKey = cursor.getString(INDEX_KEY_PRIMARY);
        boolean starred = cursor.getInt(INDEX_STARRED) == 1;
        int addressType = cursor.getInt(INDEX_EMAIL_TYPE);
        String addressLabel = null;
        switch(addressType) {
            case ContactsContract.CommonDataKinds.Email.TYPE_HOME:
                {
                    addressLabel = getContext().getString(R.string.address_type_home);
                    break;
                }
            case ContactsContract.CommonDataKinds.Email.TYPE_WORK:
                {
                    addressLabel = getContext().getString(R.string.address_type_work);
                    break;
                }
            case ContactsContract.CommonDataKinds.Email.TYPE_OTHER:
                {
                    addressLabel = getContext().getString(R.string.address_type_other);
                    break;
                }
            case ContactsContract.CommonDataKinds.Email.TYPE_MOBILE:
                {
                    // mobile isn't listed as an option contacts app, but it has a constant so we better support it
                    addressLabel = getContext().getString(R.string.address_type_mobile);
                    break;
                }
            case ContactsContract.CommonDataKinds.Email.TYPE_CUSTOM:
                {
                    addressLabel = cursor.getString(INDEX_EMAIL_CUSTOM_LABEL);
                    break;
                }
        }
        Recipient recipient = new Recipient(name, email, addressLabel, contactId, lookupKey, timesContacted, sortKey, starred);
        if (recipient.isValidEmailAddress()) {
            recipient.photoThumbnailUri = cursor.isNull(INDEX_PHOTO_URI) ? null : Uri.parse(cursor.getString(INDEX_PHOTO_URI));
            recipientMap.put(email, recipient);
            recipients.add(recipient);
        }
    }
    cursor.close();
}
Also used : Recipient(com.fsck.k9.view.RecipientSelectView.Recipient)

Example 75 with Address

use of com.fsck.k9.mail.Address in project k-9 by k9mail.

the class MessageCryptoHelper method getDecryptVerifyIntent.

@NonNull
private Intent getDecryptVerifyIntent() {
    Intent decryptIntent = new Intent(OpenPgpApi.ACTION_DECRYPT_VERIFY);
    Address[] from = currentMessage.getFrom();
    if (from.length > 0) {
        decryptIntent.putExtra(OpenPgpApi.EXTRA_SENDER_ADDRESS, from[0].getAddress());
        // we add this here independently of the autocrypt peer update, to allow picking up signing keys as gossip
        decryptIntent.putExtra(OpenPgpApi.EXTRA_AUTOCRYPT_PEER_ID, from[0].getAddress());
    }
    autocryptOperations.addAutocryptPeerUpdateToIntentIfPresent(currentMessage, decryptIntent);
    decryptIntent.putExtra(OpenPgpApi.EXTRA_SUPPORT_OVERRIDE_CRYPTO_WARNING, true);
    decryptIntent.putExtra(OpenPgpApi.EXTRA_DECRYPTION_RESULT, cachedDecryptionResult);
    return decryptIntent;
}
Also used : Address(com.fsck.k9.mail.Address) PendingIntent(android.app.PendingIntent) Intent(android.content.Intent) NonNull(androidx.annotation.NonNull)

Aggregations

Address (com.fsck.k9.mail.Address)72 Test (org.junit.Test)40 Uri (android.net.Uri)13 RobolectricTest (com.fsck.k9.RobolectricTest)12 Message (com.fsck.k9.mail.Message)10 Date (java.util.Date)8 MimeMessage (com.fsck.k9.mail.internet.MimeMessage)7 ArrayList (java.util.ArrayList)7 MessagingException (com.fsck.k9.mail.MessagingException)6 Contacts (com.fsck.k9.helper.Contacts)5 Recipient (com.fsck.k9.view.RecipientSelectView.Recipient)5 AddressStyle (com.zegoggles.smssync.preferences.AddressStyle)5 IOException (java.io.IOException)5 Intent (android.content.Intent)4 Cursor (android.database.Cursor)4 MatrixCursor (android.database.MatrixCursor)4 AuthenticationFailedException (com.fsck.k9.mail.AuthenticationFailedException)4 CertificateValidationException (com.fsck.k9.mail.CertificateValidationException)4 TextBody (com.fsck.k9.mail.internet.TextBody)4 SpannableString (android.text.SpannableString)3