diff --git a/app/src/main/java/eu/faircode/email/MessageHelper.java b/app/src/main/java/eu/faircode/email/MessageHelper.java index 1fe4d07625..326eb27d80 100644 --- a/app/src/main/java/eu/faircode/email/MessageHelper.java +++ b/app/src/main/java/eu/faircode/email/MessageHelper.java @@ -1251,28 +1251,27 @@ public class MessageHelper { try { ContentType ct = new ContentType(part.getContentType()); - String charset = ct.getParameter("charset"); - // Fix common mistakes - if (charset != null) { + String charset = ct.getParameter("charset"); + if (charset == null) + charset = StandardCharsets.ISO_8859_1.name(); + else { charset = charset.replace("\"", ""); - if ("ASCII".equals(charset.toUpperCase())) - charset = "US-ASCII"; - else if (charset.toLowerCase().endsWith("8859-16")) // not supported by Android - charset = null; // Use ISO8859-1 instead - } - - if (TextUtils.isEmpty(charset) || "US-ASCII".equals(charset.toUpperCase())) { - // The first 127 characters are the same as in US-ASCII - result = new String(result.getBytes(StandardCharsets.ISO_8859_1)); - } else { - // See UnknownCharsetProvider class - if ("US-ASCII".equals(Charset.forName(charset).name())) { - Log.w("Unsupported encoding charset=" + charset); + charset = MimeUtility.javaCharset(charset); + + if (!Charset.isSupported(charset)) { + // x-binaryenc + // UseInqueCodePage + // none + // unknown-8bit + // X-UNKNOWN + Log.e("Unsupported encoding charset=" + charset); warnings.add(context.getString(R.string.title_no_charset, charset)); - result = new String(result.getBytes(StandardCharsets.ISO_8859_1)); + charset = StandardCharsets.ISO_8859_1.name(); } } + + result = new String(result.getBytes(Charset.forName(charset))); } catch (ParseException ex) { Log.w(ex); warnings.add(Log.formatThrowable(ex, false)); diff --git a/app/src/main/java/eu/faircode/email/UnknownCharsetProvider.java b/app/src/main/java/eu/faircode/email/UnknownCharsetProvider.java deleted file mode 100644 index a5363c4726..0000000000 --- a/app/src/main/java/eu/faircode/email/UnknownCharsetProvider.java +++ /dev/null @@ -1,48 +0,0 @@ -package eu.faircode.email; - -/* - This file is part of FairEmail. - - FairEmail is free software: you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. - - FairEmail is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with FairEmail. If not, see . - - Copyright 2018-2020 by Marcel Bokhorst (M66B) -*/ - -import android.text.TextUtils; - -import java.nio.charset.Charset; -import java.nio.charset.StandardCharsets; -import java.nio.charset.spi.CharsetProvider; -import java.util.Collections; -import java.util.Iterator; - -public class UnknownCharsetProvider extends CharsetProvider { - @Override - public Iterator charsets() { - return Collections.emptyIterator(); - } - - @Override - public Charset charsetForName(String name) { - // x-binaryenc - // UseInqueCodePage - // none - // unknown-8bit - // X-UNKNOWN - // https://javaee.github.io/javamail/FAQ#unsupen - if (!TextUtils.isEmpty(name)) - Log.e("Unknown charset=" + name); - return StandardCharsets.US_ASCII; - } -} diff --git a/app/src/main/resources/META-INF/javamail.charset.map b/app/src/main/resources/META-INF/javamail.charset.map index cc58d4f986..268f0ae111 100644 --- a/app/src/main/resources/META-INF/javamail.charset.map +++ b/app/src/main/resources/META-INF/javamail.charset.map @@ -1,6 +1,17 @@ ### JDK-to-MIME charset mapping table #### ### This should be the first mapping table ### +### FairEmail + +8859_16 ISO-8859-1 +iso8859_16 ISO-8859-1 +ISO8859-16 ISO-8859-1 + +ascii ISO-8859-1 +ASCII ISO-8859-1 + +### Inherited + 8859_1 ISO-8859-1 iso8859_1 ISO-8859-1 ISO8859-1 ISO-8859-1 diff --git a/app/src/main/resources/META-INF/services/java.nio.charset.spi.CharsetProvider b/app/src/main/resources/META-INF/services/java.nio.charset.spi.CharsetProvider deleted file mode 100644 index c95b3fc030..0000000000 --- a/app/src/main/resources/META-INF/services/java.nio.charset.spi.CharsetProvider +++ /dev/null @@ -1 +0,0 @@ -eu.faircode.email.UnknownCharsetProvider