mirror of
https://github.com/SchildiChat/SchildiChat-android.git
synced 2025-03-17 19:58:57 +03:00
Cleanup
This commit is contained in:
parent
76ab34f1a6
commit
34723ce4f7
6 changed files with 52 additions and 51 deletions
|
@ -218,12 +218,12 @@ class DebugMenuActivity : VectorBaseActivity<ActivityDebugMenuBinding>() {
|
|||
}
|
||||
|
||||
private fun scanQRCode() {
|
||||
if (checkPermissions(PERMISSIONS_FOR_TAKING_PHOTO, this, permissionCamera)) {
|
||||
if (checkPermissions(PERMISSIONS_FOR_TAKING_PHOTO, this, permissionCameraLauncher)) {
|
||||
doScanQRCode()
|
||||
}
|
||||
}
|
||||
|
||||
private val permissionCamera = registerForPermissionsResult { allGranted, _ ->
|
||||
private val permissionCameraLauncher = registerForPermissionsResult { allGranted, _ ->
|
||||
if (allGranted) {
|
||||
doScanQRCode()
|
||||
}
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Copyright 2019 New Vector Ltd
|
||||
* Copyright 2021 New Vector Ltd
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
|
@ -24,7 +24,6 @@ import androidx.core.app.ActivityCompat
|
|||
import androidx.core.content.ContextCompat
|
||||
import im.vector.app.R
|
||||
import im.vector.app.core.platform.VectorBaseActivity
|
||||
import im.vector.app.core.utils.PERMISSIONS_ALL
|
||||
import im.vector.app.core.utils.checkPermissions
|
||||
import im.vector.app.core.utils.onPermissionDeniedDialog
|
||||
import im.vector.app.core.utils.onPermissionDeniedSnackbar
|
||||
|
@ -38,6 +37,14 @@ class DebugPermissionActivity : VectorBaseActivity<ActivityDebugPermissionBindin
|
|||
|
||||
override fun getCoordinatorLayout() = views.coordinatorLayout
|
||||
|
||||
// For debug
|
||||
private val allPermissions = listOf(
|
||||
Manifest.permission.CAMERA,
|
||||
Manifest.permission.RECORD_AUDIO,
|
||||
Manifest.permission.WRITE_EXTERNAL_STORAGE,
|
||||
Manifest.permission.READ_EXTERNAL_STORAGE,
|
||||
Manifest.permission.READ_CONTACTS)
|
||||
|
||||
private var lastPermissions = emptyList<String>()
|
||||
|
||||
override fun initUiAndData() {
|
||||
|
@ -51,6 +58,10 @@ class DebugPermissionActivity : VectorBaseActivity<ActivityDebugPermissionBindin
|
|||
lastPermissions = listOf(Manifest.permission.RECORD_AUDIO)
|
||||
checkPerm()
|
||||
}
|
||||
views.cameraAudio.setOnClickListener {
|
||||
lastPermissions = listOf(Manifest.permission.CAMERA, Manifest.permission.RECORD_AUDIO)
|
||||
checkPerm()
|
||||
}
|
||||
views.write.setOnClickListener {
|
||||
lastPermissions = listOf(Manifest.permission.WRITE_EXTERNAL_STORAGE)
|
||||
checkPerm()
|
||||
|
@ -80,9 +91,9 @@ class DebugPermissionActivity : VectorBaseActivity<ActivityDebugPermissionBindin
|
|||
if (deniedPermanently) {
|
||||
dialogOrSnackbar = !dialogOrSnackbar
|
||||
if (dialogOrSnackbar) {
|
||||
onPermissionDeniedDialog(R.string.denied_permission_camera)
|
||||
onPermissionDeniedDialog(R.string.denied_permission_generic)
|
||||
} else {
|
||||
onPermissionDeniedSnackbar(R.string.denied_permission_camera)
|
||||
onPermissionDeniedSnackbar(R.string.denied_permission_generic)
|
||||
}
|
||||
} else {
|
||||
Toast.makeText(this, "Denied", Toast.LENGTH_SHORT).show()
|
||||
|
@ -103,9 +114,9 @@ class DebugPermissionActivity : VectorBaseActivity<ActivityDebugPermissionBindin
|
|||
return buildString {
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
|
||||
Timber.v("## debugPermission() : log the permissions status used by the app")
|
||||
PERMISSIONS_ALL.forEach { permission ->
|
||||
allPermissions.forEach { permission ->
|
||||
append("[$permission] : ")
|
||||
if (PackageManager.PERMISSION_GRANTED == ContextCompat.checkSelfPermission(this@DebugPermissionActivity, permission)) {
|
||||
if (ContextCompat.checkSelfPermission(this@DebugPermissionActivity, permission) == PackageManager.PERMISSION_GRANTED) {
|
||||
append("PERMISSION_GRANTED")
|
||||
} else {
|
||||
append("PERMISSION_DENIED")
|
||||
|
|
|
@ -156,7 +156,7 @@
|
|||
android:id="@+id/debug_permission"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="Permission status" />
|
||||
android:text="Permissions" />
|
||||
|
||||
</LinearLayout>
|
||||
|
||||
|
|
|
@ -30,35 +30,42 @@
|
|||
android:id="@+id/camera"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="Manifest.permission.CAMERA"
|
||||
android:text="CAMERA"
|
||||
android:textAllCaps="false" />
|
||||
|
||||
<Button
|
||||
android:id="@+id/audio"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="Manifest.permission.RECORD_AUDIO"
|
||||
android:text="RECORD_AUDIO"
|
||||
android:textAllCaps="false" />
|
||||
|
||||
<Button
|
||||
android:id="@+id/camera_audio"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="CAMERA + RECORD_AUDIO"
|
||||
android:textAllCaps="false" />
|
||||
|
||||
<Button
|
||||
android:id="@+id/write"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="Manifest.permission.WRITE_EXTERNAL_STORAGE"
|
||||
android:text="WRITE_EXTERNAL_STORAGE"
|
||||
android:textAllCaps="false" />
|
||||
|
||||
<Button
|
||||
android:id="@+id/read"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="Manifest.permission.READ_EXTERNAL_STORAGE"
|
||||
android:text="READ_EXTERNAL_STORAGE"
|
||||
android:textAllCaps="false" />
|
||||
|
||||
<Button
|
||||
android:id="@+id/contact"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="Manifest.permission.READ_CONTACTS"
|
||||
android:text="READ_CONTACTS"
|
||||
android:textAllCaps="false" />
|
||||
|
||||
</LinearLayout>
|
||||
|
|
|
@ -42,14 +42,6 @@ val PERMISSIONS_FOR_PICKING_CONTACT = listOf(Manifest.permission.READ_CONTACTS)
|
|||
|
||||
val PERMISSIONS_EMPTY = emptyList<String>()
|
||||
|
||||
// For debug
|
||||
val PERMISSIONS_ALL = listOf(
|
||||
Manifest.permission.CAMERA,
|
||||
Manifest.permission.RECORD_AUDIO,
|
||||
Manifest.permission.WRITE_EXTERNAL_STORAGE,
|
||||
Manifest.permission.READ_EXTERNAL_STORAGE,
|
||||
Manifest.permission.READ_CONTACTS)
|
||||
|
||||
// This is not ideal to store the value like that, but it works
|
||||
private var permissionDialogDisplayed = false
|
||||
|
||||
|
@ -62,42 +54,33 @@ private var permissionDialogDisplayed = false
|
|||
fun ComponentActivity.registerForPermissionsResult(lambda: (allGranted: Boolean, deniedPermanently: Boolean) -> Unit)
|
||||
: ActivityResultLauncher<Array<String>> {
|
||||
return registerForActivityResult(ActivityResultContracts.RequestMultiplePermissions()) { result ->
|
||||
if (result.keys.all { result[it] == true }) {
|
||||
lambda(true, /* not used */ false)
|
||||
} else {
|
||||
if (permissionDialogDisplayed) {
|
||||
// A permission dialog has been displayed, so even if the user has checked the do not ask again button, we do
|
||||
// not tell the user to open the app settings
|
||||
lambda(false, false)
|
||||
} else {
|
||||
// No dialog has been displayed, so tell the user to go to the system setting
|
||||
lambda(false, true)
|
||||
}
|
||||
}
|
||||
// Reset
|
||||
permissionDialogDisplayed = false
|
||||
onPermissionResult(result, lambda)
|
||||
}
|
||||
}
|
||||
|
||||
fun Fragment.registerForPermissionsResult(lambda: (allGranted: Boolean, deniedPermanently: Boolean) -> Unit): ActivityResultLauncher<Array<String>> {
|
||||
return registerForActivityResult(ActivityResultContracts.RequestMultiplePermissions()) { result ->
|
||||
if (result.keys.all { result[it] == true }) {
|
||||
lambda(true, /* not used */ false)
|
||||
} else {
|
||||
if (permissionDialogDisplayed) {
|
||||
// A permission dialog has been displayed, so even if the user has checked the do not ask again button, we do
|
||||
// not tell the user to open the app settings
|
||||
lambda(false, false)
|
||||
} else {
|
||||
// No dialog has been displayed, so tell the user to go to the system setting
|
||||
lambda(false, true)
|
||||
}
|
||||
}
|
||||
// Reset
|
||||
permissionDialogDisplayed = false
|
||||
onPermissionResult(result, lambda)
|
||||
}
|
||||
}
|
||||
|
||||
private fun onPermissionResult(result: Map<String, Boolean>, lambda: (allGranted: Boolean, deniedPermanently: Boolean) -> Unit) {
|
||||
if (result.keys.all { result[it] == true }) {
|
||||
lambda(true, /* not used */ false)
|
||||
} else {
|
||||
if (permissionDialogDisplayed) {
|
||||
// A permission dialog has been displayed, so even if the user has checked the do not ask again button, we do
|
||||
// not tell the user to open the app settings
|
||||
lambda(false, false)
|
||||
} else {
|
||||
// No dialog has been displayed, so tell the user to go to the system setting
|
||||
lambda(false, true)
|
||||
}
|
||||
}
|
||||
// Reset
|
||||
permissionDialogDisplayed = false
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if the permissions provided in the list are granted.
|
||||
* This is an asynchronous method if permissions are requested, the final response
|
||||
|
|
|
@ -112,7 +112,7 @@ class ScanUserCodeFragment @Inject constructor()
|
|||
super.onResume()
|
||||
// Register ourselves as a handler for scan results.
|
||||
views.userCodeScannerView.setResultHandler(this)
|
||||
if (PackageManager.PERMISSION_GRANTED == ContextCompat.checkSelfPermission(requireContext(), Manifest.permission.CAMERA)) {
|
||||
if (ContextCompat.checkSelfPermission(requireContext(), Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED) {
|
||||
startCamera()
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue