From 6f8b49c974add78075f1e09359646946cf378f3f Mon Sep 17 00:00:00 2001 From: Toby Kurien Date: Sat, 5 Sep 2015 09:54:25 +0200 Subject: [PATCH] proper handling of file resources, implemented progress stats for async downloader --- .../fdroid/fdroid/net/AsyncDownloader.java | 68 ++++++++++++++++--- .../receiver/DownloadManagerReceiver.java | 1 + 2 files changed, 59 insertions(+), 10 deletions(-) diff --git a/F-Droid/src/org/fdroid/fdroid/net/AsyncDownloader.java b/F-Droid/src/org/fdroid/fdroid/net/AsyncDownloader.java index f6e63735f..57046e0c1 100644 --- a/F-Droid/src/org/fdroid/fdroid/net/AsyncDownloader.java +++ b/F-Droid/src/org/fdroid/fdroid/net/AsyncDownloader.java @@ -12,12 +12,14 @@ import android.os.Build; import android.os.ParcelFileDescriptor; import org.fdroid.fdroid.AppDetails; +import org.fdroid.fdroid.ProgressListener; import org.fdroid.fdroid.data.Apk; import org.fdroid.fdroid.data.App; import org.fdroid.fdroid.data.SanitizedFile; import java.io.ByteArrayOutputStream; import java.io.File; +import java.io.FileDescriptor; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; @@ -73,15 +75,7 @@ public class AsyncDownloader extends AsyncDownloadWrapper { try { // write the downloaded file to the expected location ParcelFileDescriptor fd = dm.openDownloadedFile(downloadId); - InputStream is = new FileInputStream(fd.getFileDescriptor()); - OutputStream os = new FileOutputStream(localFile); - byte[] buffer = new byte[1024]; - int count = 0; - while ((count = is.read(buffer, 0, buffer.length)) > 0) { - os.write(buffer, 0, count); - } - os.close(); - + copyFile(fd.getFileDescriptor(), localFile); listener.onDownloadComplete(); } catch (IOException e) { listener.onErrorDownloading(e.getLocalizedMessage()); @@ -101,13 +95,67 @@ public class AsyncDownloader extends AsyncDownloadWrapper { this.downloadId = dm.enqueue(request); } + /** + * Copy input file to output file + * @param inputFile + * @param outputFile + * @throws IOException + */ + private void copyFile(FileDescriptor inputFile, SanitizedFile outputFile) throws IOException { + InputStream is = new FileInputStream(inputFile); + OutputStream os = new FileOutputStream(outputFile); + byte[] buffer = new byte[1024]; + int count = 0; + + try { + while ((count = is.read(buffer, 0, buffer.length)) > 0) { + os.write(buffer, 0, count); + } + } finally { + os.close(); + is.close(); + } + } + @Override public int getBytesRead() { + if (downloadId < 0) return 0; + + DownloadManager.Query query = new DownloadManager.Query(); + query.setFilterById(downloadId); + Cursor c = dm.query(query); + + try { + if (c.moveToFirst()) { + // we use the description column to store the app id + int columnIndex = c.getColumnIndex(DownloadManager.COLUMN_BYTES_DOWNLOADED_SO_FAR); + return c.getInt(columnIndex); + } + } finally { + c.close(); + } + return 0; } @Override public int getTotalBytes() { + if (downloadId < 0) return 0; + + DownloadManager.Query query = new DownloadManager.Query(); + query.setFilterById(downloadId); + Cursor c = dm.query(query); + + try { + if (c.moveToFirst()) { + // we use the description column to store the app id + int columnIndex = c.getColumnIndex(DownloadManager.COLUMN_TOTAL_SIZE_BYTES); + return c.getInt(columnIndex); + } + } finally { + c.close(); + } + return 0; } @@ -156,7 +204,7 @@ public class AsyncDownloader extends AsyncDownloadWrapper { } if (intent.hasExtra(DownloadManager.EXTRA_NOTIFICATION_CLICK_DOWNLOAD_IDS)) { - // we have been passed a DownloadManager download id, so get the app id for it + // we have been passed multiple download id's - just return the first one long[] downloadIds = intent.getLongArrayExtra(DownloadManager.EXTRA_NOTIFICATION_CLICK_DOWNLOAD_IDS); if (downloadIds != null && downloadIds.length > 0) { return downloadIds[0]; diff --git a/F-Droid/src/org/fdroid/fdroid/receiver/DownloadManagerReceiver.java b/F-Droid/src/org/fdroid/fdroid/receiver/DownloadManagerReceiver.java index 913d1c4df..0a689b40d 100644 --- a/F-Droid/src/org/fdroid/fdroid/receiver/DownloadManagerReceiver.java +++ b/F-Droid/src/org/fdroid/fdroid/receiver/DownloadManagerReceiver.java @@ -1,5 +1,6 @@ package org.fdroid.fdroid.receiver; +import android.app.DownloadManager; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent;