|
14 | 14 | */ |
15 | 15 | package org.fairscan.app |
16 | 16 |
|
| 17 | +import android.Manifest |
17 | 18 | import android.content.ActivityNotFoundException |
18 | 19 | import android.content.Intent |
19 | 20 | import android.content.pm.PackageManager |
| 21 | +import android.content.pm.PackageManager.PERMISSION_GRANTED |
20 | 22 | import android.media.MediaScannerConnection |
21 | 23 | import android.net.Uri |
| 24 | +import android.os.Build.VERSION.SDK_INT |
| 25 | +import android.os.Build.VERSION_CODES.Q |
22 | 26 | import android.os.Bundle |
23 | 27 | import android.util.Log |
24 | 28 | import android.widget.Toast |
25 | 29 | import androidx.activity.ComponentActivity |
| 30 | +import androidx.activity.compose.ManagedActivityResultLauncher |
| 31 | +import androidx.activity.compose.rememberLauncherForActivityResult |
26 | 32 | import androidx.activity.compose.setContent |
27 | 33 | import androidx.activity.enableEdgeToEdge |
| 34 | +import androidx.activity.result.contract.ActivityResultContracts |
28 | 35 | import androidx.activity.viewModels |
29 | 36 | import androidx.compose.runtime.getValue |
| 37 | +import androidx.compose.ui.platform.LocalContext |
| 38 | +import androidx.core.content.ContextCompat.checkSelfPermission |
30 | 39 | import androidx.core.content.FileProvider |
31 | 40 | import androidx.core.net.toFile |
32 | 41 | import androidx.core.net.toUri |
@@ -59,10 +68,22 @@ class MainActivity : ComponentActivity() { |
59 | 68 | } |
60 | 69 | enableEdgeToEdge() |
61 | 70 | setContent { |
| 71 | + val context = LocalContext.current |
62 | 72 | val currentScreen by viewModel.currentScreen.collectAsStateWithLifecycle() |
63 | 73 | val liveAnalysisState by viewModel.liveAnalysisState.collectAsStateWithLifecycle() |
64 | 74 | val document by viewModel.documentUiModel.collectAsStateWithLifecycle() |
65 | 75 | val cameraPermission = rememberCameraPermissionState() |
| 76 | + val savePdf = { savePdf(viewModel.getFinalPdf(), viewModel) } |
| 77 | + val storagePermissionLauncher = rememberLauncherForActivityResult( |
| 78 | + ActivityResultContracts.RequestPermission() |
| 79 | + ) { isGranted -> |
| 80 | + if (isGranted) { |
| 81 | + savePdf() |
| 82 | + } else { |
| 83 | + val message = getString(R.string.storage_permission_denied) |
| 84 | + Toast.makeText(context, message, Toast.LENGTH_SHORT).show() |
| 85 | + } |
| 86 | + } |
66 | 87 | MyScanTheme { |
67 | 88 | val navigation = Navigation( |
68 | 89 | toHomeScreen = { viewModel.navigateTo(Screen.Main.Home) }, |
@@ -112,7 +133,7 @@ class MainActivity : ComponentActivity() { |
112 | 133 | setFilename = viewModel::setFilename, |
113 | 134 | uiStateFlow = viewModel.pdfUiState, |
114 | 135 | sharePdf = { sharePdf(viewModel.getFinalPdf(), viewModel) }, |
115 | | - savePdf = { savePdf(viewModel.getFinalPdf(), viewModel) }, |
| 136 | + savePdf = { checkPermissionThen(storagePermissionLauncher, savePdf) }, |
116 | 137 | openPdf = { openPdf(viewModel.pdfUiState.value.savedFileUri) } |
117 | 138 | ), |
118 | 139 | onCloseScan = { |
@@ -154,6 +175,19 @@ class MainActivity : ComponentActivity() { |
154 | 175 | startActivity(chooser) |
155 | 176 | } |
156 | 177 |
|
| 178 | + private fun checkPermissionThen( |
| 179 | + requestPermissionLauncher: ManagedActivityResultLauncher<String, Boolean>, |
| 180 | + action: () -> Unit |
| 181 | + ) { |
| 182 | + val permission = Manifest.permission.WRITE_EXTERNAL_STORAGE |
| 183 | + if (SDK_INT < Q && checkSelfPermission(this, permission) != PERMISSION_GRANTED |
| 184 | + ) { |
| 185 | + requestPermissionLauncher.launch(permission) |
| 186 | + } else { |
| 187 | + action() |
| 188 | + } |
| 189 | + } |
| 190 | + |
157 | 191 | private fun savePdf(generatedPdf: GeneratedPdf?, viewModel: MainViewModel) { |
158 | 192 | if (generatedPdf == null) |
159 | 193 | return |
|
0 commit comments