Skip to content

Commit

Permalink
enhancement: add drawing option to multimedia fragment
Browse files Browse the repository at this point in the history
* refactor: compress camera clicked pictures
* refactor: add camera permission check
  • Loading branch information
criticalAY authored and david-allison committed Aug 7, 2024
1 parent 541ef81 commit 6a8ede0
Show file tree
Hide file tree
Showing 6 changed files with 298 additions and 41 deletions.
45 changes: 36 additions & 9 deletions AnkiDroid/src/main/java/com/ichi2/anki/NoteEditor.kt
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ import com.ichi2.anki.multimedia.MultimediaBottomSheet
import com.ichi2.anki.multimedia.MultimediaImageFragment
import com.ichi2.anki.multimedia.MultimediaUtils.createImageFile
import com.ichi2.anki.multimedia.MultimediaViewModel
import com.ichi2.anki.multimediacard.IMultimediaEditableNote
import com.ichi2.anki.multimediacard.fields.AudioRecordingField
import com.ichi2.anki.multimediacard.fields.EFieldType
import com.ichi2.anki.multimediacard.fields.IField
Expand Down Expand Up @@ -503,7 +504,12 @@ class NoteEditor : AnkiFragment(R.layout.note_editor), DeckSelectionListener, Su
Timber.w("Note is null, returning")
return
}
// TODO: start the MultimediaImageFragment with the image intent
openMultimediaImageFragment(
fieldIndex = 0,
field = ImageField(),
multimediaNote = note,
imageUri = imageUri
)
}

override fun onSaveInstanceState(outState: Bundle) {
Expand Down Expand Up @@ -1695,13 +1701,7 @@ class NoteEditor : AnkiFragment(R.layout.note_editor), DeckSelectionListener, Su
Timber.i("Selected Image option")
val field = ImageField()
note.setField(fieldIndex, field)
val imageIntent = MultimediaImageFragment.getIntent(
requireContext(),
MultimediaActivityExtra(fieldIndex, field, note),
MultimediaImageFragment.ImageOptions.GALLERY
)

multimediaFragmentLauncher.launch(imageIntent)
openMultimediaImageFragment(fieldIndex = fieldIndex, field, note)
}

MultimediaBottomSheet.MultimediaAction.SELECT_AUDIO_FILE -> {
Expand All @@ -1718,7 +1718,17 @@ class NoteEditor : AnkiFragment(R.layout.note_editor), DeckSelectionListener, Su
}

MultimediaBottomSheet.MultimediaAction.OPEN_DRAWING -> {
// TODO("Not yet implemented")
Timber.i("Selected Drawing option")
val field = ImageField()
note.setField(fieldIndex, field)

val drawingIntent = MultimediaImageFragment.getIntent(
requireContext(),
MultimediaActivityExtra(fieldIndex, field, note),
MultimediaImageFragment.ImageOptions.DRAWING
)

multimediaFragmentLauncher.launch(drawingIntent)
}

MultimediaBottomSheet.MultimediaAction.SELECT_AUDIO_RECORDING -> {
Expand Down Expand Up @@ -1765,6 +1775,23 @@ class NoteEditor : AnkiFragment(R.layout.note_editor), DeckSelectionListener, Su
}
}

private fun openMultimediaImageFragment(
fieldIndex: Int,
field: IField,
multimediaNote: IMultimediaEditableNote,
imageUri: Uri? = null
) {
val multimediaExtra = MultimediaActivityExtra(fieldIndex, field, multimediaNote, imageUri?.toString())

val imageIntent = MultimediaImageFragment.getIntent(
requireContext(),
multimediaExtra,
MultimediaImageFragment.ImageOptions.GALLERY
)

multimediaFragmentLauncher.launch(imageIntent)
}

private fun handleMultimediaResult(extras: Bundle) {
val index = extras.getInt(MULTIMEDIA_RESULT_FIELD_INDEX)
val field = extras.getSerializableCompat<IField>(MULTIMEDIA_RESULT)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ import com.ichi2.anki.multimedia.MultimediaActivity.Companion.MULTIMEDIA_RESULT
import com.ichi2.anki.multimedia.MultimediaActivity.Companion.MULTIMEDIA_RESULT_FIELD_INDEX
import com.ichi2.anki.multimedia.MultimediaUtils.createCachedFile
import com.ichi2.anki.utils.ext.sharedPrefs
import com.ichi2.annotations.NeedsTest
import com.ichi2.compat.CompatHelper
import com.ichi2.compat.CompatHelper.Companion.getSerializableCompat
import com.ichi2.utils.ExceptionUtil.executeSafe
Expand Down Expand Up @@ -87,6 +88,7 @@ class AudioVideoFragment : MultimediaFragment(R.layout.fragment_audio_video) {
* Lazily initialized instance of MultimediaMenu.
* The instance is created only when first accessed.
*/
@NeedsTest("The menu drawable icon shoule be correctly set")
private val multimediaMenu by lazy {
MultimediaMenuProvider(
menuResId = R.menu.multimedia_menu,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ import kotlin.reflect.jvm.jvmName
data class MultimediaActivityExtra(
val index: Int,
val field: IField,
val note: IMultimediaEditableNote
val note: IMultimediaEditableNote,
val imageUri: String? = null
) : Serializable

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

package com.ichi2.anki.multimedia

import android.net.Uri
import android.os.Build
import android.os.Bundle
import android.text.format.Formatter
import android.view.MenuItem
Expand All @@ -25,10 +27,12 @@ import androidx.annotation.DrawableRes
import androidx.annotation.LayoutRes
import androidx.appcompat.app.AlertDialog
import androidx.core.content.ContextCompat
import androidx.core.content.FileProvider
import androidx.core.view.MenuHost
import androidx.core.view.MenuProvider
import androidx.fragment.app.Fragment
import com.ichi2.anki.AnkiActivity
import com.ichi2.anki.CrashReportService
import com.ichi2.anki.R
import com.ichi2.anki.multimediacard.IMultimediaEditableNote
import com.ichi2.anki.multimediacard.fields.IField
Expand Down Expand Up @@ -56,6 +60,7 @@ abstract class MultimediaFragment(@LayoutRes layout: Int) : Fragment(layout) {
protected var indexValue: Int = 0
protected lateinit var field: IField
protected lateinit var note: IMultimediaEditableNote
protected var imageUri: Uri? = null

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
Expand All @@ -70,10 +75,37 @@ abstract class MultimediaFragment(@LayoutRes layout: Int) : Fragment(layout) {
indexValue = multimediaActivityExtra.index
field = multimediaActivityExtra.field
note = multimediaActivityExtra.note
if (multimediaActivityExtra.imageUri != null) {
imageUri = Uri.parse(multimediaActivityExtra.imageUri)
}
}
}
}

/**
* Get Uri based on current image path
*
* @param file the file to get URI for
* @return current image path's uri
*/
fun getUriForFile(file: File): Uri {
Timber.d("getUriForFile() %s", file)
try {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
return FileProvider.getUriForFile(
requireActivity(),
requireActivity().applicationContext.packageName + ".apkgfileprovider",
file
)
}
} catch (e: Exception) {
// #6628 - What would cause this? Is the fallback is effective? Telemetry to diagnose more:
Timber.w(e, "getUriForFile failed on %s - attempting fallback", file)
CrashReportService.sendExceptionReport(e, "MultimediaFragment", "Unexpected getUriForFile failure on $file", true)
}
return Uri.fromFile(file)
}

fun setMenuItemIcon(menuItem: MenuItem, @DrawableRes icon: Int) {
menuItem.icon = ContextCompat.getDrawable(requireContext(), icon)
}
Expand Down
Loading

0 comments on commit 6a8ede0

Please sign in to comment.