From a4e13b591b18103418b7fd0b46191c0faf6d8095 Mon Sep 17 00:00:00 2001 From: mhutti1 Date: Sat, 12 Aug 2017 16:00:48 -0400 Subject: [PATCH] Download service is correctly restared on crash --- .../downloader/DownloadFragment.java | 21 +++++++--- .../downloader/DownloadService.java | 40 ++++++++++++------- .../kiwixmobile/utils/files/FileSearch.java | 4 ++ 3 files changed, 46 insertions(+), 19 deletions(-) diff --git a/app/src/main/java/org/kiwix/kiwixmobile/downloader/DownloadFragment.java b/app/src/main/java/org/kiwix/kiwixmobile/downloader/DownloadFragment.java index 73d9eb7b3..af7104f14 100644 --- a/app/src/main/java/org/kiwix/kiwixmobile/downloader/DownloadFragment.java +++ b/app/src/main/java/org/kiwix/kiwixmobile/downloader/DownloadFragment.java @@ -3,6 +3,7 @@ package org.kiwix.kiwixmobile.downloader; import android.app.Activity; import android.app.AlertDialog; +import android.app.Application; import android.content.Context; import android.graphics.Bitmap; import android.graphics.BitmapFactory; @@ -24,6 +25,8 @@ import android.widget.ProgressBar; import android.widget.RelativeLayout; import android.widget.TextView; +import java.util.Locale; +import org.kiwix.kiwixmobile.KiwixApplication; import org.kiwix.kiwixmobile.KiwixMobileActivity; import org.kiwix.kiwixmobile.R; import org.kiwix.kiwixmobile.library.entity.LibraryNetworkEntity; @@ -97,18 +100,26 @@ public class DownloadFragment extends Fragment { .show(); } - public String toHumanReadableTime(int seconds) { + public static String toHumanReadableTime(int seconds) { final double MINUTES = 60; final double HOURS = 60 * MINUTES; final double DAYS = 24 * HOURS; 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) - 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) - return String.format("%d %s %s", Math.round(seconds / MINUTES), getString(R.string.time_minute), getString(R.string.time_left)); - return String.format("%d %s %s", seconds, getString(R.string.time_second), getString(R.string.time_left)); + return String.format(Locale.getDefault(), "%d %s %s", Math.round(seconds / MINUTES), + 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 { diff --git a/app/src/main/java/org/kiwix/kiwixmobile/downloader/DownloadService.java b/app/src/main/java/org/kiwix/kiwixmobile/downloader/DownloadService.java index 40bd03238..ed2e7dfca 100644 --- a/app/src/main/java/org/kiwix/kiwixmobile/downloader/DownloadService.java +++ b/app/src/main/java/org/kiwix/kiwixmobile/downloader/DownloadService.java @@ -161,7 +161,7 @@ public class DownloadService extends Service { LibraryFragment.downloadingBooks.remove(book); String url = intent.getExtras().getString(DownloadIntent.DOWNLOAD_URL_PARAMETER); downloadBook(url, notificationCount, book); - return START_STICKY; + return START_REDELIVER_INTENT; } public void stopDownload(int notificationID) { @@ -169,9 +169,11 @@ public class DownloadService extends Service { synchronized (pauseLock) { pauseLock.notify(); } - DownloadFragment.mDownloads.remove(notificationID); - DownloadFragment.mDownloadFiles.remove(notificationID); - DownloadFragment.downloadAdapter.notifyDataSetChanged(); + if (!DownloadFragment.mDownloads.isEmpty()) { + DownloadFragment.mDownloads.remove(notificationID); + DownloadFragment.mDownloadFiles.remove(notificationID); + DownloadFragment.downloadAdapter.notifyDataSetChanged(); + } updateForeground(); 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).setContentText(getString(R.string.download_paused)); notificationManager.notify(notificationID, notification.get(notificationID).build()); - DownloadFragment.downloadAdapter.notifyDataSetChanged(); - downloadFragment.listView.invalidateViews(); + if (DownloadFragment.downloadAdapter != null) { + DownloadFragment.downloadAdapter.notifyDataSetChanged(); + downloadFragment.listView.invalidateViews(); + } } 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).setContentText(""); notificationManager.notify(notificationID, notification.get(notificationID).build()); - DownloadFragment.downloadAdapter.notifyDataSetChanged(); - downloadFragment.listView.invalidateViews(); + if (DownloadFragment.downloadAdapter != null) { + DownloadFragment.downloadAdapter.notifyDataSetChanged(); + downloadFragment.listView.invalidateViews(); + } return true; } 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); if (book.file != null && (book.file.exists() || new File(book.file.getPath() + ".part").exists())) { // Calculate initial download progress @@ -264,7 +273,7 @@ public class DownloadService extends Service { } notification.get(notificationID).setProgress(100, progress, false); 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()); if (progress == 0 || progress == 100) { // Tells android to not kill the service @@ -344,10 +353,13 @@ public class DownloadService extends Service { downloaded += output.length(); if (chunk.getStartByte() == 0) { - LibraryNetworkEntity.Book book = DownloadFragment.mDownloads.get(chunk.getNotificationID()); - book.remoteUrl = book.getUrl(); - book.file = fullFile; - bookDao.saveBook(book); + if (!DownloadFragment.mDownloads.isEmpty()) { + LibraryNetworkEntity.Book book = DownloadFragment.mDownloads + .get(chunk.getNotificationID()); + book.remoteUrl = book.getUrl(); + book.file = fullFile; + bookDao.saveBook(book); + } downloadStatus.put(chunk.getNotificationID(), PLAY); downloadProgress.put(chunk.getNotificationID(), 0); } diff --git a/app/src/main/java/org/kiwix/kiwixmobile/utils/files/FileSearch.java b/app/src/main/java/org/kiwix/kiwixmobile/utils/files/FileSearch.java index efe693468..f37cdb817 100644 --- a/app/src/main/java/org/kiwix/kiwixmobile/utils/files/FileSearch.java +++ b/app/src/main/java/org/kiwix/kiwixmobile/utils/files/FileSearch.java @@ -88,6 +88,10 @@ public class FileSearch { Cursor query = contentResolver.query(uri, projection, selection, new String[]{"%."+zimFiles[0], "%."+zimFiles[1]}, null); + if (query == null) { + return; + } + try { while (query.moveToNext()) { File file = new File(query.getString(0));