From 2709a66e34643a4bf87a2767793e07fbce7d504d Mon Sep 17 00:00:00 2001 From: M66B Date: Fri, 18 Jan 2019 07:20:14 +0000 Subject: [PATCH] Assume ISO-8859-1 if text/plain without charset ICU4J is too large --- README.md | 2 +- app/build.gradle | 2 +- .../java/eu/faircode/email/MessageHelper.java | 22 ++++++++----------- app/src/main/res/values/strings.xml | 1 - 4 files changed, 11 insertions(+), 16 deletions(-) diff --git a/README.md b/README.md index ed18b42c17..6d244b5992 100644 --- a/README.md +++ b/README.md @@ -137,7 +137,7 @@ FairEmail uses: * [JavaMail](https://projects.eclipse.org/projects/ee4j.javamail). Copyright (c) 1997-2018 Oracle® and/or its affiliates. All rights reserved. [GPLv2+CE license](https://javaee.github.io/javamail/JavaMail-License). * [jsoup](https://jsoup.org/). Copyright © 2009 - 2017 Jonathan Hedley. [MIT license](https://jsoup.org/license). * ~~[JCharset](http://www.freeutils.net/source/jcharset/). Copyright © 2005-2015 Amichai Rothman. [GNU General Public License](http://www.freeutils.net/source/jcharset/#license)~~ -* [ICU4J](http://site.icu-project.org). Copyright © 1991-2019 Unicode, Inc. All rights reserved. [UNICODE, INC. LICENSE AGREEMENT](http://www.unicode.org/copyright.html#License) +* ~~[ICU4J](http://site.icu-project.org). Copyright © 1991-2019 Unicode, Inc. All rights reserved. [UNICODE, INC. LICENSE AGREEMENT](http://www.unicode.org/copyright.html#License)~~ * [Android Support Library](https://developer.android.com/tools/support-library/). Copyright (C) 2011 The Android Open Source Project. [Apache license](https://android.googlesource.com/platform/frameworks/support/+/master/LICENSE.txt). * [Android Architecture Components](https://developer.android.com/topic/libraries/architecture/). Copyright 2018 The Android Open Source Project, Inc. [Apache license](https://github.com/googlesamples/android-architecture-components/blob/master/LICENSE). * [colorpicker](https://android.googlesource.com/platform/frameworks/opt/colorpicker). Copyright (C) 2013 The Android Open Source Project. [Apache license](https://android.googlesource.com/platform/frameworks/opt/colorpicker/+/master/src/com/android/colorpicker/ColorPickerDialog.java). diff --git a/app/build.gradle b/app/build.gradle index 943afe3880..0983964a9b 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -162,7 +162,7 @@ dependencies { //implementation "net.freeutils:jcharset:$jcharset_version" // https://mvnrepository.com/artifact/com.ibm.icu/icu4j - implementation "com.ibm.icu:icu4j:$icu4j_version" + //implementation "com.ibm.icu:icu4j:$icu4j_version" // http://www.xbill.org/dnsjava/ implementation "dnsjava:dnsjava:$dnsjava_version" diff --git a/app/src/main/java/eu/faircode/email/MessageHelper.java b/app/src/main/java/eu/faircode/email/MessageHelper.java index e464bc641b..0adfb545bf 100644 --- a/app/src/main/java/eu/faircode/email/MessageHelper.java +++ b/app/src/main/java/eu/faircode/email/MessageHelper.java @@ -23,9 +23,6 @@ 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; @@ -568,16 +565,15 @@ public class MessageHelper { ContentType ct = new ContentType(part.getContentType()); String charset = ct.getParameter("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)); - } + if (BuildConfig.DEBUG) + warnings.add(context.getString(R.string.title_no_charset, ct.toString())); + if (part.isMimeType("text/plain")) + try { + // The first 127 characters are the same as in US-ASCII + result = new String(result.getBytes("ISO-8859-1")); + } catch (UnsupportedEncodingException ex) { + warnings.add(Helper.formatThrowable(ex)); + } } else { if ("US-ASCII".equals(Charset.forName(charset).name()) && !"US-ASCII".equals(charset.toUpperCase())) diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 2a868b9b86..87111f3415 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -457,7 +457,6 @@ Adobe Acrobat reader cannot open safely shared files, see the FAQ for more information No message text found Unsupported encoding: %1$s - Guessed encoding: %1$s Try FairEmail, an open source, privacy friendly email app for Android