Download service is correctly restared on crash

This commit is contained in:
mhutti1 2017-08-12 16:00:48 -04:00
parent 8a3fa8153a
commit a4e13b591b
3 changed files with 46 additions and 19 deletions

View File

@ -3,6 +3,7 @@ package org.kiwix.kiwixmobile.downloader;
import android.app.Activity; import android.app.Activity;
import android.app.AlertDialog; import android.app.AlertDialog;
import android.app.Application;
import android.content.Context; import android.content.Context;
import android.graphics.Bitmap; import android.graphics.Bitmap;
import android.graphics.BitmapFactory; import android.graphics.BitmapFactory;
@ -24,6 +25,8 @@ import android.widget.ProgressBar;
import android.widget.RelativeLayout; import android.widget.RelativeLayout;
import android.widget.TextView; import android.widget.TextView;
import java.util.Locale;
import org.kiwix.kiwixmobile.KiwixApplication;
import org.kiwix.kiwixmobile.KiwixMobileActivity; import org.kiwix.kiwixmobile.KiwixMobileActivity;
import org.kiwix.kiwixmobile.R; import org.kiwix.kiwixmobile.R;
import org.kiwix.kiwixmobile.library.entity.LibraryNetworkEntity; import org.kiwix.kiwixmobile.library.entity.LibraryNetworkEntity;
@ -97,18 +100,26 @@ public class DownloadFragment extends Fragment {
.show(); .show();
} }
public String toHumanReadableTime(int seconds) { public static String toHumanReadableTime(int seconds) {
final double MINUTES = 60; final double MINUTES = 60;
final double HOURS = 60 * MINUTES; final double HOURS = 60 * MINUTES;
final double DAYS = 24 * HOURS; final double DAYS = 24 * HOURS;
if (Math.round(seconds / DAYS) > 0) if (Math.round(seconds / DAYS) > 0)
return String.format("%d %s %s", Math.round(seconds / DAYS), getString(R.string.time_day), getString(R.string.time_left)); return String.format(Locale.getDefault(), "%d %s %s", Math.round(seconds / DAYS),
KiwixApplication.getInstance().getResources().getString(R.string.time_day),
KiwixApplication.getInstance().getResources().getString(R.string.time_left));
if (Math.round(seconds / HOURS) > 0) if (Math.round(seconds / HOURS) > 0)
return String.format("%d %s %s", Math.round(seconds / HOURS), getString(R.string.time_hour), getString(R.string.time_left)); return String.format(Locale.getDefault(), "%d %s %s", Math.round(seconds / HOURS),
KiwixApplication.getInstance().getResources().getString(R.string.time_hour),
KiwixApplication.getInstance().getResources().getString(R.string.time_left));
if (Math.round(seconds / MINUTES) > 0) if (Math.round(seconds / MINUTES) > 0)
return String.format("%d %s %s", Math.round(seconds / MINUTES), getString(R.string.time_minute), getString(R.string.time_left)); return String.format(Locale.getDefault(), "%d %s %s", Math.round(seconds / MINUTES),
return String.format("%d %s %s", seconds, getString(R.string.time_second), getString(R.string.time_left)); KiwixApplication.getInstance().getResources().getString(R.string.time_minute),
KiwixApplication.getInstance().getResources().getString(R.string.time_left));
return String.format(Locale.getDefault(), "%d %s %s", seconds,
KiwixApplication.getInstance().getResources().getString(R.string.time_second),
KiwixApplication.getInstance().getResources().getString(R.string.time_left));
} }
public class DownloadAdapter extends BaseAdapter { public class DownloadAdapter extends BaseAdapter {

View File

@ -161,7 +161,7 @@ public class DownloadService extends Service {
LibraryFragment.downloadingBooks.remove(book); LibraryFragment.downloadingBooks.remove(book);
String url = intent.getExtras().getString(DownloadIntent.DOWNLOAD_URL_PARAMETER); String url = intent.getExtras().getString(DownloadIntent.DOWNLOAD_URL_PARAMETER);
downloadBook(url, notificationCount, book); downloadBook(url, notificationCount, book);
return START_STICKY; return START_REDELIVER_INTENT;
} }
public void stopDownload(int notificationID) { public void stopDownload(int notificationID) {
@ -169,9 +169,11 @@ public class DownloadService extends Service {
synchronized (pauseLock) { synchronized (pauseLock) {
pauseLock.notify(); pauseLock.notify();
} }
DownloadFragment.mDownloads.remove(notificationID); if (!DownloadFragment.mDownloads.isEmpty()) {
DownloadFragment.mDownloadFiles.remove(notificationID); DownloadFragment.mDownloads.remove(notificationID);
DownloadFragment.downloadAdapter.notifyDataSetChanged(); DownloadFragment.mDownloadFiles.remove(notificationID);
DownloadFragment.downloadAdapter.notifyDataSetChanged();
}
updateForeground(); updateForeground();
notificationManager.cancel(notificationID); notificationManager.cancel(notificationID);
} }
@ -210,8 +212,10 @@ public class DownloadService extends Service {
notification.get(notificationID).mActions.get(0).icon = R.drawable.ic_play_arrow_black_24dp; notification.get(notificationID).mActions.get(0).icon = R.drawable.ic_play_arrow_black_24dp;
notification.get(notificationID).setContentText(getString(R.string.download_paused)); notification.get(notificationID).setContentText(getString(R.string.download_paused));
notificationManager.notify(notificationID, notification.get(notificationID).build()); notificationManager.notify(notificationID, notification.get(notificationID).build());
DownloadFragment.downloadAdapter.notifyDataSetChanged(); if (DownloadFragment.downloadAdapter != null) {
downloadFragment.listView.invalidateViews(); DownloadFragment.downloadAdapter.notifyDataSetChanged();
downloadFragment.listView.invalidateViews();
}
} }
public boolean playDownload(int notificationID) { public boolean playDownload(int notificationID) {
@ -223,14 +227,19 @@ public class DownloadService extends Service {
notification.get(notificationID).mActions.get(0).icon = R.drawable.ic_pause_black_24dp; notification.get(notificationID).mActions.get(0).icon = R.drawable.ic_pause_black_24dp;
notification.get(notificationID).setContentText(""); notification.get(notificationID).setContentText("");
notificationManager.notify(notificationID, notification.get(notificationID).build()); notificationManager.notify(notificationID, notification.get(notificationID).build());
DownloadFragment.downloadAdapter.notifyDataSetChanged(); if (DownloadFragment.downloadAdapter != null) {
downloadFragment.listView.invalidateViews(); DownloadFragment.downloadAdapter.notifyDataSetChanged();
downloadFragment.listView.invalidateViews();
}
return true; return true;
} }
private void downloadBook(String url, int notificationID, LibraryNetworkEntity.Book book) { private void downloadBook(String url, int notificationID, LibraryNetworkEntity.Book book) {
downloadFragment.addDownload(notificationID, book, KIWIX_ROOT + StorageUtils.getFileNameFromUrl(book.getUrl())); if (downloadFragment != null) {
downloadFragment.addDownload(notificationID, book,
KIWIX_ROOT + StorageUtils.getFileNameFromUrl(book.getUrl()));
}
TestingUtils.bindResource(DownloadService.class); TestingUtils.bindResource(DownloadService.class);
if (book.file != null && (book.file.exists() || new File(book.file.getPath() + ".part").exists())) { if (book.file != null && (book.file.exists() || new File(book.file.getPath() + ".part").exists())) {
// Calculate initial download progress // Calculate initial download progress
@ -264,7 +273,7 @@ public class DownloadService extends Service {
} }
notification.get(notificationID).setProgress(100, progress, false); notification.get(notificationID).setProgress(100, progress, false);
if (progress != 100 && timeRemaining.get(notificationID) != -1) if (progress != 100 && timeRemaining.get(notificationID) != -1)
notification.get(notificationID).setContentText(downloadFragment.toHumanReadableTime(timeRemaining.get(notificationID))); notification.get(notificationID).setContentText(DownloadFragment.toHumanReadableTime(timeRemaining.get(notificationID)));
notificationManager.notify(notificationID, notification.get(notificationID).build()); notificationManager.notify(notificationID, notification.get(notificationID).build());
if (progress == 0 || progress == 100) { if (progress == 0 || progress == 100) {
// Tells android to not kill the service // Tells android to not kill the service
@ -344,10 +353,13 @@ public class DownloadService extends Service {
downloaded += output.length(); downloaded += output.length();
if (chunk.getStartByte() == 0) { if (chunk.getStartByte() == 0) {
LibraryNetworkEntity.Book book = DownloadFragment.mDownloads.get(chunk.getNotificationID()); if (!DownloadFragment.mDownloads.isEmpty()) {
book.remoteUrl = book.getUrl(); LibraryNetworkEntity.Book book = DownloadFragment.mDownloads
book.file = fullFile; .get(chunk.getNotificationID());
bookDao.saveBook(book); book.remoteUrl = book.getUrl();
book.file = fullFile;
bookDao.saveBook(book);
}
downloadStatus.put(chunk.getNotificationID(), PLAY); downloadStatus.put(chunk.getNotificationID(), PLAY);
downloadProgress.put(chunk.getNotificationID(), 0); downloadProgress.put(chunk.getNotificationID(), 0);
} }

View File

@ -88,6 +88,10 @@ public class FileSearch {
Cursor query = contentResolver.query(uri, projection, selection, new String[]{"%."+zimFiles[0], "%."+zimFiles[1]}, null); Cursor query = contentResolver.query(uri, projection, selection, new String[]{"%."+zimFiles[0], "%."+zimFiles[1]}, null);
if (query == null) {
return;
}
try { try {
while (query.moveToNext()) { while (query.moveToNext()) {
File file = new File(query.getString(0)); File file = new File(query.getString(0));