mirror of
https://github.com/owncloud/android-library.git
synced 2025-06-08 00:16:09 +00:00
Apply code review suggestions
This commit is contained in:
parent
96c8e87a5f
commit
82abe5fdaa
@ -50,8 +50,9 @@ class DownloadRemoteFileOperation(
|
|||||||
localFolderPath: String
|
localFolderPath: String
|
||||||
) : RemoteOperation<Unit>() {
|
) : RemoteOperation<Unit>() {
|
||||||
|
|
||||||
private val mCancellationRequested = AtomicBoolean(false)
|
private val cancellationRequested = AtomicBoolean(false)
|
||||||
private val mDataTransferListeners: MutableSet<OnDatatransferProgressListener> = HashSet()
|
private val dataTransferListeners: MutableSet<OnDatatransferProgressListener> = HashSet()
|
||||||
|
|
||||||
var modificationTimestamp: Long = 0
|
var modificationTimestamp: Long = 0
|
||||||
private set
|
private set
|
||||||
|
|
||||||
@ -59,21 +60,20 @@ class DownloadRemoteFileOperation(
|
|||||||
private set
|
private set
|
||||||
|
|
||||||
override fun run(client: OwnCloudClient): RemoteOperationResult<Unit> {
|
override fun run(client: OwnCloudClient): RemoteOperationResult<Unit> {
|
||||||
var result: RemoteOperationResult<Unit>
|
|
||||||
|
|
||||||
// download will be performed to a temporal file, then moved to the final location
|
// download will be performed to a temporal file, then moved to the final location
|
||||||
val tmpFile = File(tmpPath)
|
val tmpFile = File(tmpPath)
|
||||||
|
|
||||||
// perform the download
|
// perform the download
|
||||||
try {
|
return try {
|
||||||
tmpFile.parentFile?.mkdirs()
|
tmpFile.parentFile?.mkdirs()
|
||||||
result = downloadFile(client, tmpFile)
|
downloadFile(client, tmpFile).also { result ->
|
||||||
Timber.i("Download of $remotePath to $tmpPath: ${result.logMessage}")
|
Timber.i("Download of $remotePath to $tmpPath: ${result.logMessage}")
|
||||||
|
}
|
||||||
} catch (e: Exception) {
|
} catch (e: Exception) {
|
||||||
result = RemoteOperationResult(e)
|
RemoteOperationResult<Unit>(e).also { result ->
|
||||||
Timber.e(e, "Download of $remotePath to $tmpPath: ${result.logMessage}")
|
Timber.e(e, "Download of $remotePath to $tmpPath: ${result.logMessage}")
|
||||||
}
|
}
|
||||||
return result
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Throws(Exception::class)
|
@Throws(Exception::class)
|
||||||
@ -95,26 +95,27 @@ class DownloadRemoteFileOperation(
|
|||||||
fos = FileOutputStream(targetFile)
|
fos = FileOutputStream(targetFile)
|
||||||
var transferred: Long = 0
|
var transferred: Long = 0
|
||||||
val contentLength = getMethod.getResponseHeader(HttpConstants.CONTENT_LENGTH_HEADER)
|
val contentLength = getMethod.getResponseHeader(HttpConstants.CONTENT_LENGTH_HEADER)
|
||||||
val totalToTransfer =
|
val totalToTransfer = if (!contentLength.isNullOrEmpty()) {
|
||||||
if (contentLength != null && contentLength.isNotEmpty()) contentLength.toLong() else 0
|
contentLength.toLong()
|
||||||
|
} else {
|
||||||
|
0
|
||||||
|
}
|
||||||
val bytes = ByteArray(4096)
|
val bytes = ByteArray(4096)
|
||||||
var readResult: Int
|
var readResult: Int
|
||||||
while (bis.read(bytes).also { readResult = it } != -1) {
|
while (bis.read(bytes).also { readResult = it } != -1) {
|
||||||
synchronized(mCancellationRequested) {
|
synchronized(cancellationRequested) {
|
||||||
if (mCancellationRequested.get()) {
|
if (cancellationRequested.get()) {
|
||||||
getMethod.abort()
|
getMethod.abort()
|
||||||
throw OperationCancelledException()
|
throw OperationCancelledException()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
fos.write(bytes, 0, readResult)
|
fos.write(bytes, 0, readResult)
|
||||||
transferred += readResult.toLong()
|
transferred += readResult.toLong()
|
||||||
synchronized(mDataTransferListeners) {
|
synchronized(dataTransferListeners) {
|
||||||
it = mDataTransferListeners.iterator()
|
it = dataTransferListeners.iterator()
|
||||||
while (it.hasNext()) {
|
while (it.hasNext()) {
|
||||||
it.next().onTransferProgress(
|
it.next()
|
||||||
readResult.toLong(), transferred, totalToTransfer,
|
.onTransferProgress(readResult.toLong(), transferred, totalToTransfer, targetFile.name)
|
||||||
targetFile.name
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -170,15 +171,14 @@ class DownloadRemoteFileOperation(
|
|||||||
private val tmpPath: String = localFolderPath + remotePath
|
private val tmpPath: String = localFolderPath + remotePath
|
||||||
|
|
||||||
fun addDatatransferProgressListener(listener: OnDatatransferProgressListener) {
|
fun addDatatransferProgressListener(listener: OnDatatransferProgressListener) {
|
||||||
synchronized(mDataTransferListeners) { mDataTransferListeners.add(listener) }
|
synchronized(dataTransferListeners) { dataTransferListeners.add(listener) }
|
||||||
}
|
}
|
||||||
|
|
||||||
fun removeDatatransferProgressListener(listener: OnDatatransferProgressListener?) {
|
fun removeDatatransferProgressListener(listener: OnDatatransferProgressListener?) {
|
||||||
synchronized(mDataTransferListeners) { mDataTransferListeners.remove(listener) }
|
synchronized(dataTransferListeners) { dataTransferListeners.remove(listener) }
|
||||||
}
|
}
|
||||||
|
|
||||||
fun cancel() {
|
fun cancel() {
|
||||||
mCancellationRequested.set(true) // atomic set; there is no need of synchronizing it
|
cancellationRequested.set(true) // atomic set; there is no need of synchronizing it
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user