Guess unspecified character sets

This commit is contained in:
M66B
2019-01-17 20:32:44 +00:00
parent 1f1332a169
commit e564c18b90
4 changed files with 21 additions and 1 deletions

View File

@@ -23,6 +23,9 @@ import android.content.Context;
import android.text.TextUtils;
import android.webkit.MimeTypeMap;
import com.ibm.icu.text.CharsetDetector;
import com.ibm.icu.text.CharsetMatch;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Element;
@@ -564,7 +567,18 @@ public class MessageHelper {
ContentType ct = new ContentType(part.getContentType());
String charset = ct.getParameter("charset");
if (!TextUtils.isEmpty(charset)) {
if (TextUtils.isEmpty(charset)) {
try {
CharsetDetector detector = new CharsetDetector();
String defset = (part.isMimeType("text/plain") ? "US-ASCII" : "ISO-8859-1");
detector.setText(result.getBytes(defset));
CharsetMatch match = detector.detect();
result = new String(result.getBytes(match.getName()));
warnings.add(context.getString(R.string.title_guessed_charset, match.getName()));
} catch (UnsupportedEncodingException ex) {
warnings.add(Helper.formatThrowable(ex));
}
} else {
if ("US-ASCII".equals(Charset.forName(charset).name()) &&
!"US-ASCII".equals(charset.toUpperCase()))
warnings.add(context.getString(R.string.title_no_charset, charset));