diff --git a/src/org/kiwix/kiwixmobile/KiwixMobileActivity.java b/src/org/kiwix/kiwixmobile/KiwixMobileActivity.java index a89b4b7f6..dfffe4b16 100644 --- a/src/org/kiwix/kiwixmobile/KiwixMobileActivity.java +++ b/src/org/kiwix/kiwixmobile/KiwixMobileActivity.java @@ -63,6 +63,8 @@ import android.view.animation.AnimationUtils; import android.webkit.JavascriptInterface; import android.webkit.MimeTypeMap; import android.webkit.WebChromeClient; +import android.webkit.WebResourceRequest; +import android.webkit.WebResourceResponse; import android.webkit.WebSettings; import android.webkit.WebView; import android.webkit.WebViewClient; @@ -94,7 +96,9 @@ import org.kiwix.kiwixmobile.views.AnimatedProgressBar; import org.kiwix.kiwixmobile.views.CompatFindActionModeCallback; import org.kiwix.kiwixmobile.views.KiwixWebView; +import java.io.ByteArrayInputStream; import java.io.File; +import java.io.InputStream; import java.util.ArrayList; import java.util.HashMap; import java.util.List; @@ -1700,6 +1704,10 @@ public class KiwixMobileActivity extends AppCompatActivity { mAdapter = adapter; } + @Override + public void onLoadResource(WebView view, String url) { + Log.d("kiwix", url); + } @Override public boolean shouldOverrideUrlLoading(WebView view, String url) { diff --git a/src/org/kiwix/kiwixmobile/downloader/DownloadService.java b/src/org/kiwix/kiwixmobile/downloader/DownloadService.java index 12dbcc2ab..9b74629ed 100644 --- a/src/org/kiwix/kiwixmobile/downloader/DownloadService.java +++ b/src/org/kiwix/kiwixmobile/downloader/DownloadService.java @@ -1,6 +1,7 @@ package org.kiwix.kiwixmobile.downloader; import android.app.DownloadManager; +import android.app.Notification; import android.app.NotificationManager; import android.app.PendingIntent; import android.app.Service; @@ -24,7 +25,9 @@ import java.io.IOException; import java.io.RandomAccessFile; import java.util.ArrayList; import java.util.HashMap; +import java.util.Iterator; import java.util.List; +import java.util.Map; import java.util.Random; import okhttp3.OkHttpClient; @@ -57,7 +60,7 @@ public class DownloadService extends Service { public static int notificationCount = 1; public static ArrayList notifications = new ArrayList(); public String notificationTitle; - private NotificationCompat.Builder notification; + private HashMap notification = new HashMap<>(); private NotificationManager notificationManager; public HashMap downloadStatus = new HashMap(); public HashMap downloadProgress = new HashMap(); @@ -87,14 +90,13 @@ public class DownloadService extends Service { PendingIntent pendingIntent = PendingIntent.getActivity(getBaseContext(), 0, target, PendingIntent.FLAG_CANCEL_CURRENT); - notification = new NotificationCompat.Builder(this) + notification.put(notificationCount , new NotificationCompat.Builder(this) .setContentTitle(getResources().getString(R.string.zim_file_downloading) + " " + notificationTitle) .setProgress(100, 0, false) .setSmallIcon(R.drawable.kiwix_notification) .setColor(Color.BLACK) .setContentIntent(pendingIntent) - .setOngoing(true); - + .setOngoing(true)); downloadStatus.put(notificationCount, 0); String url = intent.getExtras().getString(DownloadIntent.DOWNLOAD_URL_PARAMETER); @@ -108,6 +110,7 @@ public class DownloadService extends Service { pauseLock.notify(); } notificationManager.cancel(notificationID); + updateForeground(); } public void pauseDownload(int notificationID) { @@ -131,13 +134,17 @@ public class DownloadService extends Service { .distinctUntilChanged() .subscribe(progress -> { if (progress == 100) { - notification.setOngoing(false); - notification.setContentTitle(notificationTitle + " " + getResources().getString(R.string.zim_file_downloaded)); + notification.get(notificationID).setOngoing(false); + notification.get(notificationID).setContentTitle(notificationTitle + " " + getResources().getString(R.string.zim_file_downloaded)); book.downloaded = true; bookDao.saveBook(book); + updateForeground(); + } else if (progress == 0) { + // Tells android to not kill the service + startForeground(notificationCount, notification.get(notificationCount).build()); } - notification.setProgress(100, progress, false); - notificationManager.notify(notificationID, notification.build()); + notification.get(notificationID).setProgress(100, progress, false); + notificationManager.notify(notificationID, notification.get(notificationID).build()); if (DownloadFragment.mDownloads != null && DownloadFragment.mDownloads.get(notificationID) != null) { handler.post(new Runnable() { @Override @@ -148,11 +155,21 @@ public class DownloadService extends Service { } }); } - stopForeground(true); - stopSelf(); }, Throwable::printStackTrace); } + private void updateForeground() { + // Allow notification to be dismissible while ensuring integrity of service if active downloads + stopForeground(true); + Iterator it = downloadStatus.entrySet().iterator(); + while (it.hasNext()){ + Map.Entry pair = (Map.Entry) it.next(); + if ((int) pair.getValue() != 4 && (int) pair.getValue() != 2 ){ + startForeground( (int) pair.getKey(), notification.get(pair.getKey()).build()); + } + } + } + private Observable> getMetaLinkContentLength(String url) { return Observable.create(subscriber -> { if (subscriber.isUnsubscribed()) return;