-
Notifications
You must be signed in to change notification settings - Fork 189
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
**Background** Right now we don't shown progress on download file in mfkey32 **Changes** Create `download` method and use it **Test plan** Try open mfkey32 util. And also put [mfkey32.log](https://github.com/flipperdevices/Flipper-Android-App/files/13187632/mfkey32.log) to `/ext/nfc/.mfkey32.log`
- Loading branch information
Showing
8 changed files
with
155 additions
and
59 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
88 changes: 88 additions & 0 deletions
88
...mpl/src/main/java/com/flipperdevices/bridge/rpc/impl/delegates/FlipperDownloadDelegate.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
package com.flipperdevices.bridge.rpc.impl.delegates | ||
|
||
import com.flipperdevices.bridge.api.manager.FlipperRequestApi | ||
import com.flipperdevices.bridge.api.model.wrapToRequest | ||
import com.flipperdevices.core.log.LogTagProvider | ||
import com.flipperdevices.core.log.info | ||
import com.flipperdevices.core.progress.ProgressListener | ||
import com.flipperdevices.core.progress.ProgressWrapperTracker | ||
import com.flipperdevices.protobuf.Flipper | ||
import com.flipperdevices.protobuf.main | ||
import com.flipperdevices.protobuf.storage.readRequest | ||
import com.flipperdevices.protobuf.storage.statRequest | ||
import kotlinx.coroutines.flow.toList | ||
import java.io.File | ||
import java.io.FileNotFoundException | ||
import javax.inject.Inject | ||
|
||
class FlipperDownloadDelegate @Inject constructor() : LogTagProvider { | ||
override val TAG = "FlipperDownloadDelegate" | ||
suspend fun download( | ||
requestApi: FlipperRequestApi, | ||
pathOnFlipper: String, | ||
fileOnAndroid: File, | ||
externalProgressListener: ProgressListener | ||
) { | ||
info { "Start download file $pathOnFlipper to ${fileOnAndroid.path}" } | ||
val progressListener = ProgressWrapperTracker(externalProgressListener) | ||
|
||
val totalSize = getTotalSize(requestApi, pathOnFlipper) | ||
info { "Receive total size of file: $totalSize bytes" } | ||
|
||
var totalDownloaded = 0L | ||
|
||
requestApi.request( | ||
main { | ||
storageReadRequest = readRequest { | ||
path = pathOnFlipper | ||
} | ||
}.wrapToRequest() | ||
).collect { response -> | ||
if (response.commandStatus == Flipper.CommandStatus.ERROR_STORAGE_NOT_EXIST) { | ||
throw FileNotFoundException() | ||
} | ||
if (response.commandStatus != Flipper.CommandStatus.OK) { | ||
error("Failed with $response") | ||
} | ||
val data = response.storageReadResponse.file.data | ||
fileOnAndroid.appendBytes(data.toByteArray()) | ||
totalDownloaded += data.size() | ||
progressListener.report( | ||
totalDownloaded, | ||
totalSize | ||
) | ||
} | ||
} | ||
|
||
private suspend fun getTotalSize(requestApi: FlipperRequestApi, pathOnFlipper: String): Long { | ||
val listingList = requestApi.request( | ||
main { | ||
storageStatRequest = statRequest { | ||
path = pathOnFlipper | ||
} | ||
}.wrapToRequest() | ||
).toList() | ||
if (listingList.isEmpty()) { | ||
error("Not received any response for listing $pathOnFlipper") | ||
} | ||
|
||
if (listingList.size > 1) { | ||
error( | ||
"Listing request return more than one response. " + | ||
"Are you sure that you want download a file, not a directory?" | ||
) | ||
} | ||
|
||
val response = listingList.single() | ||
|
||
if (response.commandStatus == Flipper.CommandStatus.ERROR_STORAGE_NOT_EXIST) { | ||
throw FileNotFoundException() | ||
} | ||
|
||
if (!response.hasStorageStatResponse()) { | ||
error("Failed with $response") | ||
} | ||
|
||
return response.storageStatResponse.file.size.toLong() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 0 additions & 33 deletions
33
...creen/src/main/java/com/flipperdevices/nfc/mfkey32/screen/viewmodel/DownloadFileHelper.kt
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters