Upgrade ISO-8859-1 to UTF-8 for plain text messages

This commit is contained in:
M66B 2020-10-18 19:46:33 +02:00
parent 121456cc8c
commit e9c0bfa542
1 changed files with 22 additions and 8 deletions

View File

@ -57,6 +57,7 @@ import java.io.UnsupportedEncodingException;
import java.net.IDN;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.charset.UnsupportedCharsetException;
import java.security.NoSuchAlgorithmException;
import java.text.Normalizer;
import java.text.ParsePosition;
@ -1775,16 +1776,29 @@ public class MessageHelper {
charset = null;
if (h.part.isMimeType("text/plain")) {
if (charset == null) {
Charset cs = null;
try {
if (charset != null)
cs = Charset.forName(charset);
} catch (UnsupportedCharsetException ignored) {
}
if (charset == null || StandardCharsets.ISO_8859_1.equals(cs)) {
Charset detected = CharsetHelper.detect(result);
if (detected == null) {
if (CharsetHelper.isUTF8(result)) {
Log.i("Charset plain=UTF8");
result = new String(result.getBytes(StandardCharsets.ISO_8859_1), StandardCharsets.UTF_8);
}
if (StandardCharsets.ISO_8859_1.equals(cs) &&
StandardCharsets.UTF_8.equals(detected)) {
Log.i("Charset upgrade=UTF8");
result = new String(result.getBytes(StandardCharsets.ISO_8859_1), StandardCharsets.UTF_8);
} else {
Log.i("Charset plain=" + detected.name());
result = new String(result.getBytes(StandardCharsets.ISO_8859_1), detected);
if (detected == null) {
if (CharsetHelper.isUTF8(result)) {
Log.i("Charset plain=UTF8");
result = new String(result.getBytes(StandardCharsets.ISO_8859_1), StandardCharsets.UTF_8);
}
} else {
Log.i("Charset plain=" + detected.name());
result = new String(result.getBytes(StandardCharsets.ISO_8859_1), detected);
}
}
}