From bb45f3b6b8e729d4f3998088364914efdbd41105 Mon Sep 17 00:00:00 2001 From: Mohamed Sameh Date: Tue, 11 Feb 2020 16:55:42 +0200 Subject: [PATCH 1/6] Add voice data checks for Read Aloud --- .../core/main/KiwixTextToSpeech.java | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/core/src/main/java/org/kiwix/kiwixmobile/core/main/KiwixTextToSpeech.java b/core/src/main/java/org/kiwix/kiwixmobile/core/main/KiwixTextToSpeech.java index 9c39ccd48..714eb6f6e 100644 --- a/core/src/main/java/org/kiwix/kiwixmobile/core/main/KiwixTextToSpeech.java +++ b/core/src/main/java/org/kiwix/kiwixmobile/core/main/KiwixTextToSpeech.java @@ -131,8 +131,26 @@ public class KiwixTextToSpeech { context.getResources().getString(R.string.tts_lang_not_supported), Toast.LENGTH_LONG).show(); } else { + // check if voice needs to be installed for API below 21 + if(VERSION.SDK_INT < VERSION_CODES.LOLLIPOP + && tts.getFeatures(locale).contains(TextToSpeech.Engine.KEY_FEATURE_NOT_INSTALLED)){ + Toast.makeText(context, + context.getResources().getString(R.string.tts_lang_not_supported), + Toast.LENGTH_LONG).show(); + return; + } + tts.setLanguage(locale); + // check if voice needs to be installed for API 21 or higher + if (VERSION.SDK_INT >= VERSION_CODES.LOLLIPOP + && tts.getVoice().getFeatures().contains(TextToSpeech.Engine.KEY_FEATURE_NOT_INSTALLED)){ + Toast.makeText(context, + context.getResources().getString(R.string.tts_lang_not_supported), + Toast.LENGTH_LONG).show(); + return; + } + if (requestAudioFocus()) { loadURL(webView); } From 2ead1030759ef5d31824db98065fd4569b3dc3ab Mon Sep 17 00:00:00 2001 From: Mohamed Sameh Date: Wed, 12 Feb 2020 22:41:40 +0200 Subject: [PATCH 2/6] Use ContextExtensionKt.toast and unify checking conditions for voice data checks --- .../core/main/KiwixTextToSpeech.java | 22 ++++++------------- 1 file changed, 7 insertions(+), 15 deletions(-) diff --git a/core/src/main/java/org/kiwix/kiwixmobile/core/main/KiwixTextToSpeech.java b/core/src/main/java/org/kiwix/kiwixmobile/core/main/KiwixTextToSpeech.java index 714eb6f6e..102d0dca6 100644 --- a/core/src/main/java/org/kiwix/kiwixmobile/core/main/KiwixTextToSpeech.java +++ b/core/src/main/java/org/kiwix/kiwixmobile/core/main/KiwixTextToSpeech.java @@ -39,6 +39,7 @@ import org.kiwix.kiwixmobile.core.CoreApp; import org.kiwix.kiwixmobile.core.R; import org.kiwix.kiwixmobile.core.utils.LanguageUtils; import org.kiwix.kiwixmobile.core.reader.ZimReaderContainer; +import org.kiwix.kiwixmobile.core.extensions.ContextExtensionsKt; import static org.kiwix.kiwixmobile.core.utils.Constants.TAG_KIWIX; @@ -131,23 +132,14 @@ public class KiwixTextToSpeech { context.getResources().getString(R.string.tts_lang_not_supported), Toast.LENGTH_LONG).show(); } else { - // check if voice needs to be installed for API below 21 - if(VERSION.SDK_INT < VERSION_CODES.LOLLIPOP - && tts.getFeatures(locale).contains(TextToSpeech.Engine.KEY_FEATURE_NOT_INSTALLED)){ - Toast.makeText(context, - context.getResources().getString(R.string.tts_lang_not_supported), - Toast.LENGTH_LONG).show(); - return; - } - tts.setLanguage(locale); - // check if voice needs to be installed for API 21 or higher - if (VERSION.SDK_INT >= VERSION_CODES.LOLLIPOP - && tts.getVoice().getFeatures().contains(TextToSpeech.Engine.KEY_FEATURE_NOT_INSTALLED)){ - Toast.makeText(context, - context.getResources().getString(R.string.tts_lang_not_supported), - Toast.LENGTH_LONG).show(); + if ((VERSION.SDK_INT < VERSION_CODES.LOLLIPOP // check if voice needs to be installed for API below 21 + && tts.getFeatures(locale).contains(TextToSpeech.Engine.KEY_FEATURE_NOT_INSTALLED)) + // check if voice needs to be installed for API 21 or higher + ||(VERSION.SDK_INT >= VERSION_CODES.LOLLIPOP + && tts.getVoice().getFeatures().contains(TextToSpeech.Engine.KEY_FEATURE_NOT_INSTALLED))){ + ContextExtensionsKt.toast(this.context,R.string.tts_lang_not_supported,Toast.LENGTH_LONG); return; } From 99a85349a3a4ce7d99a6e03e8fc078d3a05f529f Mon Sep 17 00:00:00 2001 From: Mohamed Sameh Date: Fri, 14 Feb 2020 23:52:56 +0200 Subject: [PATCH 3/6] Add a method for tts features, Remove redundant code --- .../kiwixmobile/core/main/KiwixTextToSpeech.java | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/core/src/main/java/org/kiwix/kiwixmobile/core/main/KiwixTextToSpeech.java b/core/src/main/java/org/kiwix/kiwixmobile/core/main/KiwixTextToSpeech.java index 102d0dca6..8419b6bef 100644 --- a/core/src/main/java/org/kiwix/kiwixmobile/core/main/KiwixTextToSpeech.java +++ b/core/src/main/java/org/kiwix/kiwixmobile/core/main/KiwixTextToSpeech.java @@ -34,6 +34,7 @@ import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Locale; +import java.util.Set; import java.util.concurrent.atomic.AtomicInteger; import org.kiwix.kiwixmobile.core.CoreApp; import org.kiwix.kiwixmobile.core.R; @@ -134,11 +135,7 @@ public class KiwixTextToSpeech { } else { tts.setLanguage(locale); - if ((VERSION.SDK_INT < VERSION_CODES.LOLLIPOP // check if voice needs to be installed for API below 21 - && tts.getFeatures(locale).contains(TextToSpeech.Engine.KEY_FEATURE_NOT_INSTALLED)) - // check if voice needs to be installed for API 21 or higher - ||(VERSION.SDK_INT >= VERSION_CODES.LOLLIPOP - && tts.getVoice().getFeatures().contains(TextToSpeech.Engine.KEY_FEATURE_NOT_INSTALLED))){ + if (getFeatures(tts,locale).contains(TextToSpeech.Engine.KEY_FEATURE_NOT_INSTALLED)){ ContextExtensionsKt.toast(this.context,R.string.tts_lang_not_supported,Toast.LENGTH_LONG); return; } @@ -149,7 +146,9 @@ public class KiwixTextToSpeech { } } } - + private Set getFeatures(TextToSpeech tts, Locale locale){ + return (VERSION.SDK_INT < VERSION_CODES.LOLLIPOP)? tts.getFeatures(locale) : tts.getVoice().getFeatures(); + } private void loadURL(WebView webView) { // We use JavaScript to get the content of the page conveniently, earlier making some // changes in the page From e4d0ff13f8c57508dec11f39160ccd3823184d68 Mon Sep 17 00:00:00 2001 From: Mohamed Sameh Date: Sat, 15 Feb 2020 00:00:54 +0200 Subject: [PATCH 4/6] Fix linter errors --- .../org/kiwix/kiwixmobile/core/main/KiwixTextToSpeech.java | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/core/src/main/java/org/kiwix/kiwixmobile/core/main/KiwixTextToSpeech.java b/core/src/main/java/org/kiwix/kiwixmobile/core/main/KiwixTextToSpeech.java index 8419b6bef..4a24e196b 100644 --- a/core/src/main/java/org/kiwix/kiwixmobile/core/main/KiwixTextToSpeech.java +++ b/core/src/main/java/org/kiwix/kiwixmobile/core/main/KiwixTextToSpeech.java @@ -147,7 +147,10 @@ public class KiwixTextToSpeech { } } private Set getFeatures(TextToSpeech tts, Locale locale){ - return (VERSION.SDK_INT < VERSION_CODES.LOLLIPOP)? tts.getFeatures(locale) : tts.getVoice().getFeatures(); + if(VERSION.SDK_INT < VERSION_CODES.LOLLIPOP) + return tts.getFeatures(locale); + else + return tts.getVoice().getFeatures(); } private void loadURL(WebView webView) { // We use JavaScript to get the content of the page conveniently, earlier making some From ec1bbec00d332ad279666750f11371b3bdf49af1 Mon Sep 17 00:00:00 2001 From: Mohamed Sameh Date: Thu, 20 Feb 2020 19:38:53 +0200 Subject: [PATCH 5/6] Add linter suppressor to use ternary operator --- .../org/kiwix/kiwixmobile/core/main/KiwixTextToSpeech.java | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/core/src/main/java/org/kiwix/kiwixmobile/core/main/KiwixTextToSpeech.java b/core/src/main/java/org/kiwix/kiwixmobile/core/main/KiwixTextToSpeech.java index 4a24e196b..753791788 100644 --- a/core/src/main/java/org/kiwix/kiwixmobile/core/main/KiwixTextToSpeech.java +++ b/core/src/main/java/org/kiwix/kiwixmobile/core/main/KiwixTextToSpeech.java @@ -146,11 +146,9 @@ public class KiwixTextToSpeech { } } } + @SuppressLint("NewApi") private Set getFeatures(TextToSpeech tts, Locale locale){ - if(VERSION.SDK_INT < VERSION_CODES.LOLLIPOP) - return tts.getFeatures(locale); - else - return tts.getVoice().getFeatures(); + return ((VERSION.SDK_INT < VERSION_CODES.LOLLIPOP)?tts.getFeatures(locale):tts.getVoice().getFeatures()); } private void loadURL(WebView webView) { // We use JavaScript to get the content of the page conveniently, earlier making some From 29b868ffe562cbc53e4ef0589e5bf1ad49389c5d Mon Sep 17 00:00:00 2001 From: Mohamed Sameh Date: Fri, 21 Feb 2020 11:01:14 +0200 Subject: [PATCH 6/6] Add some Formatting --- .../kiwixmobile/core/main/KiwixTextToSpeech.java | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/core/src/main/java/org/kiwix/kiwixmobile/core/main/KiwixTextToSpeech.java b/core/src/main/java/org/kiwix/kiwixmobile/core/main/KiwixTextToSpeech.java index 753791788..0928ee237 100644 --- a/core/src/main/java/org/kiwix/kiwixmobile/core/main/KiwixTextToSpeech.java +++ b/core/src/main/java/org/kiwix/kiwixmobile/core/main/KiwixTextToSpeech.java @@ -135,8 +135,9 @@ public class KiwixTextToSpeech { } else { tts.setLanguage(locale); - if (getFeatures(tts,locale).contains(TextToSpeech.Engine.KEY_FEATURE_NOT_INSTALLED)){ - ContextExtensionsKt.toast(this.context,R.string.tts_lang_not_supported,Toast.LENGTH_LONG); + if (getFeatures(tts, locale).contains(TextToSpeech.Engine.KEY_FEATURE_NOT_INSTALLED)) { + ContextExtensionsKt.toast(context, R.string.tts_lang_not_supported, + Toast.LENGTH_LONG); return; } @@ -146,10 +147,13 @@ public class KiwixTextToSpeech { } } } + @SuppressLint("NewApi") - private Set getFeatures(TextToSpeech tts, Locale locale){ - return ((VERSION.SDK_INT < VERSION_CODES.LOLLIPOP)?tts.getFeatures(locale):tts.getVoice().getFeatures()); + private Set getFeatures(TextToSpeech tts, Locale locale) { + return VERSION.SDK_INT < VERSION_CODES.LOLLIPOP ? tts.getFeatures(locale) + : tts.getVoice().getFeatures(); } + private void loadURL(WebView webView) { // We use JavaScript to get the content of the page conveniently, earlier making some // changes in the page