mirror of
https://github.com/kiwix/kiwix-android.git
synced 2025-09-11 16:37:18 -04:00
Android Download service should not be killed by system #261
This commit is contained in:
parent
5333706f7d
commit
ce7aee791d
@ -63,6 +63,8 @@ import android.view.animation.AnimationUtils;
|
|||||||
import android.webkit.JavascriptInterface;
|
import android.webkit.JavascriptInterface;
|
||||||
import android.webkit.MimeTypeMap;
|
import android.webkit.MimeTypeMap;
|
||||||
import android.webkit.WebChromeClient;
|
import android.webkit.WebChromeClient;
|
||||||
|
import android.webkit.WebResourceRequest;
|
||||||
|
import android.webkit.WebResourceResponse;
|
||||||
import android.webkit.WebSettings;
|
import android.webkit.WebSettings;
|
||||||
import android.webkit.WebView;
|
import android.webkit.WebView;
|
||||||
import android.webkit.WebViewClient;
|
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.CompatFindActionModeCallback;
|
||||||
import org.kiwix.kiwixmobile.views.KiwixWebView;
|
import org.kiwix.kiwixmobile.views.KiwixWebView;
|
||||||
|
|
||||||
|
import java.io.ByteArrayInputStream;
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
|
import java.io.InputStream;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
@ -1700,6 +1704,10 @@ public class KiwixMobileActivity extends AppCompatActivity {
|
|||||||
mAdapter = adapter;
|
mAdapter = adapter;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onLoadResource(WebView view, String url) {
|
||||||
|
Log.d("kiwix", url);
|
||||||
|
}
|
||||||
@Override
|
@Override
|
||||||
public boolean shouldOverrideUrlLoading(WebView view, String url) {
|
public boolean shouldOverrideUrlLoading(WebView view, String url) {
|
||||||
|
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
package org.kiwix.kiwixmobile.downloader;
|
package org.kiwix.kiwixmobile.downloader;
|
||||||
|
|
||||||
import android.app.DownloadManager;
|
import android.app.DownloadManager;
|
||||||
|
import android.app.Notification;
|
||||||
import android.app.NotificationManager;
|
import android.app.NotificationManager;
|
||||||
import android.app.PendingIntent;
|
import android.app.PendingIntent;
|
||||||
import android.app.Service;
|
import android.app.Service;
|
||||||
@ -24,7 +25,9 @@ import java.io.IOException;
|
|||||||
import java.io.RandomAccessFile;
|
import java.io.RandomAccessFile;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
|
import java.util.Iterator;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
import java.util.Random;
|
import java.util.Random;
|
||||||
|
|
||||||
import okhttp3.OkHttpClient;
|
import okhttp3.OkHttpClient;
|
||||||
@ -57,7 +60,7 @@ public class DownloadService extends Service {
|
|||||||
public static int notificationCount = 1;
|
public static int notificationCount = 1;
|
||||||
public static ArrayList<String> notifications = new ArrayList<String>();
|
public static ArrayList<String> notifications = new ArrayList<String>();
|
||||||
public String notificationTitle;
|
public String notificationTitle;
|
||||||
private NotificationCompat.Builder notification;
|
private HashMap<Integer, NotificationCompat.Builder> notification = new HashMap<>();
|
||||||
private NotificationManager notificationManager;
|
private NotificationManager notificationManager;
|
||||||
public HashMap<Integer, Integer> downloadStatus = new HashMap<Integer, Integer>();
|
public HashMap<Integer, Integer> downloadStatus = new HashMap<Integer, Integer>();
|
||||||
public HashMap<Integer, Integer> downloadProgress = new HashMap<Integer, Integer>();
|
public HashMap<Integer, Integer> downloadProgress = new HashMap<Integer, Integer>();
|
||||||
@ -87,14 +90,13 @@ public class DownloadService extends Service {
|
|||||||
PendingIntent pendingIntent = PendingIntent.getActivity(getBaseContext(), 0,
|
PendingIntent pendingIntent = PendingIntent.getActivity(getBaseContext(), 0,
|
||||||
target, PendingIntent.FLAG_CANCEL_CURRENT);
|
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)
|
.setContentTitle(getResources().getString(R.string.zim_file_downloading) + " " + notificationTitle)
|
||||||
.setProgress(100, 0, false)
|
.setProgress(100, 0, false)
|
||||||
.setSmallIcon(R.drawable.kiwix_notification)
|
.setSmallIcon(R.drawable.kiwix_notification)
|
||||||
.setColor(Color.BLACK)
|
.setColor(Color.BLACK)
|
||||||
.setContentIntent(pendingIntent)
|
.setContentIntent(pendingIntent)
|
||||||
.setOngoing(true);
|
.setOngoing(true));
|
||||||
|
|
||||||
|
|
||||||
downloadStatus.put(notificationCount, 0);
|
downloadStatus.put(notificationCount, 0);
|
||||||
String url = intent.getExtras().getString(DownloadIntent.DOWNLOAD_URL_PARAMETER);
|
String url = intent.getExtras().getString(DownloadIntent.DOWNLOAD_URL_PARAMETER);
|
||||||
@ -108,6 +110,7 @@ public class DownloadService extends Service {
|
|||||||
pauseLock.notify();
|
pauseLock.notify();
|
||||||
}
|
}
|
||||||
notificationManager.cancel(notificationID);
|
notificationManager.cancel(notificationID);
|
||||||
|
updateForeground();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void pauseDownload(int notificationID) {
|
public void pauseDownload(int notificationID) {
|
||||||
@ -131,13 +134,17 @@ public class DownloadService extends Service {
|
|||||||
.distinctUntilChanged()
|
.distinctUntilChanged()
|
||||||
.subscribe(progress -> {
|
.subscribe(progress -> {
|
||||||
if (progress == 100) {
|
if (progress == 100) {
|
||||||
notification.setOngoing(false);
|
notification.get(notificationID).setOngoing(false);
|
||||||
notification.setContentTitle(notificationTitle + " " + getResources().getString(R.string.zim_file_downloaded));
|
notification.get(notificationID).setContentTitle(notificationTitle + " " + getResources().getString(R.string.zim_file_downloaded));
|
||||||
book.downloaded = true;
|
book.downloaded = true;
|
||||||
bookDao.saveBook(book);
|
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);
|
notification.get(notificationID).setProgress(100, progress, false);
|
||||||
notificationManager.notify(notificationID, notification.build());
|
notificationManager.notify(notificationID, notification.get(notificationID).build());
|
||||||
if (DownloadFragment.mDownloads != null && DownloadFragment.mDownloads.get(notificationID) != null) {
|
if (DownloadFragment.mDownloads != null && DownloadFragment.mDownloads.get(notificationID) != null) {
|
||||||
handler.post(new Runnable() {
|
handler.post(new Runnable() {
|
||||||
@Override
|
@Override
|
||||||
@ -148,11 +155,21 @@ public class DownloadService extends Service {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
stopForeground(true);
|
|
||||||
stopSelf();
|
|
||||||
}, Throwable::printStackTrace);
|
}, 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<Pair<String, Long>> getMetaLinkContentLength(String url) {
|
private Observable<Pair<String, Long>> getMetaLinkContentLength(String url) {
|
||||||
return Observable.create(subscriber -> {
|
return Observable.create(subscriber -> {
|
||||||
if (subscriber.isUnsubscribed()) return;
|
if (subscriber.isUnsubscribed()) return;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user