Search in sources :

Example 6 with MessagingException

use of com.android.voicemail.impl.mail.MessagingException in project android_packages_apps_Dialer by LineageOS.

the class ImapHelper method fetchAllVoicemails.

/**
 * Fetch a list of voicemails from the server.
 *
 * @return A list of voicemail objects containing data about voicemails stored on the server.
 */
public List<Voicemail> fetchAllVoicemails() {
    List<Voicemail> result = new ArrayList<Voicemail>();
    Message[] messages;
    try {
        mFolder = openImapFolder(ImapFolder.MODE_READ_WRITE);
        if (mFolder == null) {
            // This means we were unable to successfully open the folder.
            return null;
        }
        // This method retrieves lightweight messages containing only the uid of the message.
        messages = mFolder.getMessages(null);
        for (Message message : messages) {
            // Get the voicemail details (message structure).
            MessageStructureWrapper messageStructureWrapper = fetchMessageStructure(message);
            if (messageStructureWrapper != null) {
                result.add(getVoicemailFromMessageStructure(messageStructureWrapper));
            }
        }
        return result;
    } catch (MessagingException e) {
        LogUtils.e(TAG, e, "Messaging Exception");
        return null;
    } finally {
        closeImapFolder();
    }
}
Also used : Message(com.android.voicemail.impl.mail.Message) MimeMessage(com.android.voicemail.impl.mail.internet.MimeMessage) MessagingException(com.android.voicemail.impl.mail.MessagingException) ArrayList(java.util.ArrayList) Voicemail(com.android.voicemail.impl.Voicemail)

Example 7 with MessagingException

use of com.android.voicemail.impl.mail.MessagingException in project android_packages_apps_Dialer by LineageOS.

the class Vvm3Protocol method startProvisionNewUser.

private void startProvisionNewUser(ActivationTask task, PhoneAccountHandle phoneAccountHandle, OmtpVvmCarrierConfigHelper config, VoicemailStatus.Editor status, StatusMessage message) {
    try (NetworkWrapper wrapper = VvmNetworkRequest.getNetwork(config, phoneAccountHandle, status)) {
        Network network = wrapper.get();
        VvmLog.i(TAG, "new user: network available");
        try (ImapHelper helper = new ImapHelper(config.getContext(), phoneAccountHandle, network, status)) {
            // TODO(b/29082671): use LocaleList
            if (Locale.getDefault().getLanguage().equals(new Locale(ISO639_Spanish).getLanguage())) {
                // Spanish
                helper.changeVoicemailTuiLanguage(VVM3_VM_LANGUAGE_SPANISH_STANDARD_NO_GUEST_PROMPTS);
            } else {
                // English
                helper.changeVoicemailTuiLanguage(VVM3_VM_LANGUAGE_ENGLISH_STANDARD_NO_GUEST_PROMPTS);
            }
            VvmLog.i(TAG, "new user: language set");
            if (setPin(config.getContext(), phoneAccountHandle, helper, message)) {
                // Only close new user tutorial if the PIN has been changed.
                helper.closeNewUserTutorial();
                VvmLog.i(TAG, "new user: NUT closed");
                LoggerUtils.logImpressionOnMainThread(config.getContext(), DialerImpression.Type.VVM_PROVISIONING_COMPLETED);
                config.requestStatus(null);
            }
        } catch (InitializingException | MessagingException | IOException e) {
            config.handleEvent(status, OmtpEvents.VVM3_NEW_USER_SETUP_FAILED);
            task.fail();
            VvmLog.e(TAG, e.toString());
        }
    } catch (RequestFailedException e) {
        config.handleEvent(status, OmtpEvents.DATA_NO_CONNECTION_CELLULAR_REQUIRED);
        task.fail();
    }
}
Also used : Locale(java.util.Locale) NetworkWrapper(com.android.voicemail.impl.sync.VvmNetworkRequest.NetworkWrapper) MessagingException(com.android.voicemail.impl.mail.MessagingException) RequestFailedException(com.android.voicemail.impl.sync.VvmNetworkRequest.RequestFailedException) Network(android.net.Network) IOException(java.io.IOException) InitializingException(com.android.voicemail.impl.imap.ImapHelper.InitializingException) ImapHelper(com.android.voicemail.impl.imap.ImapHelper)

Example 8 with MessagingException

use of com.android.voicemail.impl.mail.MessagingException in project android_packages_apps_Dialer by LineageOS.

the class ImapFolder method parseBodyStructure.

private static void parseBodyStructure(ImapList bs, Part part, String id) throws MessagingException {
    if (bs.getElementOrNone(0).isList()) {
        /*
       * This is a multipart/*
       */
        MimeMultipart mp = new MimeMultipart();
        for (int i = 0, count = bs.size(); i < count; i++) {
            ImapElement e = bs.getElementOrNone(i);
            if (e.isList()) {
                /*
           * For each part in the message we're going to add a new BodyPart and parse
           * into it.
           */
                MimeBodyPart bp = new MimeBodyPart();
                if (id.equals(ImapConstants.TEXT)) {
                    parseBodyStructure(bs.getListOrEmpty(i), bp, Integer.toString(i + 1));
                } else {
                    parseBodyStructure(bs.getListOrEmpty(i), bp, id + "." + (i + 1));
                }
                mp.addBodyPart(bp);
            } else {
                if (e.isString()) {
                    mp.setSubType(bs.getStringOrEmpty(i).getString().toLowerCase(Locale.US));
                }
                // Ignore the rest of the list.
                break;
            }
        }
        part.setBody(mp);
    } else {
        /*
       * This is a body. We need to add as much information as we can find out about
       * it to the Part.
       */
        /*
      body type
      body subtype
      body parameter parenthesized list
      body id
      body description
      body encoding
      body size
      */
        final ImapString type = bs.getStringOrEmpty(0);
        final ImapString subType = bs.getStringOrEmpty(1);
        final String mimeType = (type.getString() + "/" + subType.getString()).toLowerCase(Locale.US);
        final ImapList bodyParams = bs.getListOrEmpty(2);
        final ImapString cid = bs.getStringOrEmpty(3);
        final ImapString encoding = bs.getStringOrEmpty(5);
        final int size = bs.getStringOrEmpty(6).getNumberOrZero();
        if (MimeUtility.mimeTypeMatches(mimeType, MimeUtility.MIME_TYPE_RFC822)) {
            /*
         * This will be caught by fetch and handled appropriately.
         */
            throw new MessagingException("BODYSTRUCTURE " + MimeUtility.MIME_TYPE_RFC822 + " not yet supported.");
        }
        /*
       * Set the content type with as much information as we know right now.
       */
        final StringBuilder contentType = new StringBuilder(mimeType);
        /*
       * If there are body params we might be able to get some more information out
       * of them.
       */
        for (int i = 1, count = bodyParams.size(); i < count; i += 2) {
            // TODO We need to convert " into %22, but
            // because MimeUtility.getHeaderParameter doesn't recognize it,
            // we can't fix it for now.
            contentType.append(String.format(";\n %s=\"%s\"", bodyParams.getStringOrEmpty(i - 1).getString(), bodyParams.getStringOrEmpty(i).getString()));
        }
        part.setHeader(MimeHeader.HEADER_CONTENT_TYPE, contentType.toString());
        // Extension items
        final ImapList bodyDisposition;
        if (type.is(ImapConstants.TEXT) && bs.getElementOrNone(9).isList()) {
            // If media-type is TEXT, 9th element might be: [body-fld-lines] := number
            // So, if it's not a list, use 10th element.
            // (Couldn't find evidence in the RFC if it's ALWAYS 10th element.)
            bodyDisposition = bs.getListOrEmpty(9);
        } else {
            bodyDisposition = bs.getListOrEmpty(8);
        }
        final StringBuilder contentDisposition = new StringBuilder();
        if (bodyDisposition.size() > 0) {
            final String bodyDisposition0Str = bodyDisposition.getStringOrEmpty(0).getString().toLowerCase(Locale.US);
            if (!TextUtils.isEmpty(bodyDisposition0Str)) {
                contentDisposition.append(bodyDisposition0Str);
            }
            final ImapList bodyDispositionParams = bodyDisposition.getListOrEmpty(1);
            if (!bodyDispositionParams.isEmpty()) {
                /*
           * If there is body disposition information we can pull some more
           * information about the attachment out.
           */
                for (int i = 1, count = bodyDispositionParams.size(); i < count; i += 2) {
                    // TODO We need to convert " into %22.  See above.
                    contentDisposition.append(String.format(Locale.US, ";\n %s=\"%s\"", bodyDispositionParams.getStringOrEmpty(i - 1).getString().toLowerCase(Locale.US), bodyDispositionParams.getStringOrEmpty(i).getString()));
                }
            }
        }
        if ((size > 0) && (MimeUtility.getHeaderParameter(contentDisposition.toString(), "size") == null)) {
            contentDisposition.append(String.format(Locale.US, ";\n size=%d", size));
        }
        if (contentDisposition.length() > 0) {
            /*
         * Set the content disposition containing at least the size. Attachment
         * handling code will use this down the road.
         */
            part.setHeader(MimeHeader.HEADER_CONTENT_DISPOSITION, contentDisposition.toString());
        }
        /*
       * Set the Content-Transfer-Encoding header. Attachment code will use this
       * to parse the body.
       */
        if (!encoding.isEmpty()) {
            part.setHeader(MimeHeader.HEADER_CONTENT_TRANSFER_ENCODING, encoding.getString());
        }
        /*
       * Set the Content-ID header.
       */
        if (!cid.isEmpty()) {
            part.setHeader(MimeHeader.HEADER_CONTENT_ID, cid.getString());
        }
        if (size > 0) {
            if (part instanceof ImapMessage) {
                ((ImapMessage) part).setSize(size);
            } else if (part instanceof MimeBodyPart) {
                ((MimeBodyPart) part).setSize(size);
            } else {
                throw new MessagingException("Unknown part type " + part.toString());
            }
        }
        part.setHeader(MimeHeader.HEADER_ANDROID_ATTACHMENT_STORE_DATA, id);
    }
}
Also used : ImapElement(com.android.voicemail.impl.mail.store.imap.ImapElement) ImapList(com.android.voicemail.impl.mail.store.imap.ImapList) MimeMultipart(com.android.voicemail.impl.mail.internet.MimeMultipart) MessagingException(com.android.voicemail.impl.mail.MessagingException) ImapMessage(com.android.voicemail.impl.mail.store.ImapStore.ImapMessage) ImapString(com.android.voicemail.impl.mail.store.imap.ImapString) MimeBodyPart(com.android.voicemail.impl.mail.internet.MimeBodyPart) ImapString(com.android.voicemail.impl.mail.store.imap.ImapString)

Example 9 with MessagingException

use of com.android.voicemail.impl.mail.MessagingException in project android_packages_apps_Dialer by LineageOS.

the class ImapFolder method fetchInternal.

public void fetchInternal(Message[] messages, FetchProfile fp, MessageRetrievalListener listener) throws MessagingException {
    if (messages.length == 0) {
        return;
    }
    checkOpen();
    ArrayMap<String, Message> messageMap = new ArrayMap<String, Message>();
    for (Message m : messages) {
        messageMap.put(m.getUid(), m);
    }
    /*
     * Figure out what command we are going to run:
     * FLAGS     - UID FETCH (FLAGS)
     * ENVELOPE  - UID FETCH (INTERNALDATE UID RFC822.SIZE FLAGS BODY.PEEK[
     *                            HEADER.FIELDS (date subject from content-type to cc)])
     * STRUCTURE - UID FETCH (BODYSTRUCTURE)
     * BODY_SANE - UID FETCH (BODY.PEEK[]<0.N>) where N = max bytes returned
     * BODY      - UID FETCH (BODY.PEEK[])
     * Part      - UID FETCH (BODY.PEEK[ID]) where ID = mime part ID
     */
    final LinkedHashSet<String> fetchFields = new LinkedHashSet<String>();
    fetchFields.add(ImapConstants.UID);
    if (fp.contains(FetchProfile.Item.FLAGS)) {
        fetchFields.add(ImapConstants.FLAGS);
    }
    if (fp.contains(FetchProfile.Item.ENVELOPE)) {
        fetchFields.add(ImapConstants.INTERNALDATE);
        fetchFields.add(ImapConstants.RFC822_SIZE);
        fetchFields.add(ImapConstants.FETCH_FIELD_HEADERS);
    }
    if (fp.contains(FetchProfile.Item.STRUCTURE)) {
        fetchFields.add(ImapConstants.BODYSTRUCTURE);
    }
    if (fp.contains(FetchProfile.Item.BODY_SANE)) {
        fetchFields.add(ImapConstants.FETCH_FIELD_BODY_PEEK_SANE);
    }
    if (fp.contains(FetchProfile.Item.BODY)) {
        fetchFields.add(ImapConstants.FETCH_FIELD_BODY_PEEK);
    }
    // TODO Why are we only fetching the first part given?
    final Part fetchPart = fp.getFirstPart();
    if (fetchPart != null) {
        final String[] partIds = fetchPart.getHeader(MimeHeader.HEADER_ANDROID_ATTACHMENT_STORE_DATA);
        // the first id if there are more than one?
        if (partIds != null) {
            fetchFields.add(ImapConstants.FETCH_FIELD_BODY_PEEK_BARE + "[" + partIds[0] + "]");
        }
    }
    try {
        mConnection.sendCommand(String.format(Locale.US, ImapConstants.UID_FETCH + " %s (%s)", ImapStore.joinMessageUids(messages), Utility.combine(fetchFields.toArray(new String[fetchFields.size()]), ' ')), false);
        ImapResponse response;
        do {
            response = null;
            try {
                response = mConnection.readResponse();
                if (!response.isDataResponse(1, ImapConstants.FETCH)) {
                    // Ignore
                    continue;
                }
                final ImapList fetchList = response.getListOrEmpty(2);
                final String uid = fetchList.getKeyedStringOrEmpty(ImapConstants.UID).getString();
                if (TextUtils.isEmpty(uid))
                    continue;
                ImapMessage message = (ImapMessage) messageMap.get(uid);
                if (message == null)
                    continue;
                if (fp.contains(FetchProfile.Item.FLAGS)) {
                    final ImapList flags = fetchList.getKeyedListOrEmpty(ImapConstants.FLAGS);
                    for (int i = 0, count = flags.size(); i < count; i++) {
                        final ImapString flag = flags.getStringOrEmpty(i);
                        if (flag.is(ImapConstants.FLAG_DELETED)) {
                            message.setFlagInternal(Flag.DELETED, true);
                        } else if (flag.is(ImapConstants.FLAG_ANSWERED)) {
                            message.setFlagInternal(Flag.ANSWERED, true);
                        } else if (flag.is(ImapConstants.FLAG_SEEN)) {
                            message.setFlagInternal(Flag.SEEN, true);
                        } else if (flag.is(ImapConstants.FLAG_FLAGGED)) {
                            message.setFlagInternal(Flag.FLAGGED, true);
                        }
                    }
                }
                if (fp.contains(FetchProfile.Item.ENVELOPE)) {
                    final Date internalDate = fetchList.getKeyedStringOrEmpty(ImapConstants.INTERNALDATE).getDateOrNull();
                    final int size = fetchList.getKeyedStringOrEmpty(ImapConstants.RFC822_SIZE).getNumberOrZero();
                    final String header = fetchList.getKeyedStringOrEmpty(ImapConstants.BODY_BRACKET_HEADER, true).getString();
                    message.setInternalDate(internalDate);
                    message.setSize(size);
                    try {
                        message.parse(Utility.streamFromAsciiString(header));
                    } catch (Exception e) {
                        VvmLog.e(TAG, "Error parsing header %s", e);
                    }
                }
                if (fp.contains(FetchProfile.Item.STRUCTURE)) {
                    ImapList bs = fetchList.getKeyedListOrEmpty(ImapConstants.BODYSTRUCTURE);
                    if (!bs.isEmpty()) {
                        try {
                            parseBodyStructure(bs, message, ImapConstants.TEXT);
                        } catch (MessagingException e) {
                            VvmLog.v(TAG, "Error handling message", e);
                            message.setBody(null);
                        }
                    }
                }
                if (fp.contains(FetchProfile.Item.BODY) || fp.contains(FetchProfile.Item.BODY_SANE)) {
                    // Body is keyed by "BODY[]...".
                    // Previously used "BODY[..." but this can be confused with "BODY[HEADER..."
                    // TODO Should we accept "RFC822" as well??
                    ImapString body = fetchList.getKeyedStringOrEmpty("BODY[]", true);
                    InputStream bodyStream = body.getAsStream();
                    try {
                        message.parse(bodyStream);
                    } catch (Exception e) {
                        VvmLog.e(TAG, "Error parsing body %s", e);
                    }
                }
                if (fetchPart != null) {
                    InputStream bodyStream = fetchList.getKeyedStringOrEmpty("BODY[", true).getAsStream();
                    String[] encodings = fetchPart.getHeader(MimeHeader.HEADER_CONTENT_TRANSFER_ENCODING);
                    String contentTransferEncoding = null;
                    if (encodings != null && encodings.length > 0) {
                        contentTransferEncoding = encodings[0];
                    } else {
                        // According to http://tools.ietf.org/html/rfc2045#section-6.1
                        // "7bit" is the default.
                        contentTransferEncoding = "7bit";
                    }
                    try {
                        // TODO Don't create 2 temp files.
                        // decodeBody creates BinaryTempFileBody, but we could avoid this
                        // if we implement ImapStringBody.
                        // (We'll need to share a temp file.  Protect it with a ref-count.)
                        message.setBody(decodeBody(mStore.getContext(), bodyStream, contentTransferEncoding, fetchPart.getSize(), listener));
                    } catch (Exception e) {
                        // TODO: Figure out what kinds of exceptions might actually be thrown
                        // from here. This blanket catch-all is because we're not sure what to
                        // do if we don't have a contentTransferEncoding, and we don't have
                        // time to figure out what exceptions might be thrown.
                        VvmLog.e(TAG, "Error fetching body %s", e);
                    }
                }
                if (listener != null) {
                    listener.messageRetrieved(message);
                }
            } finally {
                destroyResponses();
            }
        } while (!response.isTagged());
    } catch (IOException ioe) {
        mStore.getImapHelper().handleEvent(OmtpEvents.DATA_GENERIC_IMAP_IOE);
        throw ioExceptionHandler(mConnection, ioe);
    }
}
Also used : LinkedHashSet(java.util.LinkedHashSet) ImapList(com.android.voicemail.impl.mail.store.imap.ImapList) Message(com.android.voicemail.impl.mail.Message) ImapMessage(com.android.voicemail.impl.mail.store.ImapStore.ImapMessage) ImapMessage(com.android.voicemail.impl.mail.store.ImapStore.ImapMessage) MessagingException(com.android.voicemail.impl.mail.MessagingException) InputStream(java.io.InputStream) ArrayMap(android.util.ArrayMap) ImapResponse(com.android.voicemail.impl.mail.store.imap.ImapResponse) ImapString(com.android.voicemail.impl.mail.store.imap.ImapString) IOException(java.io.IOException) Date(java.util.Date) AuthenticationFailedException(com.android.voicemail.impl.mail.AuthenticationFailedException) IOException(java.io.IOException) ImapException(com.android.voicemail.impl.mail.store.ImapStore.ImapException) Base64DataException(android.util.Base64DataException) MessagingException(com.android.voicemail.impl.mail.MessagingException) Part(com.android.voicemail.impl.mail.Part) MimeBodyPart(com.android.voicemail.impl.mail.internet.MimeBodyPart) ImapString(com.android.voicemail.impl.mail.store.imap.ImapString)

Example 10 with MessagingException

use of com.android.voicemail.impl.mail.MessagingException in project android_packages_apps_Dialer by LineageOS.

the class ImapHelper method fetchTranscription.

public boolean fetchTranscription(TranscriptionFetchedCallback callback, String uid) {
    try {
        mFolder = openImapFolder(ImapFolder.MODE_READ_WRITE);
        if (mFolder == null) {
            // This means we were unable to successfully open the folder.
            return false;
        }
        Message message = mFolder.getMessage(uid);
        if (message == null) {
            return false;
        }
        MessageStructureWrapper messageStructureWrapper = fetchMessageStructure(message);
        if (messageStructureWrapper != null) {
            TranscriptionFetchedListener listener = new TranscriptionFetchedListener();
            if (messageStructureWrapper.transcriptionBodyPart != null) {
                FetchProfile fetchProfile = new FetchProfile();
                fetchProfile.add(messageStructureWrapper.transcriptionBodyPart);
                // This method is called synchronously so the transcription will be populated
                // in the listener once the next method is called.
                mFolder.fetch(new Message[] { message }, fetchProfile, listener);
                callback.setVoicemailTranscription(listener.getVoicemailTranscription());
            }
        }
        return true;
    } catch (MessagingException e) {
        LogUtils.e(TAG, e, "Messaging Exception");
        return false;
    } finally {
        closeImapFolder();
    }
}
Also used : FetchProfile(com.android.voicemail.impl.mail.FetchProfile) Message(com.android.voicemail.impl.mail.Message) MimeMessage(com.android.voicemail.impl.mail.internet.MimeMessage) MessagingException(com.android.voicemail.impl.mail.MessagingException)

Aggregations

MessagingException (com.android.voicemail.impl.mail.MessagingException)12 IOException (java.io.IOException)5 Message (com.android.voicemail.impl.mail.Message)4 MimeMessage (com.android.voicemail.impl.mail.internet.MimeMessage)3 ImapResponse (com.android.voicemail.impl.mail.store.imap.ImapResponse)3 ImapString (com.android.voicemail.impl.mail.store.imap.ImapString)3 MimeBodyPart (com.android.voicemail.impl.mail.internet.MimeBodyPart)2 ImapMessage (com.android.voicemail.impl.mail.store.ImapStore.ImapMessage)2 ImapList (com.android.voicemail.impl.mail.store.imap.ImapList)2 Network (android.net.Network)1 ArrayMap (android.util.ArrayMap)1 Base64DataException (android.util.Base64DataException)1 Voicemail (com.android.voicemail.impl.Voicemail)1 ImapHelper (com.android.voicemail.impl.imap.ImapHelper)1 InitializingException (com.android.voicemail.impl.imap.ImapHelper.InitializingException)1 AuthenticationFailedException (com.android.voicemail.impl.mail.AuthenticationFailedException)1 CertificateValidationException (com.android.voicemail.impl.mail.CertificateValidationException)1 FetchProfile (com.android.voicemail.impl.mail.FetchProfile)1 FixedLengthInputStream (com.android.voicemail.impl.mail.FixedLengthInputStream)1 Part (com.android.voicemail.impl.mail.Part)1