Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#2303 Impossible to surf on 3D printing SE ZIM file #2494

Merged
Prev Previous commit
Next Next commit
#2303 Impossible to surf on 3D printing SE ZIM file - opening a zim f…
…ile unregisters before loading
  • Loading branch information
macgills committed Nov 13, 2020
commit 1481752f3f35e06a4ed89694ca21a3bc4735e7fa
Original file line number Diff line number Diff line change
Expand Up @@ -119,11 +119,11 @@
import org.kiwix.kiwixmobile.core.utils.dialog.KiwixDialog;
import org.kiwix.kiwixmobile.core.utils.files.FileUtils;

import static android.content.ContentValues.TAG;
import static android.content.pm.PackageManager.PERMISSION_GRANTED;
import static org.kiwix.kiwixmobile.core.base.FragmentActivityExtensions.Super.ShouldCall;
import static org.kiwix.kiwixmobile.core.base.FragmentActivityExtensions.Super.ShouldNotCall;
import static org.kiwix.kiwixmobile.core.downloader.fetch.FetchDownloadNotificationManagerKt.DOWNLOAD_NOTIFICATION_TITLE;
import static org.kiwix.kiwixmobile.core.main.ServiceWorkerUninitialiserKt.UNINITIALISER_ADDRESS;
import static org.kiwix.kiwixmobile.core.page.history.adapter.HistoryListItem.HistoryItem;
import static org.kiwix.kiwixmobile.core.utils.AnimationUtils.rotate;
import static org.kiwix.kiwixmobile.core.utils.ConstantsKt.REQUEST_STORAGE_PERMISSION;
Expand Down Expand Up @@ -742,7 +742,6 @@ protected void loadUrl(String url, KiwixWebView webview) {
}
}


private KiwixWebView initalizeWebView(String url) {
AttributeSet attrs = StyleUtils.getAttributes(requireActivity(), R.xml.webview);
KiwixWebView webView = createWebView(attrs);
Expand All @@ -769,6 +768,10 @@ protected KiwixWebView newTab(String url) {
tabsAdapter.notifyDataSetChanged();
setUpWebViewWithTextToSpeech();
documentParser.initInterface(webView);
new ServiceWorkerUninitialiser(() -> {
openMainPage();
return Unit.INSTANCE;
}).initInterface(webView);
return webView;
}

Expand Down Expand Up @@ -965,7 +968,6 @@ boolean goToBookmarks() {
return true;
}


@Override public void onFullscreenVideoToggled(boolean isFullScreen) {
// does nothing because custom doesn't have a nav bar
}
Expand Down Expand Up @@ -1045,7 +1047,7 @@ private void openAndSetInContainer(File file) {
if (mainMenu != null) {
mainMenu.onFileOpened(urlIsValid());
}
openMainPage();
openUninitialiserPage();
safeDispose();
bookmarkingDisposable = Flowable.combineLatest(
newBookmarksDao.bookmarkUrlsForCurrentBook(zimFileReader),
Expand All @@ -1065,6 +1067,10 @@ private void openAndSetInContainer(File file) {
}
}

private void openUninitialiserPage() {
openArticle(UNINITIALISER_ADDRESS);
}

private void safeDispose() {
if (bookmarkingDisposable != null) {
bookmarkingDisposable.dispose();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
* Kiwix Android
* Copyright (c) 2020 Kiwix <android.kiwix.org>
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/

package org.kiwix.kiwixmobile.core.main

import android.os.Handler
import android.os.Looper
import android.webkit.JavascriptInterface
import android.webkit.WebView

const val UNINITIALISER_ADDRESS = "A/remove_service_workers.html"
const val UNINITIALISE_HTML = """
<html>
<head>
<title>...</title>
<script type="text/javascript">console.log("** INSIDE BLANK **");
function do_unregister() {
if (!navigator.serviceWorker) {
return;
}
navigator.serviceWorker.getRegistrations().then(async function (registrations) {
if (registrations.length) {
console.debug('we do have ' + registrations.length + ' registration(s)');
var registration = registrations[0];
registration.unregister()
.then(function (success) { ServiceWorkerUninitialiser.onUninitialised();})
.catch(function (e) {alert("ERR:" + e)});
}
else {
ServiceWorkerUninitialiser.onUninitialised();
}
});
}
do_unregister();
</script>
</head>
<h1>---</h1>
macgills marked this conversation as resolved.
Show resolved Hide resolved
</html>
"""

class ServiceWorkerUninitialiser(val onUninitialisedAction: () -> Unit) {

@JavascriptInterface
fun onUninitialised() {
Handler(Looper.getMainLooper()).post {
onUninitialisedAction()
}
}

fun initInterface(webView: WebView) {
webView.addJavascriptInterface(this, "ServiceWorkerUninitialiser")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ import org.kiwix.kiwixlib.Pair
import org.kiwix.kiwixmobile.core.CoreApp
import org.kiwix.kiwixmobile.core.NightModeConfig
import org.kiwix.kiwixmobile.core.entity.LibraryNetworkEntity.Book
import org.kiwix.kiwixmobile.core.main.UNINITIALISER_ADDRESS
import org.kiwix.kiwixmobile.core.main.UNINITIALISE_HTML
import org.kiwix.kiwixmobile.core.reader.ZimFileReader.Companion.CONTENT_PREFIX
import org.kiwix.kiwixmobile.core.search.SearchSuggestion
import org.kiwix.kiwixmobile.core.utils.files.FileUtils
Expand Down Expand Up @@ -136,7 +138,10 @@ class ZimFileReader constructor(
fun getRedirect(url: String) = "${toRedirect(url)}"

fun isRedirect(url: String) =
url.startsWith(CONTENT_PREFIX) && url != getRedirect(url)
when {
url.endsWith(UNINITIALISER_ADDRESS) -> false
else -> url.startsWith(CONTENT_PREFIX) && url != getRedirect(url)
}

private fun toRedirect(url: String) =
"$CONTENT_PREFIX${jniKiwixReader.checkUrl(url.toUri().filePath)}".toUri()
Expand Down Expand Up @@ -176,11 +181,15 @@ class ZimFileReader constructor(
Completable.fromAction {
try {
outputStream.use {
getContentAndMimeType(uri).let { (content: ByteArray, mimeType: String) ->
if ("text/css" == mimeType && nightModeConfig.isNightModeActive()) {
it.write(INVERT_IMAGES_VIDEO.toByteArray(Charsets.UTF_8))
if (uri.endsWith(UNINITIALISER_ADDRESS)) {
it.write(UNINITIALISE_HTML.toByteArray())
} else {
getContentAndMimeType(uri).let { (content: ByteArray, mimeType: String) ->
if ("text/css" == mimeType && nightModeConfig.isNightModeActive()) {
it.write(INVERT_IMAGES_VIDEO.toByteArray())
}
it.write(content)
}
it.write(content)
}
}
} catch (ioException: IOException) {
Expand Down