Recently Written · git

subread-dictionary

Pop-up dictionary for Android that reads Yomitan dictionaries, with local audio

git clone https://github.com/equwal/subread-dictionary

Log | Files | Refs


app/src/main/kotlin/space/subread/dictionary/Anki.kt (2390 bytes)

1 package space.subread.dictionary
2 
3 import android.content.ClipData
4 import android.content.Context
5 import android.content.Intent
6 import android.net.Uri
7 import androidx.core.content.FileProvider
8 import java.io.File
9 
10 /**
11  * Sends a card to SubRead Anki. The strings are the intent contract of that app, see its
12  * docs/intent-api.md. SubRead Anki adds the sentence and the screenshot of the moment, the
13  * sentence audio, and puts the card in AnkiDroid.
14  */
15 object Anki {
16     const val PACKAGE = "space.subread.anki"
17     const val INSTALL = "https://github.com/equwal/subread-anki/releases/latest"
18     private const val ACTION_ADD = "space.subread.anki.action.ADD"
19     private const val EXTRA_WORD = "space.subread.anki.extra.WORD"
20     private const val EXTRA_READING = "space.subread.anki.extra.READING"
21     private const val EXTRA_DEFINITION = "space.subread.anki.extra.DEFINITION"
22     private const val EXTRA_SENTENCE = "space.subread.anki.extra.SENTENCE"
23     private const val EXTRA_AUDIO = "space.subread.anki.extra.AUDIO"
24     private const val EXTRA_SOURCE = "space.subread.anki.extra.SOURCE"
25     private const val EXTRA_SCREENSHOT = "space.subread.anki.extra.SCREENSHOT"
26 
27     fun installed(context: Context): Boolean =
28         context.packageManager.resolveActivity(Intent(ACTION_ADD).setPackage(PACKAGE), 0) != null
29 
30     /**
31      * The intent for one card. The audio file, when there is one, goes through the file provider
32      * of this app; the clip data of the intent carries the read permission for it.
33      */
34     fun intent(context: Context, word: String, reading: String, definition: String, sentence: String, source: String, audio: File?): Intent {
35         val intent = Intent(ACTION_ADD)
36             .setPackage(PACKAGE)
37             .putExtra(EXTRA_WORD, word)
38             .putExtra(EXTRA_READING, reading)
39             .putExtra(EXTRA_DEFINITION, definition)
40             .putExtra(EXTRA_SENTENCE, sentence)
41             .putExtra(EXTRA_SOURCE, source)
42             .putExtra(EXTRA_SCREENSHOT, true)
43         if (audio != null) {
44             val uri: Uri = FileProvider.getUriForFile(context, context.packageName + ".files", audio)
45             intent.putExtra(EXTRA_AUDIO, uri.toString())
46             intent.clipData = ClipData.newUri(context.contentResolver, "audio", uri)
47             intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
48         }
49         return intent
50     }
51 }