From 150d3ca077ddacd515e5bd74a053cd030bc26b1d Mon Sep 17 00:00:00 2001 From: =?utf8?q?Reimar=20D=C3=B6ffinger?= Date: Sat, 18 Apr 2020 13:33:03 +0200 Subject: [PATCH] Restore Android 10 API compatibility. --- Util | 2 +- .../android/dictionary/HtmlDisplayActivity.java | 11 ++++++++--- .../hughes/android/dictionary/engine/HtmlEntry.java | 3 ++- 3 files changed, 11 insertions(+), 5 deletions(-) diff --git a/Util b/Util index dccff89..695e457 160000 --- a/Util +++ b/Util @@ -1 +1 @@ -Subproject commit dccff891ce76045d8de2b1000798c1eb452359f2 +Subproject commit 695e457b4c2f8f8d18b7f39e07999df1f4c44320 diff --git a/src/com/hughes/android/dictionary/HtmlDisplayActivity.java b/src/com/hughes/android/dictionary/HtmlDisplayActivity.java index 5e98159..e2e3d60 100644 --- a/src/com/hughes/android/dictionary/HtmlDisplayActivity.java +++ b/src/com/hughes/android/dictionary/HtmlDisplayActivity.java @@ -31,7 +31,7 @@ import com.hughes.util.StringUtil; import java.io.IOException; import java.io.InputStream; -import java.nio.charset.StandardCharsets; +import java.io.UnsupportedEncodingException; public final class HtmlDisplayActivity extends AppCompatActivity { @@ -101,8 +101,13 @@ public final class HtmlDisplayActivity extends AppCompatActivity { fontSizeSp = 14; } webView.getSettings().setDefaultFontSize(fontSizeSp); - // No way to get pure UTF-8 data into WebView - html = Base64.encodeToString(html.getBytes(StandardCharsets.UTF_8), Base64.DEFAULT); + try { + // No way to get pure UTF-8 data into WebView + // Cannot use StandardCharsets due to older Android. + html = Base64.encodeToString(html.getBytes("UTF-8"), Base64.DEFAULT); + } catch (UnsupportedEncodingException e) { + throw new RuntimeException("Missing UTF-8 support?!", e); + } // Use loadURL to allow specifying a charset webView.loadUrl("data:text/html;charset=utf-8;base64," + html); webView.activity = this; diff --git a/src/com/hughes/android/dictionary/engine/HtmlEntry.java b/src/com/hughes/android/dictionary/engine/HtmlEntry.java index fc15c78..7aa43d5 100644 --- a/src/com/hughes/android/dictionary/engine/HtmlEntry.java +++ b/src/com/hughes/android/dictionary/engine/HtmlEntry.java @@ -267,7 +267,8 @@ public class HtmlEntry extends AbstractEntry implements Comparable { buf.readFully(zipBytes); try { final byte[] bytes = StringUtil.unzipFully(zipBytes, numBytes); - html = new String(bytes, StandardCharsets.UTF_8); + // Cannot use StandardCharsets due to older Android. + html = new String(bytes, "UTF-8"); } catch (IOException e) { throw new RuntimeException("Dictionary HTML data corrupted", e); } -- 2.43.0