From 2ef69c1ee7dbbc3d5aed04edcde7767f47d8c928 Mon Sep 17 00:00:00 2001 From: Sean Mac Gillicuddy Date: Mon, 16 Dec 2019 13:16:28 +0000 Subject: [PATCH 1/5] #344 Broken rendering, mixing content beetween different (TED) ZIM files - clear cache on webview creation - some fixes to tabs - fixed bug where nightmode was activating when a new tab was opened --- .../core/extensions/ContextExtensions.kt | 9 ++++++ .../core/extensions/ImageViewExtensions.kt | 7 +++++ .../core/main/CoreMainActivity.java | 31 +++++-------------- .../kiwixmobile/core/main/KiwixWebView.java | 8 +++++ .../kiwixmobile/core/main/TabsAdapter.java | 5 ++- 5 files changed, 35 insertions(+), 25 deletions(-) diff --git a/core/src/main/java/org/kiwix/kiwixmobile/core/extensions/ContextExtensions.kt b/core/src/main/java/org/kiwix/kiwixmobile/core/extensions/ContextExtensions.kt index 1e0015e47..a6fee171f 100644 --- a/core/src/main/java/org/kiwix/kiwixmobile/core/extensions/ContextExtensions.kt +++ b/core/src/main/java/org/kiwix/kiwixmobile/core/extensions/ContextExtensions.kt @@ -23,7 +23,9 @@ import android.content.Intent import android.content.IntentFilter import android.os.Build.VERSION import android.os.Build.VERSION_CODES +import android.util.TypedValue import android.widget.Toast +import androidx.annotation.AttrRes import org.kiwix.kiwixmobile.core.base.BaseBroadcastReceiver import java.util.Locale @@ -54,3 +56,10 @@ val Context.locale: Locale get() = if (VERSION.SDK_INT >= VERSION_CODES.N) resources.configuration.locales.get(0) else resources.configuration.locale + +fun Context.getColorAttribute(@AttrRes attributeRes: Int) = with(TypedValue()) { + if (theme.resolveAttribute(attributeRes, this, true)) + data + else + throw RuntimeException("invalid attribute $attributeRes") +} diff --git a/core/src/main/java/org/kiwix/kiwixmobile/core/extensions/ImageViewExtensions.kt b/core/src/main/java/org/kiwix/kiwixmobile/core/extensions/ImageViewExtensions.kt index 7d3e1e723..791f42e61 100644 --- a/core/src/main/java/org/kiwix/kiwixmobile/core/extensions/ImageViewExtensions.kt +++ b/core/src/main/java/org/kiwix/kiwixmobile/core/extensions/ImageViewExtensions.kt @@ -18,7 +18,10 @@ package org.kiwix.kiwixmobile.core.extensions +import android.content.res.ColorStateList import android.widget.ImageView +import androidx.annotation.ColorInt +import androidx.core.widget.ImageViewCompat import org.kiwix.kiwixmobile.core.downloader.model.Base64String fun ImageView.setBitmap(base64String: Base64String) { @@ -36,3 +39,7 @@ fun ImageView.setBitmap(base64String: Base64String) { fun ImageView.setBitmapFromString(string: String?) { setBitmap(Base64String(string)) } + +fun ImageView.tint(@ColorInt colorId: Int) { + ImageViewCompat.setImageTintList(this, ColorStateList.valueOf(colorId)) +} diff --git a/core/src/main/java/org/kiwix/kiwixmobile/core/main/CoreMainActivity.java b/core/src/main/java/org/kiwix/kiwixmobile/core/main/CoreMainActivity.java index 1b733e837..d5d99ae53 100644 --- a/core/src/main/java/org/kiwix/kiwixmobile/core/main/CoreMainActivity.java +++ b/core/src/main/java/org/kiwix/kiwixmobile/core/main/CoreMainActivity.java @@ -43,7 +43,6 @@ import android.view.MenuItem; import android.view.View; import android.view.ViewGroup; import android.view.WindowManager; -import android.webkit.WebSettings; import android.webkit.WebView; import android.widget.Button; import android.widget.FrameLayout; @@ -356,7 +355,7 @@ public abstract class CoreMainActivity extends BaseActivity new BookOnDiskDelegate.BookDelegate(sharedPreferenceUtil, bookOnDiskItem -> { open(bookOnDiskItem); - getCurrentWebView().activateNightMode(); + updateNightMode(); return Unit.INSTANCE; }, null, @@ -769,7 +768,7 @@ public abstract class CoreMainActivity extends BaseActivity webViewList.add(webView); selectTab(webViewList.size() - 1); tabsAdapter.notifyDataSetChanged(); - setUpWebView(); + setUpWebViewWithTextToSpeech(); documentParser.initInterface(webView); return webView; } @@ -778,7 +777,7 @@ public abstract class CoreMainActivity extends BaseActivity KiwixWebView webView = getWebView(url); webViewList.add(webView); tabsAdapter.notifyDataSetChanged(); - setUpWebView(); + setUpWebViewWithTextToSpeech(); documentParser.initInterface(webView); } @@ -791,7 +790,7 @@ public abstract class CoreMainActivity extends BaseActivity .setAction(R.string.undo, v -> { webViewList.add(index, tempForUndo); tabsAdapter.notifyItemInserted(index); - setUpWebView(); + setUpWebViewWithTextToSpeech(); }) .show(); openHomeScreen(); @@ -1090,8 +1089,7 @@ public abstract class CoreMainActivity extends BaseActivity openZimFile(file); } scanStorageForZims(); - } - else { + } else { Snackbar.make(snackbarRoot, R.string.request_storage, Snackbar.LENGTH_LONG) .setAction(R.string.menu_settings, view -> { Intent intent = new Intent(); @@ -1192,7 +1190,7 @@ public abstract class CoreMainActivity extends BaseActivity webViewList.set(i, getWebView(webViewList.get(i).getUrl())); } selectTab(currentWebViewIndex); - setUpWebView(); + setUpWebViewWithTextToSpeech(); } presenter.loadCurrentZimBookmarksUrl(); @@ -1324,22 +1322,7 @@ public abstract class CoreMainActivity extends BaseActivity openArticle(articleUrl); } - private void setUpWebView() { - getCurrentWebView().getSettings().setJavaScriptEnabled(true); - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { - WebView.setWebContentsDebuggingEnabled(true); - } - - // webView.getSettings().setLoadsImagesAutomatically(false); - // Does not make much sense to cache data from zim files.(Not clear whether - // this actually has any effect) - getCurrentWebView().getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE); - - // Should basically resemble the behavior when setWebClient not done - // (i.p. internal urls load in webView, external urls in browser) - // as currently no custom setWebViewClient required it is commented - // However, it must notify the bookmark system when a page is finished loading - // so that it can refresh the menu. + private void setUpWebViewWithTextToSpeech() { tts.initWebView(getCurrentWebView()); } diff --git a/core/src/main/java/org/kiwix/kiwixmobile/core/main/KiwixWebView.java b/core/src/main/java/org/kiwix/kiwixmobile/core/main/KiwixWebView.java index 9f6f77393..20c0de6a1 100644 --- a/core/src/main/java/org/kiwix/kiwixmobile/core/main/KiwixWebView.java +++ b/core/src/main/java/org/kiwix/kiwixmobile/core/main/KiwixWebView.java @@ -18,6 +18,7 @@ package org.kiwix.kiwixmobile.core.main; +import android.annotation.SuppressLint; import android.app.Activity; import android.content.Context; import android.graphics.ColorMatrixColorFilter; @@ -41,6 +42,7 @@ import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import javax.inject.Inject; +import org.kiwix.kiwixmobile.core.BuildConfig; import org.kiwix.kiwixmobile.core.CoreApp; import org.kiwix.kiwixmobile.core.R; import org.kiwix.kiwixmobile.core.reader.ZimReaderContainer; @@ -66,15 +68,21 @@ public class KiwixWebView extends VideoEnabledWebView { super(context); } + @SuppressLint("SetJavaScriptEnabled") public KiwixWebView(Context context, WebViewCallback callback, AttributeSet attrs, ViewGroup nonVideoView, ViewGroup videoView, CoreWebViewClient webViewClient) { super(context, attrs); + if (BuildConfig.DEBUG == true && Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { + setWebContentsDebuggingEnabled(true); + } this.callback = callback; CoreApp.getCoreComponent().inject(this); // Set the user agent to the current locale so it can be read with navigator.userAgent final WebSettings settings = getSettings(); settings.setUserAgentString(LanguageUtils.getCurrentLocale(context).toString()); settings.setDomStorageEnabled(true); + settings.setJavaScriptEnabled(true); + clearCache(true); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) { settings.setAllowUniversalAccessFromFileURLs(true); } diff --git a/core/src/main/java/org/kiwix/kiwixmobile/core/main/TabsAdapter.java b/core/src/main/java/org/kiwix/kiwixmobile/core/main/TabsAdapter.java index 2755033a4..545fedcbc 100644 --- a/core/src/main/java/org/kiwix/kiwixmobile/core/main/TabsAdapter.java +++ b/core/src/main/java/org/kiwix/kiwixmobile/core/main/TabsAdapter.java @@ -32,6 +32,8 @@ import androidx.core.content.ContextCompat; import androidx.recyclerview.widget.RecyclerView; import java.util.List; import org.kiwix.kiwixmobile.core.R; +import org.kiwix.kiwixmobile.core.extensions.ContextExtensionsKt; +import org.kiwix.kiwixmobile.core.extensions.ImageViewExtensionsKt; import static org.kiwix.kiwixmobile.core.utils.DimenUtils.getToolbarHeight; import static org.kiwix.kiwixmobile.core.utils.DimenUtils.getWindowHeight; @@ -67,6 +69,8 @@ public class TabsAdapter extends RecyclerView.Adapter { ImageView close = new ImageView(context); close.setId(2); close.setImageDrawable(ContextCompat.getDrawable(context, R.drawable.ic_clear_white_24dp)); + ImageViewExtensionsKt.tint(close, + ContextExtensionsKt.getColorAttribute(context, R.attr.colorOnSurface)); CardView cardView = new CardView(context); cardView.setId(3); @@ -90,7 +94,6 @@ public class TabsAdapter extends RecyclerView.Adapter { textView.setId(4); textView.setMaxLines(1); textView.setEllipsize(TextUtils.TruncateAt.END); - textView.setTextColor(activity.getResources().getColor(R.color.white)); constraintLayout.addView(textView, new ConstraintLayout.LayoutParams(0, ConstraintLayout.LayoutParams.WRAP_CONTENT)); From b0560d349c1814bf48ee6454e953f656ad6692ea Mon Sep 17 00:00:00 2001 From: Sean Mac Gillicuddy Date: Mon, 16 Dec 2019 14:26:36 +0000 Subject: [PATCH 2/5] bump version to 3.1.2 --- app/build.gradle.kts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/build.gradle.kts b/app/build.gradle.kts index dfb99a18f..fef544bb5 100644 --- a/app/build.gradle.kts +++ b/app/build.gradle.kts @@ -11,7 +11,7 @@ apply(from = rootProject.file("jacoco.gradle")) ext { set("versionMajor", 3) set("versionMinor", 1) - set("versionPatch", 1) + set("versionPatch", 2) } fun generateVersionName() = "${ext["versionMajor"]}.${ext["versionMinor"]}.${ext["versionPatch"]}" From 599720c3cfff840dac8866f5f4802effe23e67e1 Mon Sep 17 00:00:00 2001 From: Sean Mac Gillicuddy Date: Mon, 16 Dec 2019 14:33:09 +0000 Subject: [PATCH 3/5] fix crash report --- .../core/main/CoreMainActivity.java | 20 ++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/core/src/main/java/org/kiwix/kiwixmobile/core/main/CoreMainActivity.java b/core/src/main/java/org/kiwix/kiwixmobile/core/main/CoreMainActivity.java index d5d99ae53..3ead9a4f5 100644 --- a/core/src/main/java/org/kiwix/kiwixmobile/core/main/CoreMainActivity.java +++ b/core/src/main/java/org/kiwix/kiwixmobile/core/main/CoreMainActivity.java @@ -494,16 +494,18 @@ public abstract class CoreMainActivity extends BaseActivity } protected void hideTabSwitcher() { - actionBar.setDisplayHomeAsUpEnabled(false); - actionBar.setDisplayShowTitleEnabled(true); + if (actionBar != null) { + actionBar.setDisplayHomeAsUpEnabled(false); + actionBar.setDisplayShowTitleEnabled(true); - drawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED); - closeAllTabsButton.setImageDrawable( - ContextCompat.getDrawable(this, R.drawable.ic_close_black_24dp)); - tabSwitcherRoot.setVisibility(View.GONE); - progressBar.setVisibility(View.VISIBLE); - contentFrame.setVisibility(View.VISIBLE); - mainMenu.showWebViewOptions(!urlIsInvalid()); + drawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED); + closeAllTabsButton.setImageDrawable( + ContextCompat.getDrawable(this, R.drawable.ic_close_black_24dp)); + tabSwitcherRoot.setVisibility(View.GONE); + progressBar.setVisibility(View.VISIBLE); + contentFrame.setVisibility(View.VISIBLE); + mainMenu.showWebViewOptions(!urlIsInvalid()); + } } @OnClick(R2.id.bottom_toolbar_arrow_back) From d6dc9b0c24918787a25c33263e3c4c919e05a8ad Mon Sep 17 00:00:00 2001 From: Sean Mac Gillicuddy Date: Mon, 16 Dec 2019 14:37:48 +0000 Subject: [PATCH 4/5] fix potential webview crash --- .../org/kiwix/kiwixmobile/core/base/BaseActivity.kt | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/core/src/main/java/org/kiwix/kiwixmobile/core/base/BaseActivity.kt b/core/src/main/java/org/kiwix/kiwixmobile/core/base/BaseActivity.kt index 5c9c39413..178219690 100644 --- a/core/src/main/java/org/kiwix/kiwixmobile/core/base/BaseActivity.kt +++ b/core/src/main/java/org/kiwix/kiwixmobile/core/base/BaseActivity.kt @@ -17,6 +17,8 @@ */ package org.kiwix.kiwixmobile.core.base +import android.content.res.Configuration +import android.os.Build import android.os.Bundle import androidx.annotation.LayoutRes import androidx.appcompat.app.AppCompatActivity @@ -49,6 +51,16 @@ abstract class BaseActivity : AppCompatActivity() { unbinder = ButterKnife.bind(this) } + // TODO https://issuetracker.google.com/issues/141132133 remove this once appcompat has been fixed + override fun applyOverrideConfiguration(overrideConfiguration: Configuration?) { + if (Build.VERSION.SDK_INT in Build.VERSION_CODES.LOLLIPOP..Build.VERSION_CODES.N_MR1 && + (resources.configuration.uiMode == applicationContext.resources.configuration.uiMode) + ) { + return + } + super.applyOverrideConfiguration(overrideConfiguration) + } + override fun onDestroy() { super.onDestroy() unbinder?.unbind() From ca8e9a8c3e818983047ce6219b1920ca03d005b6 Mon Sep 17 00:00:00 2001 From: Sean Mac Gillicuddy Date: Mon, 16 Dec 2019 14:43:30 +0000 Subject: [PATCH 5/5] fix NPE in SearchActivity --- .../org/kiwix/kiwixmobile/core/search/SearchActivity.java | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/core/src/main/java/org/kiwix/kiwixmobile/core/search/SearchActivity.java b/core/src/main/java/org/kiwix/kiwixmobile/core/search/SearchActivity.java index dacbf834c..decb30157 100644 --- a/core/src/main/java/org/kiwix/kiwixmobile/core/search/SearchActivity.java +++ b/core/src/main/java/org/kiwix/kiwixmobile/core/search/SearchActivity.java @@ -346,6 +346,8 @@ public class SearchActivity extends BaseActivity @Override protected void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); - outState.putString(EXTRA_SEARCH_TEXT, searchView.getQuery().toString()); + if (searchView != null && searchView.getQuery() != null) { + outState.putString(EXTRA_SEARCH_TEXT, searchView.getQuery().toString()); + } } }