Merge pull request #1645 from kiwix/feature/macgills/344-mixed-rendering

#344 Broken rendering, mixing content beetween different (TED) ZIM fi…
This commit is contained in:
Seán Mac Gillicuddy 2019-12-16 14:21:59 +00:00 committed by GitHub
commit e2d4bc7e4c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 35 additions and 25 deletions

View File

@ -23,7 +23,9 @@ import android.content.Intent
import android.content.IntentFilter import android.content.IntentFilter
import android.os.Build.VERSION import android.os.Build.VERSION
import android.os.Build.VERSION_CODES import android.os.Build.VERSION_CODES
import android.util.TypedValue
import android.widget.Toast import android.widget.Toast
import androidx.annotation.AttrRes
import org.kiwix.kiwixmobile.core.base.BaseBroadcastReceiver import org.kiwix.kiwixmobile.core.base.BaseBroadcastReceiver
import java.util.Locale import java.util.Locale
@ -54,3 +56,10 @@ val Context.locale: Locale
get() = get() =
if (VERSION.SDK_INT >= VERSION_CODES.N) resources.configuration.locales.get(0) if (VERSION.SDK_INT >= VERSION_CODES.N) resources.configuration.locales.get(0)
else resources.configuration.locale 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")
}

View File

@ -18,7 +18,10 @@
package org.kiwix.kiwixmobile.core.extensions package org.kiwix.kiwixmobile.core.extensions
import android.content.res.ColorStateList
import android.widget.ImageView import android.widget.ImageView
import androidx.annotation.ColorInt
import androidx.core.widget.ImageViewCompat
import org.kiwix.kiwixmobile.core.downloader.model.Base64String import org.kiwix.kiwixmobile.core.downloader.model.Base64String
fun ImageView.setBitmap(base64String: Base64String) { fun ImageView.setBitmap(base64String: Base64String) {
@ -36,3 +39,7 @@ fun ImageView.setBitmap(base64String: Base64String) {
fun ImageView.setBitmapFromString(string: String?) { fun ImageView.setBitmapFromString(string: String?) {
setBitmap(Base64String(string)) setBitmap(Base64String(string))
} }
fun ImageView.tint(@ColorInt colorId: Int) {
ImageViewCompat.setImageTintList(this, ColorStateList.valueOf(colorId))
}

View File

@ -43,7 +43,6 @@ import android.view.MenuItem;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
import android.view.WindowManager; import android.view.WindowManager;
import android.webkit.WebSettings;
import android.webkit.WebView; import android.webkit.WebView;
import android.widget.Button; import android.widget.Button;
import android.widget.FrameLayout; import android.widget.FrameLayout;
@ -356,7 +355,7 @@ public abstract class CoreMainActivity extends BaseActivity
new BookOnDiskDelegate.BookDelegate(sharedPreferenceUtil, new BookOnDiskDelegate.BookDelegate(sharedPreferenceUtil,
bookOnDiskItem -> { bookOnDiskItem -> {
open(bookOnDiskItem); open(bookOnDiskItem);
getCurrentWebView().activateNightMode(); updateNightMode();
return Unit.INSTANCE; return Unit.INSTANCE;
}, },
null, null,
@ -769,7 +768,7 @@ public abstract class CoreMainActivity extends BaseActivity
webViewList.add(webView); webViewList.add(webView);
selectTab(webViewList.size() - 1); selectTab(webViewList.size() - 1);
tabsAdapter.notifyDataSetChanged(); tabsAdapter.notifyDataSetChanged();
setUpWebView(); setUpWebViewWithTextToSpeech();
documentParser.initInterface(webView); documentParser.initInterface(webView);
return webView; return webView;
} }
@ -778,7 +777,7 @@ public abstract class CoreMainActivity extends BaseActivity
KiwixWebView webView = getWebView(url); KiwixWebView webView = getWebView(url);
webViewList.add(webView); webViewList.add(webView);
tabsAdapter.notifyDataSetChanged(); tabsAdapter.notifyDataSetChanged();
setUpWebView(); setUpWebViewWithTextToSpeech();
documentParser.initInterface(webView); documentParser.initInterface(webView);
} }
@ -791,7 +790,7 @@ public abstract class CoreMainActivity extends BaseActivity
.setAction(R.string.undo, v -> { .setAction(R.string.undo, v -> {
webViewList.add(index, tempForUndo); webViewList.add(index, tempForUndo);
tabsAdapter.notifyItemInserted(index); tabsAdapter.notifyItemInserted(index);
setUpWebView(); setUpWebViewWithTextToSpeech();
}) })
.show(); .show();
openHomeScreen(); openHomeScreen();
@ -1090,8 +1089,7 @@ public abstract class CoreMainActivity extends BaseActivity
openZimFile(file); openZimFile(file);
} }
scanStorageForZims(); scanStorageForZims();
} } else {
else {
Snackbar.make(snackbarRoot, R.string.request_storage, Snackbar.LENGTH_LONG) Snackbar.make(snackbarRoot, R.string.request_storage, Snackbar.LENGTH_LONG)
.setAction(R.string.menu_settings, view -> { .setAction(R.string.menu_settings, view -> {
Intent intent = new Intent(); Intent intent = new Intent();
@ -1192,7 +1190,7 @@ public abstract class CoreMainActivity extends BaseActivity
webViewList.set(i, getWebView(webViewList.get(i).getUrl())); webViewList.set(i, getWebView(webViewList.get(i).getUrl()));
} }
selectTab(currentWebViewIndex); selectTab(currentWebViewIndex);
setUpWebView(); setUpWebViewWithTextToSpeech();
} }
presenter.loadCurrentZimBookmarksUrl(); presenter.loadCurrentZimBookmarksUrl();
@ -1324,22 +1322,7 @@ public abstract class CoreMainActivity extends BaseActivity
openArticle(articleUrl); openArticle(articleUrl);
} }
private void setUpWebView() { private void setUpWebViewWithTextToSpeech() {
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.
tts.initWebView(getCurrentWebView()); tts.initWebView(getCurrentWebView());
} }

View File

@ -18,6 +18,7 @@
package org.kiwix.kiwixmobile.core.main; package org.kiwix.kiwixmobile.core.main;
import android.annotation.SuppressLint;
import android.app.Activity; import android.app.Activity;
import android.content.Context; import android.content.Context;
import android.graphics.ColorMatrixColorFilter; import android.graphics.ColorMatrixColorFilter;
@ -41,6 +42,7 @@ import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.OutputStream; import java.io.OutputStream;
import javax.inject.Inject; import javax.inject.Inject;
import org.kiwix.kiwixmobile.core.BuildConfig;
import org.kiwix.kiwixmobile.core.CoreApp; import org.kiwix.kiwixmobile.core.CoreApp;
import org.kiwix.kiwixmobile.core.R; import org.kiwix.kiwixmobile.core.R;
import org.kiwix.kiwixmobile.core.reader.ZimReaderContainer; import org.kiwix.kiwixmobile.core.reader.ZimReaderContainer;
@ -66,15 +68,21 @@ public class KiwixWebView extends VideoEnabledWebView {
super(context); super(context);
} }
@SuppressLint("SetJavaScriptEnabled")
public KiwixWebView(Context context, WebViewCallback callback, AttributeSet attrs, public KiwixWebView(Context context, WebViewCallback callback, AttributeSet attrs,
ViewGroup nonVideoView, ViewGroup videoView, CoreWebViewClient webViewClient) { ViewGroup nonVideoView, ViewGroup videoView, CoreWebViewClient webViewClient) {
super(context, attrs); super(context, attrs);
if (BuildConfig.DEBUG == true && Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
setWebContentsDebuggingEnabled(true);
}
this.callback = callback; this.callback = callback;
CoreApp.getCoreComponent().inject(this); CoreApp.getCoreComponent().inject(this);
// Set the user agent to the current locale so it can be read with navigator.userAgent // Set the user agent to the current locale so it can be read with navigator.userAgent
final WebSettings settings = getSettings(); final WebSettings settings = getSettings();
settings.setUserAgentString(LanguageUtils.getCurrentLocale(context).toString()); settings.setUserAgentString(LanguageUtils.getCurrentLocale(context).toString());
settings.setDomStorageEnabled(true); settings.setDomStorageEnabled(true);
settings.setJavaScriptEnabled(true);
clearCache(true);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
settings.setAllowUniversalAccessFromFileURLs(true); settings.setAllowUniversalAccessFromFileURLs(true);
} }

View File

@ -32,6 +32,8 @@ import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.RecyclerView; import androidx.recyclerview.widget.RecyclerView;
import java.util.List; import java.util.List;
import org.kiwix.kiwixmobile.core.R; 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.getToolbarHeight;
import static org.kiwix.kiwixmobile.core.utils.DimenUtils.getWindowHeight; import static org.kiwix.kiwixmobile.core.utils.DimenUtils.getWindowHeight;
@ -67,6 +69,8 @@ public class TabsAdapter extends RecyclerView.Adapter<TabsAdapter.ViewHolder> {
ImageView close = new ImageView(context); ImageView close = new ImageView(context);
close.setId(2); close.setId(2);
close.setImageDrawable(ContextCompat.getDrawable(context, R.drawable.ic_clear_white_24dp)); 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 cardView = new CardView(context);
cardView.setId(3); cardView.setId(3);
@ -90,7 +94,6 @@ public class TabsAdapter extends RecyclerView.Adapter<TabsAdapter.ViewHolder> {
textView.setId(4); textView.setId(4);
textView.setMaxLines(1); textView.setMaxLines(1);
textView.setEllipsize(TextUtils.TruncateAt.END); textView.setEllipsize(TextUtils.TruncateAt.END);
textView.setTextColor(activity.getResources().getColor(R.color.white));
constraintLayout.addView(textView, constraintLayout.addView(textView,
new ConstraintLayout.LayoutParams(0, ConstraintLayout.LayoutParams.WRAP_CONTENT)); new ConstraintLayout.LayoutParams(0, ConstraintLayout.LayoutParams.WRAP_CONTENT));