From 0f51646d3cb0ee828a126613c13f0545cff21e5a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=81lvaro=20Brey?= Date: Tue, 24 Jan 2023 16:03:44 +0100 Subject: [PATCH] Close scan activity if camera permission is denied MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Álvaro Brey --- .../client/documentscan/DocumentScanActivity.kt | 2 +- .../client/documentscan/DocumentScanViewModel.kt | 13 +++++++------ .../java/com/nextcloud/appscan/AppScanActivity.kt | 12 ++++++++++++ 3 files changed, 20 insertions(+), 7 deletions(-) diff --git a/app/src/main/java/com/nextcloud/client/documentscan/DocumentScanActivity.kt b/app/src/main/java/com/nextcloud/client/documentscan/DocumentScanActivity.kt index 15eb09a4a6..01c4ddeb4b 100644 --- a/app/src/main/java/com/nextcloud/client/documentscan/DocumentScanActivity.kt +++ b/app/src/main/java/com/nextcloud/client/documentscan/DocumentScanActivity.kt @@ -152,7 +152,7 @@ class DocumentScanActivity : ToolbarActivity(), Injectable { } } } - DocumentScanViewModel.UIState.DoneState -> { + DocumentScanViewModel.UIState.DoneState, DocumentScanViewModel.UIState.CanceledState -> { finish() } } diff --git a/app/src/main/java/com/nextcloud/client/documentscan/DocumentScanViewModel.kt b/app/src/main/java/com/nextcloud/client/documentscan/DocumentScanViewModel.kt index 1a793f7575..0e7d03cb63 100644 --- a/app/src/main/java/com/nextcloud/client/documentscan/DocumentScanViewModel.kt +++ b/app/src/main/java/com/nextcloud/client/documentscan/DocumentScanViewModel.kt @@ -72,6 +72,7 @@ class DocumentScanViewModel @Inject constructor( ) : BaseState(pageList) object DoneState : UIState + object CanceledState : UIState } private var uploadFolder: String? = null @@ -95,14 +96,14 @@ class DocumentScanViewModel @Inject constructor( val pageList = state.pageList.toMutableList() pageList.add(newPath) _uiState.postValue(UIState.NormalState(pageList)) + } else { + // result == null means cancellation or error + if (state.isEmpty) { + // close only if no pages have been added yet + _uiState.postValue(UIState.CanceledState) + } } } - - if (result != null) { - val pageList = (uiState.value as UIState.NormalState).pageList.toMutableList() - pageList.add(result) - _uiState.value = UIState.NormalState(pageList) - } } // TODO extract to usecase diff --git a/appscan/src/main/java/com/nextcloud/appscan/AppScanActivity.kt b/appscan/src/main/java/com/nextcloud/appscan/AppScanActivity.kt index 81915a9c73..89fab1a7b6 100644 --- a/appscan/src/main/java/com/nextcloud/appscan/AppScanActivity.kt +++ b/appscan/src/main/java/com/nextcloud/appscan/AppScanActivity.kt @@ -24,6 +24,7 @@ package com.nextcloud.appscan import android.app.Activity import android.content.Intent +import android.content.pm.PackageManager import android.os.Bundle import com.zynksoftware.documentscanner.ScanActivity import com.zynksoftware.documentscanner.model.DocumentScannerErrorModel @@ -40,6 +41,7 @@ class AppScanActivity : ScanActivity() { override fun onError(error: DocumentScannerErrorModel) { // TODO pass this from app somehow? + println(error) } override fun onSuccess(scannerResults: ScannerResults) { @@ -55,9 +57,19 @@ class AppScanActivity : ScanActivity() { } override fun onClose() { + setResult(Activity.RESULT_CANCELED) finish() } + override fun onRequestPermissionsResult(requestCode: Int, permissions: Array, grantResults: IntArray) { + super.onRequestPermissionsResult(requestCode, permissions, grantResults) + if (permissions.contains(android.Manifest.permission.CAMERA) && + (grantResults.isEmpty() || grantResults[0] == PackageManager.PERMISSION_DENIED) + ) { + onClose() + } + } + companion object { @JvmStatic val enabled: Boolean = true