Skip to content

Commit

Permalink
Null check cursors and DownloadManager instance
Browse files Browse the repository at this point in the history
  • Loading branch information
keianhzo committed Jun 2, 2020
1 parent f9edcd6 commit e13f749
Showing 1 changed file with 41 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ public void init() {
mContext.registerReceiver(mDownloadReceiver, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));
List<Download> downloads = getDownloads();
downloads.forEach(download -> {
if (!new File(UrlUtils.stripProtocol(download.getOutputFile())).exists()) {
if (mDownloadManager != null &&
!new File(UrlUtils.stripProtocol(download.getOutputFile())).exists()) {
mDownloadManager.remove(download.getId());
}
});
Expand Down Expand Up @@ -121,8 +122,10 @@ public void startDownload(@NonNull DownloadJob job) {
request.setDestinationUri(Uri.parse(outputPath));
}

mDownloadManager.enqueue(request);
scheduleUpdates();
if (mDownloadManager != null) {
mDownloadManager.enqueue(request);
scheduleUpdates();
}
}

@Nullable
Expand All @@ -144,15 +147,21 @@ public void removeDownload(long downloadId, boolean deleteFiles) {
if (file.exists()) {
File newFile = new File(UrlUtils.stripProtocol(download.getOutputFile().concat(".bak")));
file.renameTo(newFile);
mDownloadManager.remove(downloadId);
if (mDownloadManager != null) {
mDownloadManager.remove(downloadId);
}
newFile.renameTo(file);

} else {
mDownloadManager.remove(downloadId);
if (mDownloadManager != null) {
mDownloadManager.remove(downloadId);
}
}

} else {
mDownloadManager.remove(downloadId);
if (mDownloadManager != null) {
mDownloadManager.remove(downloadId);
}
}
}
notifyDownloadsUpdate();
Expand All @@ -166,26 +175,34 @@ public void removeAllDownloads(boolean deleteFiles) {
public Download getDownload(long downloadId) {
Download download = null;

DownloadManager.Query query = new DownloadManager.Query();
query.setFilterById(downloadId);
Cursor c = mDownloadManager.query(query);
if (c.moveToFirst()) {
download = Download.from(c);
if (mDownloadManager != null) {
DownloadManager.Query query = new DownloadManager.Query();
query.setFilterById(downloadId);
Cursor c = mDownloadManager.query(query);
if (c != null) {
if (c.moveToFirst()) {
download = Download.from(c);
}
c.close();
}
}
c.close();

return download;
}

public List<Download> getDownloads() {
List<Download> downloads = new ArrayList<>();

DownloadManager.Query query = new DownloadManager.Query();
Cursor c = mDownloadManager.query(query);
while (c.moveToNext()) {
downloads.add(Download.from(c));
if (mDownloadManager != null) {
DownloadManager.Query query = new DownloadManager.Query();
Cursor c = mDownloadManager.query(query);
if (c != null) {
while (c.moveToNext()) {
downloads.add(Download.from(c));
}
c.close();
}
}
c.close();

return downloads;
}
Expand All @@ -196,15 +213,17 @@ public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
long downloadId = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, 0);

if (DownloadManager.ACTION_DOWNLOAD_COMPLETE.equals(action)) {
if (mDownloadManager != null && DownloadManager.ACTION_DOWNLOAD_COMPLETE.equals(action)) {
DownloadManager.Query query = new DownloadManager.Query();
query.setFilterById(downloadId);
Cursor c = mDownloadManager.query(query);
if (c.moveToFirst()) {
notifyDownloadsUpdate();
notifyDownloadCompleted(Download.from(c));
if (c != null) {
if (c.moveToFirst()) {
notifyDownloadsUpdate();
notifyDownloadCompleted(Download.from(c));
}
c.close();
}
c.close();
}
}
};
Expand Down

0 comments on commit e13f749

Please sign in to comment.