From 567b5ebc04fe5328a8a8a2f44c15915b6a7684ba Mon Sep 17 00:00:00 2001 From: Joris Bodin Date: Wed, 27 Jul 2022 14:27:05 +0200 Subject: [PATCH] Add Camera permission --- .../ui/addFiles/AddFileBottomSheetDialog.kt | 10 +++- .../drive/utils/CameraPermissions.kt | 59 +++++++++++++++++++ .../drive/utils/DrivePermissions.kt | 38 ++++++------ app/src/main/res/values-de/strings.xml | 1 + app/src/main/res/values-es/strings.xml | 1 + app/src/main/res/values-fr/strings.xml | 1 + app/src/main/res/values-it/strings.xml | 1 + app/src/main/res/values/strings.xml | 1 + 8 files changed, 91 insertions(+), 21 deletions(-) create mode 100644 app/src/main/java/com/infomaniak/drive/utils/CameraPermissions.kt diff --git a/app/src/main/java/com/infomaniak/drive/ui/addFiles/AddFileBottomSheetDialog.kt b/app/src/main/java/com/infomaniak/drive/ui/addFiles/AddFileBottomSheetDialog.kt index 78b17b4c57..82b79ed206 100644 --- a/app/src/main/java/com/infomaniak/drive/ui/addFiles/AddFileBottomSheetDialog.kt +++ b/app/src/main/java/com/infomaniak/drive/ui/addFiles/AddFileBottomSheetDialog.kt @@ -58,7 +58,8 @@ class AddFileBottomSheetDialog : BottomSheetDialogFragment() { private lateinit var currentFolderFile: File private val mainViewModel: MainViewModel by activityViewModels() - private lateinit var openCameraPermissions: DrivePermissions + private lateinit var openCameraWritePermissions: DrivePermissions + private lateinit var openCameraPermissions: CameraPermissions private lateinit var uploadFilesPermissions: DrivePermissions private var mediaPhotoPath = "" @@ -88,7 +89,10 @@ class AddFileBottomSheetDialog : BottomSheetDialogFragment() { super.onViewCreated(view, savedInstanceState) currentFolder.setFileItem(currentFolderFile) - openCameraPermissions = DrivePermissions().apply { + openCameraWritePermissions = DrivePermissions().apply { + registerPermissions(this@AddFileBottomSheetDialog) { authorized -> if (authorized) openCamera() } + } + openCameraPermissions = CameraPermissions().apply { registerPermissions(this@AddFileBottomSheetDialog) { authorized -> if (authorized) openCamera() } } uploadFilesPermissions = DrivePermissions().apply { @@ -114,7 +118,7 @@ class AddFileBottomSheetDialog : BottomSheetDialogFragment() { } private fun openCamera() { - if (openCameraPermissions.checkSyncPermissions()) { + if (openCameraWritePermissions.checkSyncPermissions() && openCameraPermissions.checkCameraPermission()) { trackNewElement("takePhotoOrVideo") openCamera.isEnabled = false try { diff --git a/app/src/main/java/com/infomaniak/drive/utils/CameraPermissions.kt b/app/src/main/java/com/infomaniak/drive/utils/CameraPermissions.kt new file mode 100644 index 0000000000..2efa305008 --- /dev/null +++ b/app/src/main/java/com/infomaniak/drive/utils/CameraPermissions.kt @@ -0,0 +1,59 @@ +/* + * Infomaniak kDrive - Android + * Copyright (C) 2022 Infomaniak Network SA + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package com.infomaniak.drive.utils + +import android.Manifest +import android.os.Build +import androidx.activity.result.ActivityResultLauncher +import androidx.activity.result.contract.ActivityResultContracts.RequestMultiplePermissions +import androidx.fragment.app.Fragment +import androidx.fragment.app.FragmentActivity +import com.infomaniak.drive.R +import com.infomaniak.drive.utils.DrivePermissions.Companion.resultPermissions +import com.infomaniak.lib.core.utils.hasPermissions + +class CameraPermissions { + + companion object { + val cameraPermission = arrayOf(Manifest.permission.CAMERA) + } + + private lateinit var registerForActivityResult: ActivityResultLauncher> + private lateinit var activity: FragmentActivity + + fun registerPermissions(fragment: Fragment, onPermissionResult: ((authorized: Boolean) -> Unit)? = null) { + activity = fragment.requireActivity() + registerForActivityResult = + fragment.registerForActivityResult(RequestMultiplePermissions()) { authorizedPermissions -> + val authorized = authorizedPermissions.values.all { it } + onPermissionResult?.invoke(authorized) + activity.resultPermissions(authorized, cameraPermission, R.string.allPermissionNeeded) + } + } + + fun checkCameraPermission(requestPermission: Boolean = true): Boolean { + return when { + Build.VERSION.SDK_INT < Build.VERSION_CODES.M -> true + activity.hasPermissions(cameraPermission) -> true + else -> { + if (requestPermission) registerForActivityResult.launch(cameraPermission) + false + } + } + } +} diff --git a/app/src/main/java/com/infomaniak/drive/utils/DrivePermissions.kt b/app/src/main/java/com/infomaniak/drive/utils/DrivePermissions.kt index b531a5e7af..0db20d1e4c 100644 --- a/app/src/main/java/com/infomaniak/drive/utils/DrivePermissions.kt +++ b/app/src/main/java/com/infomaniak/drive/utils/DrivePermissions.kt @@ -19,6 +19,7 @@ package com.infomaniak.drive.utils import android.Manifest import android.annotation.SuppressLint +import android.app.Activity import android.app.Activity.RESULT_OK import android.content.ActivityNotFoundException import android.content.Context @@ -52,6 +53,17 @@ class DrivePermissions { } else { arrayOf(Manifest.permission.WRITE_EXTERNAL_STORAGE) } + + fun Activity.resultPermissions(authorized: Boolean, permissions: Array, message: Int) { + if (!authorized && !requestPermissionsIsPossible(permissions)) { + MaterialAlertDialogBuilder(this, R.style.DialogStyle) + .setTitle(R.string.androidPermissionTitle) + .setMessage(message) + .setCancelable(false) + .setPositiveButton(R.string.buttonAuthorize) { _: DialogInterface?, _: Int -> startAppSettingsConfig() } + .show() + } + } } private lateinit var batteryPermissionResultLauncher: ActivityResultLauncher @@ -62,18 +74,20 @@ class DrivePermissions { fun registerPermissions(activity: FragmentActivity, onPermissionResult: ((authorized: Boolean) -> Unit)? = null) { this.activity = activity - registerForActivityResult = activity.registerForActivityResult(RequestMultiplePermissions()) { permissions -> - val authorized = permissions.values.all { it == true } - resultPermissions(onPermissionResult, authorized) + registerForActivityResult = activity.registerForActivityResult(RequestMultiplePermissions()) { authorizedPermissions -> + val authorized = authorizedPermissions.values.all { it } + onPermissionResult?.invoke(authorized) + activity.resultPermissions(authorized, permissions, R.string.allPermissionNeeded) } } fun registerPermissions(fragment: Fragment, onPermissionResult: ((authorized: Boolean) -> Unit)? = null) { activity = fragment.requireActivity() registerForActivityResult = - fragment.registerForActivityResult(RequestMultiplePermissions()) { permissions -> - val authorized = permissions.values.all { it == true } - resultPermissions(onPermissionResult, authorized) + fragment.registerForActivityResult(RequestMultiplePermissions()) { authorizedPermissions -> + val authorized = authorizedPermissions.values.all { it } + onPermissionResult?.invoke(authorized) + activity.resultPermissions(authorized, permissions, R.string.allPermissionNeeded) } } @@ -88,18 +102,6 @@ class DrivePermissions { } } - private fun resultPermissions(onPermissionResult: ((authorized: Boolean) -> Unit)?, authorized: Boolean) { - onPermissionResult?.invoke(authorized) - if (!authorized && !activity.requestPermissionsIsPossible(permissions)) { - MaterialAlertDialogBuilder(activity, R.style.DialogStyle) - .setTitle(R.string.androidPermissionTitle) - .setMessage(R.string.allPermissionNeeded) - .setCancelable(false) - .setPositiveButton(R.string.buttonAuthorize) { _: DialogInterface?, _: Int -> activity.startAppSettingsConfig() } - .show() - } - } - /** * Check if the sync has all permissions to work * @return [Boolean] true if the sync has all permissions or false diff --git a/app/src/main/res/values-de/strings.xml b/app/src/main/res/values-de/strings.xml index 30bb273311..650f823f25 100644 --- a/app/src/main/res/values-de/strings.xml +++ b/app/src/main/res/values-de/strings.xml @@ -168,6 +168,7 @@ Mein Angebot erweitern Vom Telefon importieren Validieren + Der Zugang zu Ihrer Kamera ist deaktiviert, Sie können diesen Parameter unter Einstellungen ändern. Kategorien Bank Rechnung diff --git a/app/src/main/res/values-es/strings.xml b/app/src/main/res/values-es/strings.xml index 4ce817eb7e..23587e4851 100644 --- a/app/src/main/res/values-es/strings.xml +++ b/app/src/main/res/values-es/strings.xml @@ -168,6 +168,7 @@ Mejorar mi oferta Importar desde el teléfono Validar + El acceso a tu cámara está desactivado. Puedes cambiar este parámetro en los ajustes. Categorías Banco Factura diff --git a/app/src/main/res/values-fr/strings.xml b/app/src/main/res/values-fr/strings.xml index 74a868e232..6686c5f2af 100644 --- a/app/src/main/res/values-fr/strings.xml +++ b/app/src/main/res/values-fr/strings.xml @@ -168,6 +168,7 @@ Mettre à niveau mon offre Importer depuis le téléphone Valider + L’accès à votre caméra est désactivé, vous pouvez changer ce paramètre dans les réglages. Catégories Banque Facture diff --git a/app/src/main/res/values-it/strings.xml b/app/src/main/res/values-it/strings.xml index 1ca8cff543..bcd5bfcc7c 100644 --- a/app/src/main/res/values-it/strings.xml +++ b/app/src/main/res/values-it/strings.xml @@ -168,6 +168,7 @@ Fai evolvere la tua offerta Importa dal telefono Convalida + L’accesso alla tua fotocamera è disattivato. Puoi modificare questo parametro dalle impostazioni. Categorie Banca Fattura diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index e8429f802c..4f8b83d34a 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -179,6 +179,7 @@ Upgrade my plan Upload from phone Validate + Access to your camera is disabled: you can change this in the Settings. Categories Banking Bill