Skip to content

Commit

Permalink
fixed slow downloading speeds on some devices
Browse files Browse the repository at this point in the history
  • Loading branch information
tonyofrancis committed Jan 9, 2018
1 parent 61f5896 commit 9754f66
Show file tree
Hide file tree
Showing 17 changed files with 125 additions and 63 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
Version 2.0.0-RC4
- Fixed slow downloading speeds on some devices.

Version 2.0.0-RC3
- Fix compile and build issue for non Kotlin projects.
- Minor changes for rxFetch.
Expand Down
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[![Build Status](https://travis-ci.org/tonyofrancis/Fetch.svg?branch=v2)](https://travis-ci.org/tonyofrancis/Fetch)
[ ![Download](https://api.bintray.com/packages/tonyofrancis/maven/fetch2/images/download.svg?version=2.0.0-RC3) ](https://bintray.com/tonyofrancis/maven/fetch2/2.0.0-RC3/link)
[ ![Download](https://api.bintray.com/packages/tonyofrancis/maven/fetch2/images/download.svg?version=2.0.0-RC4) ](https://bintray.com/tonyofrancis/maven/fetch2/2.0.0-RC4/link)
[![Android Arsenal](https://img.shields.io/badge/Android%20Arsenal-Android%20Networking-blue.svg?style=flat)](https://android-arsenal.com/details/1/5196)
[![License](https://img.shields.io/badge/license-Apache%202.0-blue.svg)](https://github.com/tonyofrancis/Fetch/blob/master/LICENSE)

Expand Down Expand Up @@ -43,7 +43,7 @@ How to use Fetch
Using Fetch is easy! Just add the Gradle dependency to your application's build.gradle file.

```java
implementation "com.tonyodev.fetch2:fetch2:2.0.0-RC3"
implementation "com.tonyodev.fetch2:fetch2:2.0.0-RC4"
```

Next, get an instance of Fetch using the builder, and request a download.
Expand Down Expand Up @@ -234,7 +234,7 @@ to use the OkHttp Downloader instead. You can create your own custom downloaders
if necessary. See the Java docs for details.
```java
implementation "com.tonyodev.fetch2downloaders:fetch2downloaders:2.0.0-RC3"
implementation "com.tonyodev.fetch2downloaders:fetch2downloaders:2.0.0-RC4"
```
Set the OkHttp Downloader for Fetch to use.
```java
Expand All @@ -255,7 +255,7 @@ If you would like to take advantage of RxJava2 features when using Fetch,
add the following gradle dependency to your application's build.gradle file.

```java
implementation "com.tonyodev.fetch2rx:fetch2rx:2.0.0-RC3"
implementation "com.tonyodev.fetch2rx:fetch2rx:2.0.0-RC4"
```

RxFetch makes it super easy to enqueue download requests and query downloads using rxJava2 functional methods.
Expand Down Expand Up @@ -287,7 +287,7 @@ Fetch1 Migration

Migrate downloads from Fetch1 to Fetch2 using the migration assistant. Add the following gradle dependency to your application's build.gradle file.
```java
implementation "com.tonyodev.fetchmigrator:fetchmigrator:2.0.0-RC3"
implementation "com.tonyodev.fetchmigrator:fetchmigrator:2.0.0-RC4"
```
Then run the Migrator.
Expand Down
6 changes: 3 additions & 3 deletions fetch2/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ android {
defaultConfig {
minSdkVersion library_min_version
targetSdkVersion library_target_version
versionCode 4
versionName "2.0.0-RC3"
versionCode 5
versionName "2.0.0-RC4"

testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"

Expand Down Expand Up @@ -72,7 +72,7 @@ publish {
userOrg = 'tonyofrancis'
groupId = 'com.tonyodev.fetch2'
artifactId = 'fetch2'
publishVersion = '2.0.0-RC3'
publishVersion = '2.0.0-RC4'
desc = 'Fetch is a simple, powerful, customizable file download manager library for Android.'
website = 'https://github.com/tonyofrancis/fetch'
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ public void deleteMulti() throws Exception {
databaseManager.insert(downloadInfoList);
databaseManager.delete(downloadInfoList);
final List<Integer> ids = new ArrayList<>();
for(DownloadInfo downloadInfo : downloadInfoList) {
for (DownloadInfo downloadInfo : downloadInfoList) {
ids.add(downloadInfo.getId());
}
final List<DownloadInfo> queryList = databaseManager.get(ids);
Expand Down Expand Up @@ -188,7 +188,7 @@ public void updateMulti() throws Exception {
}
databaseManager.update(downloadInfoList);
final List<Integer> ids = new ArrayList<>();
for(DownloadInfo downloadInfo : downloadInfoList) {
for (DownloadInfo downloadInfo : downloadInfoList) {
ids.add(downloadInfo.getId());
}
final List<DownloadInfo> queryList = databaseManager.get(ids);
Expand Down Expand Up @@ -254,7 +254,7 @@ public void getIds() throws Exception {
}
databaseManager.insert(downloadInfoList);
final List<Integer> ids = new ArrayList<>();
for(DownloadInfo downloadInfo : downloadInfoList) {
for (DownloadInfo downloadInfo : downloadInfoList) {
ids.add(downloadInfo.getId());
}
final List<DownloadInfo> queryList = databaseManager.get(ids);
Expand Down Expand Up @@ -380,6 +380,27 @@ public void getGroupWithStatus() throws Exception {
}
}

@Test
public void fileUpdate() throws Exception {
databaseManager.deleteAll();
final Request request = getTestRequest();
final DownloadInfo downloadInfo = FetchTypeConverterExtensions.toDownloadInfo(request);
final Pair<DownloadInfo, Boolean> pair = databaseManager.insert(downloadInfo);
assertTrue(pair.getSecond());
final long downloaded = 2000;
final long total = Long.MAX_VALUE;
final Status status = Status.DOWNLOADING;
downloadInfo.setDownloaded(downloaded);
downloadInfo.setTotal(total);
downloadInfo.setStatus(status);
databaseManager.updateFileBytesInfoAndStatusOnly(downloadInfo);
final DownloadInfo downloadInfo1 = databaseManager.get(downloadInfo.getId());
assertNotNull(downloadInfo1);
assertEquals(downloaded, downloadInfo1.getDownloaded());
assertEquals(total, downloadInfo1.getTotal());
assertEquals(status, downloadInfo1.getStatus());
}

@Test
public void closed() throws Exception {
assertFalse(databaseManager.isClosed());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ interface DatabaseManager : Closeable {
fun deleteAll()
fun update(downloadInfo: DownloadInfo)
fun update(downloadInfoList: List<DownloadInfo>)
fun updateFileBytesInfoAndStatusOnly(downloadInfo: DownloadInfo)
fun get(): List<DownloadInfo>
fun get(id: Int): DownloadInfo?
fun get(ids: List<Int>): List<DownloadInfo?>
fun getByStatus(status: Status): List<DownloadInfo>
fun getByGroup(group: Int): List<DownloadInfo>
fun getDownloadsInGroupWithStatus(groupId: Int, status: Status): List<DownloadInfo>

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@ package com.tonyodev.fetch2.database

import android.arch.persistence.room.Room
import android.content.Context
import android.database.sqlite.SQLiteException
import com.tonyodev.fetch2.Logger
import com.tonyodev.fetch2.Status
import com.tonyodev.fetch2.exception.FetchImplementationException
import android.arch.persistence.room.RoomMasterTable.TABLE_NAME


open class DatabaseManagerImpl constructor(context: Context,
val namespace: String,
Expand Down Expand Up @@ -87,6 +90,29 @@ open class DatabaseManagerImpl constructor(context: Context,
}
}

override fun updateFileBytesInfoAndStatusOnly(downloadInfo: DownloadInfo) {
synchronized(lock) {
throwExceptionIfClosed()
val database = requestDatabaseInternal.openHelper.writableDatabase
try {
database.beginTransaction()
database.execSQL("UPDATE ${DownloadDatabase.TABLE_NAME} SET "
+ "${DownloadDatabase.COLUMN_DOWNLOADED} = ${downloadInfo.downloaded}, "
+ "${DownloadDatabase.COLUMN_TOTAL} = ${downloadInfo.total}, "
+ "${DownloadDatabase.COLUMN_STATUS} = ${downloadInfo.status.value} "
+ "WHERE ${DownloadDatabase.COLUMN_ID} = ${downloadInfo.id}")
database.setTransactionSuccessful()
} catch (e: SQLiteException) {
logger.e("DatabaseManager exception", e)
}
try {
database.endTransaction()
} catch (e: SQLiteException) {
logger.e("DatabaseManager exception", e)
}
}
}

override fun get(): List<DownloadInfo> {
synchronized(lock) {
throwExceptionIfClosed()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,19 @@ abstract class DownloadDatabase : RoomDatabase() {

companion object {
const val TABLE_NAME = "requests"
const val COLUMN_ID = "_id"
const val COLUMN_NAMESPACE = "_namespace"
const val COLUMN_URL = "_url"
const val COLUMN_FILE = "_file"
const val COLUMN_GROUP = "_group"
const val COLUMN_PRIORITY = "_priority"
const val COLUMN_HEADERS = "_headers"
const val COLUMN_DOWNLOADED = "_written_bytes"
const val COLUMN_TOTAL = "_total_bytes"
const val COLUMN_STATUS = "_status"
const val COLUMN_ERROR = "_error"
const val COLUMN_NETWORK_TYPE = "_network_type"
const val COLUMN_CREATED = "_created"
}

}
30 changes: 15 additions & 15 deletions fetch2/src/main/java/com/tonyodev/fetch2/database/DownloadInfo.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,48 +15,48 @@ import java.util.Date


@Entity(tableName = DownloadDatabase.TABLE_NAME,
indices = [(Index(value = ["_file"], unique = true)),
(Index(value = ["_group", "_status"], unique = false))])
indices = [(Index(value = [DownloadDatabase.COLUMN_FILE], unique = true)),
(Index(value = [DownloadDatabase.COLUMN_GROUP, DownloadDatabase.COLUMN_STATUS], unique = false))])
class DownloadInfo : Download {

@PrimaryKey
@ColumnInfo(name = "_id")
@ColumnInfo(name = DownloadDatabase.COLUMN_ID)
override var id: Int = 0

@ColumnInfo(name = "_namespace")
@ColumnInfo(name = DownloadDatabase.COLUMN_NAMESPACE)
override var namespace: String = ""

@ColumnInfo(name = "_url")
@ColumnInfo(name = DownloadDatabase.COLUMN_URL)
override var url: String = ""

@ColumnInfo(name = "_file")
@ColumnInfo(name = DownloadDatabase.COLUMN_FILE)
override var file: String = ""

@ColumnInfo(name = "_group")
@ColumnInfo(name = DownloadDatabase.COLUMN_GROUP)
override var group: Int = 0

@ColumnInfo(name = "_priority")
@ColumnInfo(name = DownloadDatabase.COLUMN_PRIORITY)
override var priority: Priority = defaultPriority

@ColumnInfo(name = "_headers")
@ColumnInfo(name = DownloadDatabase.COLUMN_HEADERS)
override var headers: Map<String, String> = defaultEmptyHeaderMap

@ColumnInfo(name = "_written_bytes")
@ColumnInfo(name = DownloadDatabase.COLUMN_DOWNLOADED)
override var downloaded: Long = 0L

@ColumnInfo(name = "_total_bytes")
@ColumnInfo(name = DownloadDatabase.COLUMN_TOTAL)
override var total: Long = -1L

@ColumnInfo(name = "_status")
@ColumnInfo(name = DownloadDatabase.COLUMN_STATUS)
override var status: Status = defaultStatus

@ColumnInfo(name = "_error")
@ColumnInfo(name = DownloadDatabase.COLUMN_ERROR)
override var error: Error = defaultNoError

@ColumnInfo(name = "_network_type")
@ColumnInfo(name = DownloadDatabase.COLUMN_NETWORK_TYPE)
override var networkType: NetworkType = defaultNetworkType

@ColumnInfo(name = "_created")
@ColumnInfo(name = DownloadDatabase.COLUMN_CREATED)
override var created: Long = Date().time

override val progress: Int
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ interface FileDownloader : Runnable {
etaInMilliseconds: Long)

fun onProgress(download: Download,
etaInMilliSeconds: Long,
reportProgress: Boolean = false)
etaInMilliSeconds: Long)

fun onError(download: Download)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ open class FileDownloaderImpl(val initialDownload: Download,
logger.d("FileDownloader starting Download $download")
}
if (!interrupted) {
input = BufferedInputStream(response.byteStream)
input = BufferedInputStream(response.byteStream, downloadBufferSizeBytes)
downloadInfoInternal.downloaded = downloadedInternal
downloadInfoInternal.total = totalInternal
delegate?.onStarted(
Expand Down Expand Up @@ -131,10 +131,6 @@ open class FileDownloaderImpl(val initialDownload: Download,
output.write(buffer, 0, read)
downloadedInternal += read

reportingStopTime = System.nanoTime()
val hasReportingTimeElapsed = hasIntervalTimeElapsed(reportingStartTime,
reportingStopTime, progressReportingIntervalMillis)

downloadSpeedStopTime = System.nanoTime()
val downloadSpeedCheckTimeElapsed = hasIntervalTimeElapsed(downloadSpeedStartTime,
downloadSpeedStopTime, DEFAULT_DOWNLOAD_SPEED_REPORTING_INTERVAL_IN_MILLISECONDS)
Expand All @@ -147,20 +143,24 @@ open class FileDownloaderImpl(val initialDownload: Download,
downloadedBytesPerSecond = downloadedBytesPerSecond)

downloadedBytesPerSecond = downloadedInternal
downloadSpeedStartTime = System.nanoTime()
}

reportingStopTime = System.nanoTime()
val hasReportingTimeElapsed = hasIntervalTimeElapsed(reportingStartTime,
reportingStopTime, progressReportingIntervalMillis)

if (hasReportingTimeElapsed) {
downloadInfoInternal.downloaded = downloadedInternal
downloadInfoInternal.total = totalInternal
delegate?.onProgress(
download = downloadInfoInternal,
etaInMilliSeconds = estimatedTimeRemainingInMillisecondsInternal)
reportingStartTime = System.nanoTime()
}

downloadInfoInternal.downloaded = downloadedInternal
downloadInfoInternal.total = totalInternal
delegate?.onProgress(
download = downloadInfoInternal,
etaInMilliSeconds = estimatedTimeRemainingInMillisecondsInternal,
reportProgress = hasReportingTimeElapsed)

if (downloadSpeedCheckTimeElapsed) {
downloadSpeedStartTime = System.nanoTime()
}
read = input.read(buffer, 0, downloadBufferSizeBytes)
}
if (read == -1 && !interrupted) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,13 @@ open class DownloadInfoManagerDelegate(val downloadInfoUpdater: DownloadInfoUpda
}
}

override fun onProgress(download: Download, etaInMilliSeconds: Long,
reportProgress: Boolean) {
val downloadInfo = download as DownloadInfo
downloadInfo.status = Status.DOWNLOADING
override fun onProgress(download: Download, etaInMilliSeconds: Long) {
try {
downloadInfoUpdater.update(downloadInfo)
if (reportProgress) {
uiHandler.post {
fetchListener.onProgress(downloadInfo, etaInMilliSeconds)
}
val downloadInfo = download as DownloadInfo
downloadInfo.status = Status.DOWNLOADING
downloadInfoUpdater.updateFileBytesInfoAndStatusOnly(downloadInfo)
uiHandler.post {
fetchListener.onProgress(download, etaInMilliSeconds)
}
} catch (e: Exception) {
logger.e("DownloadManagerDelegate", e)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ import com.tonyodev.fetch2.database.DownloadInfo

open class DownloadInfoUpdater(val databaseManagerInternal: DatabaseManager) {

open fun updateFileBytesInfoAndStatusOnly(downloadInfo: DownloadInfo) {
databaseManagerInternal.updateFileBytesInfoAndStatusOnly(downloadInfo)
}

open fun update(downloadInfo: DownloadInfo) {
databaseManagerInternal.update(downloadInfo)
}
Expand Down
6 changes: 3 additions & 3 deletions fetch2downloaders/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ android {
defaultConfig {
minSdkVersion library_min_version
targetSdkVersion library_target_version
versionCode 4
versionName "2.0.0-RC3"
versionCode 5
versionName "2.0.0-RC4"

testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"

Expand Down Expand Up @@ -72,7 +72,7 @@ publish {
userOrg = 'tonyofrancis'
groupId = 'com.tonyodev.fetch2downloaders'
artifactId = 'fetch2downloaders'
publishVersion = '2.0.0-RC3'
publishVersion = '2.0.0-RC4'
desc = 'Downloaders for Fetch2'
website = 'https://github.com/tonyofrancis/fetch'
}
Expand Down
Loading

0 comments on commit 9754f66

Please sign in to comment.