Skip to content

Commit d817329

Browse files
committed
refactor(android): Avoid using exceptions for flow control
1 parent 7e65e5b commit d817329

File tree

1 file changed

+30
-28
lines changed
  • react-native-mcu-manager/android/src/main/java/uk/co/playerdata/reactnativemcumanager

1 file changed

+30
-28
lines changed

react-native-mcu-manager/android/src/main/java/uk/co/playerdata/reactnativemcumanager/DeviceUpgrade.kt

Lines changed: 30 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ import io.runtime.mcumgr.exception.McuMgrException
1616
import io.runtime.mcumgr.image.McuMgrImage
1717
import java.io.IOException
1818

19-
2019
val UpgradeModes =
2120
mapOf(
2221
1 to FirmwareUpgradeManager.Mode.TEST_AND_CONFIRM,
@@ -66,11 +65,36 @@ class DeviceUpgrade(
6665
transport.release()
6766
}
6867

69-
fun uriToByteArray(uri: Uri): ByteArray? {
68+
private fun uriToByteArray(uri: Uri): ByteArray? {
7069
val inputStream = context.contentResolver.openInputStream(uri) ?: return null
7170
return inputStream.use { it.readBytes() }
7271
}
7372

73+
private fun extractImagesFrom(updateBundleUri: Uri): ImageSet {
74+
val type = context.contentResolver.getType(updateBundleUri)
75+
val binData = uriToByteArray(updateBundleUri) ?: throw IOException("Failed to read update file")
76+
77+
if (type == "application/zip") {
78+
return extractImagesFromZipFile(binData)
79+
} else {
80+
return extractImagesFromBinFile(binData)
81+
}
82+
}
83+
84+
private fun extractImagesFromBinFile(binData: ByteArray): ImageSet {
85+
// Check if the BIN file is valid.
86+
McuMgrImage.getHash(binData)
87+
88+
val binaries = ImageSet()
89+
binaries.add(binData)
90+
91+
return binaries
92+
}
93+
94+
private fun extractImagesFromZipFile(zipData: ByteArray): ImageSet {
95+
return ZipPackage(zipData).getBinaries();
96+
}
97+
7498
private fun doUpdate(updateBundleUri: Uri) {
7599
val estimatedSwapTime = updateOptions.estimatedSwapTime * 1000
76100
val modeInt = updateOptions.upgradeMode ?: 1
@@ -79,32 +103,10 @@ class DeviceUpgrade(
79103
val settings = Settings.Builder().setEstimatedSwapTime(estimatedSwapTime).build()
80104

81105
try {
82-
val byteArray = uriToByteArray(updateBundleUri) ?: return
83-
84-
try {
85-
// Check if the BIN file is valid.
86-
McuMgrImage.getHash(byteArray)
87-
88-
val stream = context.contentResolver.openInputStream(updateBundleUri)
89-
val images = ByteArray(stream!!.available())
90-
stream.read(images)
91-
92-
dfuManager.setMode(upgradeMode)
93-
dfuManager.start(images, settings)
94-
} catch (e: Exception) {
95-
try {
96-
val zip = ZipPackage(byteArray)
97-
val images: ImageSet = zip.getBinaries();
98-
dfuManager.setMode(upgradeMode)
99-
dfuManager.start(images, settings)
100-
} catch (e1: Exception) {
101-
e1.printStackTrace()
102-
disconnectDevice()
103-
withSafePromise { promise ->
104-
promise.reject(ReactNativeMcuMgrException.fromMcuMgrException(e1))
105-
}
106-
}
107-
}
106+
val images = extractImagesFrom(updateBundleUri)
107+
108+
dfuManager.setMode(upgradeMode)
109+
dfuManager.start(images, settings)
108110
} catch (e: IOException) {
109111
e.printStackTrace()
110112
disconnectDevice()

0 commit comments

Comments
 (0)