Skip to content

Commit

Permalink
fix: unnecessary camera permission check
Browse files Browse the repository at this point in the history
* We previously removed the camera permission from the manifest file (commit: fix: remove camera permission #14162, by Brayan). However, the code now includes a check for this permission that was introduced in commit 6a8ede0. Since th permission is not declared, this check fails, and the camera cannot be launched.

* The camera feature is declared in the manifest file as <uses-feature android:name="android.hardware.camera" /> rather than using <uses-permission android:name="android.permission.CAMERA" />. This omission causes the current permission
check to always return false, preventing users from accessing the camera options. This PR corrects that issue by removing that permission check.
  • Loading branch information
criticalAY authored and mikehardy committed Aug 21, 2024
1 parent 1a0f620 commit 353da3e
Show file tree
Hide file tree
Showing 4 changed files with 1 addition and 36 deletions.
2 changes: 0 additions & 2 deletions AnkiDroid/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@
<!-- <uses-feature android:name. We must ensure this is never called when running on API 23.-->
<uses-sdk tools:overrideLibrary="androidx.draganddrop"/>

<uses-feature android:name="android.hardware.camera" android:required="false" />
<uses-feature android:name="android.hardware.camera.any" android:required="false" />
<uses-feature android:name="android.hardware.audio.output" android:required="false" />
<uses-feature android:name="android.software.app_widgets" android:required="false" />
<uses-feature android:name="android.hardware.microphone" android:required="false" />
Expand Down
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 353da3e

Please sign in to comment.