Skip to content

Commit

Permalink
fix: unnecessary camera permission check
Browse files Browse the repository at this point in the history
The camera check relied on the `<uses-permission android:name="android.permission.CAMERA" />` in the manifest, but we declare the camera as a feature `<uses-feature android:name="android.hardware.camera" />` later. This caused the check to always return false, preventing users from accessing camera options.

The camera permission was not there earlier but was introduced in  ankidroid@cd03756, which introduced this issue.
  • Loading branch information
criticalAY committed Aug 8, 2024
1 parent 6a8ede0 commit e3fe714
Show file tree
Hide file tree
Showing 3 changed files with 1 addition and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@

package com.ichi2.anki.multimedia

import android.Manifest
import android.app.Activity
import android.content.Context
import android.content.Intent
Expand Down Expand Up @@ -55,7 +54,6 @@ import com.ichi2.utils.BitmapUtil
import com.ichi2.utils.ExifUtil
import com.ichi2.utils.FileUtil
import com.ichi2.utils.ImageUtils
import com.ichi2.utils.Permissions
import com.ichi2.utils.message
import com.ichi2.utils.negativeButton
import com.ichi2.utils.positiveButton
Expand Down Expand Up @@ -143,6 +141,7 @@ class MultimediaImageFragment : MultimediaFragment(R.layout.fragment_multimedia_
* Launches the device's camera to take a picture.
* This launcher is registered using `ActivityResultContracts.TakePicture()`.
*/
@NeedsTest("Works fine without permission as we use Camera as feature")
private val cameraLauncher =
registerForActivityResult(ActivityResultContracts.TakePicture()) { isPictureTaken ->
when {
Expand Down Expand Up @@ -240,37 +239,10 @@ class MultimediaImageFragment : MultimediaFragment(R.layout.fragment_multimedia_
imagePreview = view.findViewById(R.id.image_preview)
imageFileSize = view.findViewById(R.id.image_size_textview)

if (selectedImageOptions == ImageOptions.CAMERA) {
if (!hasCameraPermission()) {
return
}
}

handleImageUri()
setupDoneButton()
}

private val requestPermissionLauncher = registerForActivityResult(
ActivityResultContracts.RequestPermission()
) { isGranted ->
if (isGranted) {
Timber.d("Camera permission granted")
handleSelectedImageOptions()
} else {
Timber.d("Camera permission denied")
showErrorDialog(resources.getString(R.string.multimedia_editor_camera_permission_refused))
}
}

private fun hasCameraPermission(): Boolean {
if (!Permissions.canRecordAudio(requireContext())) {
Timber.i("Requesting Audio Permissions")
requestPermissionLauncher.launch(Manifest.permission.CAMERA)
return false
}
return true
}

private fun handleImageUri() {
if (imageUri != null) {
view?.findViewById<TextView>(R.id.no_image_textview)?.visibility = View.GONE
Expand Down
4 changes: 0 additions & 4 deletions AnkiDroid/src/main/java/com/ichi2/utils/Permissions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,6 @@ object Permissions {
Manifest.permission.WRITE_EXTERNAL_STORAGE
)

fun canUseCamera(context: Context): Boolean {
return hasPermission(context, Manifest.permission.CAMERA)
}

fun canRecordAudio(context: Context): Boolean {
return hasPermission(context, Manifest.permission.RECORD_AUDIO)
}
Expand Down
1 change: 0 additions & 1 deletion AnkiDroid/src/main/res/values/03-dialogs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,6 @@

<!-- Multimedia - Edit Field Activity -->
<string name="multimedia_editor_audio_permission_refused">Could not obtain microphone permission.</string>
<string name="multimedia_editor_camera_permission_refused">Could not obtain camera permission. Functionality has been disabled.</string>
<string name="multimedia_editor_failed">Failed to open Multimedia Editor</string>

<!-- Multimedia - Audio View -->
Expand Down

0 comments on commit e3fe714

Please sign in to comment.