Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit c75bd58f authored by cketti's avatar cketti
Browse files

Merge pull request #1209 from k9mail/migrate-npe-fix

Migration: Avoid NPE when textContent is null
parents 91c60a46 f902aef8
Loading
Loading
Loading
Loading
+26 −11
Original line number Diff line number Diff line
@@ -556,9 +556,12 @@ class MigrationTo51 {
        mimeHeader.setHeader(MimeHeader.HEADER_CONTENT_TYPE,
                String.format("multipart/alternative; boundary=\"%s\";", boundary));

        int dataLocation = textContent != null || htmlContent != null
                ? DATA_LOCATION__IN_DATABASE : DATA_LOCATION__MISSING;

        ContentValues cv = new ContentValues();
        cv.put("type", MESSAGE_PART_TYPE__UNKNOWN);
        cv.put("data_location", DATA_LOCATION__IN_DATABASE);
        cv.put("data_location", dataLocation);
        cv.put("mime_type", "multipart/alternative");
        cv.put("header", mimeHeader.toString());
        cv.put("boundary", boundary);
@@ -567,11 +570,11 @@ class MigrationTo51 {
        long multipartAlternativePartId = db.insertOrThrow("message_parts", null, cv);
        structureState = structureState.nextMultipartChild(multipartAlternativePartId);

        if (!TextUtils.isEmpty(textContent)) {
        if (textContent != null) {
            structureState = insertTextualPartIntoDatabase(db, structureState, null, textContent, false);
        }

        if (!TextUtils.isEmpty(htmlContent)) {
        if (htmlContent != null) {
            structureState = insertTextualPartIntoDatabase(db, structureState, null, htmlContent, true);
        }

@@ -587,20 +590,32 @@ class MigrationTo51 {
                isHtml ? "text/html; charset=\"utf-8\"" : "text/plain; charset=\"utf-8\"");
        mimeHeader.setHeader(MimeHeader.HEADER_CONTENT_TRANSFER_ENCODING, MimeUtil.ENC_QUOTED_PRINTABLE);

        byte[] contentBytes;
        int decodedBodySize;
        int dataLocation;
        if (content != null) {
            ByteArrayOutputStream contentOutputStream = new ByteArrayOutputStream();
            QuotedPrintableOutputStream quotedPrintableOutputStream =
                    new QuotedPrintableOutputStream(contentOutputStream, false);
            quotedPrintableOutputStream.write(content.getBytes());
            quotedPrintableOutputStream.flush();
        byte[] contentBytes = contentOutputStream.toByteArray();

            dataLocation = DATA_LOCATION__IN_DATABASE;
            contentBytes = contentOutputStream.toByteArray();
            decodedBodySize = content.length();
        } else {
            dataLocation = DATA_LOCATION__MISSING;
            contentBytes = null;
            decodedBodySize = 0;
        }

        ContentValues cv = new ContentValues();
        cv.put("type", MESSAGE_PART_TYPE__UNKNOWN);
        cv.put("data_location", DATA_LOCATION__IN_DATABASE);
        cv.put("data_location", dataLocation);
        cv.put("mime_type", isHtml ? "text/html" : "text/plain");
        cv.put("header", mimeHeader.toString());
        cv.put("data", contentBytes);
        cv.put("decoded_body_size", content.length());
        cv.put("decoded_body_size", decodedBodySize);
        cv.put("encoding", MimeUtil.ENC_QUOTED_PRINTABLE);
        cv.put("charset", "utf-8");
        structureState.applyValues(cv);