Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add cards access to enhance API functions #17345

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ class CardContentProvider : ContentProvider() {
private const val DECK_SELECTED = 4001
private const val DECKS_ID = 4002
private const val MEDIA = 5000
private const val CARDS = 6000
private val sUriMatcher = UriMatcher(UriMatcher.NO_MATCH)

/**
Expand All @@ -120,6 +121,7 @@ class CardContentProvider : ContentProvider() {
* applied to more columns. "MID", "USN", "MOD" are not really user friendly.
*/
private val sDefaultNoteProjectionDBAccess = FlashCardsContract.Note.DEFAULT_PROJECTION.clone()
private val sDefaultCardProjectionDBAccess = FlashCardsContract.TrueCard.DEFAULT_PROJECTION.clone()

private fun sanitizeNoteProjection(projection: Array<String>?): Array<String> {
if (projection.isNullOrEmpty()) {
Expand All @@ -137,6 +139,22 @@ class CardContentProvider : ContentProvider() {
return sanitized.toTypedArray()
}

private fun sanitizeCardProjection(projection: Array<String>?): Array<String> {
if (projection.isNullOrEmpty()) {
return sDefaultCardProjectionDBAccess
}
val sanitized = ArrayList<String>(projection.size)
for (column in projection) {
val idx = FlashCardsContract.TrueCard.DEFAULT_PROJECTION.indexOf(column)
if (idx >= 0) {
sanitized.add(sDefaultCardProjectionDBAccess[idx])
} else {
throw IllegalArgumentException("Unknown column $column")
}
}
return sanitized.toTypedArray()
}

init {
fun addUri(path: String, code: Int) = sUriMatcher.addURI(FlashCardsContract.AUTHORITY, path, code)
// Here you can see all the URIs at a glance
Expand All @@ -156,12 +174,19 @@ class CardContentProvider : ContentProvider() {
addUri("decks/#", DECKS_ID)
addUri("selected_deck/", DECK_SELECTED)
addUri("media", MEDIA)
addUri("cards", CARDS)

for (idx in sDefaultNoteProjectionDBAccess.indices) {
if (sDefaultNoteProjectionDBAccess[idx] == FlashCardsContract.Note._ID) {
sDefaultNoteProjectionDBAccess[idx] = "id as _id"
}
}

for (idx in sDefaultCardProjectionDBAccess.indices) {
if (sDefaultCardProjectionDBAccess[idx] == FlashCardsContract.TrueCard._ID) {
sDefaultCardProjectionDBAccess[idx] = "id as _id"
}
}
}
}

Expand All @@ -187,6 +212,7 @@ class CardContentProvider : ContentProvider() {
MODELS_ID_TEMPLATES_ID -> FlashCardsContract.CardTemplate.CONTENT_ITEM_TYPE
SCHEDULE -> FlashCardsContract.ReviewInfo.CONTENT_TYPE
DECKS, DECK_SELECTED, DECKS_ID -> FlashCardsContract.Deck.CONTENT_TYPE
CARDS -> FlashCardsContract.TrueCard.CONTENT_TYPE
else -> throw IllegalArgumentException("uri $uri is not supported")
}
}
Expand Down Expand Up @@ -394,6 +420,19 @@ class CardContentProvider : ContentProvider() {
addDeckToCursor(id, name, counts, rv, col, columns)
rv
}
CARDS -> {
/* Search for cards using the libanki browser syntax */
val proj = sanitizeCardProjection(projection)
val query = selection ?: ""
val cardIds = col.findCards(query)
if (cardIds.isNotEmpty()) {
val sel = "id in (${cardIds.joinToString(",")})"
val sql = SQLiteQueryBuilder.buildQueryString(false, "cards", proj, sel, null, null, order, null)
col.db.database.query(sql)
} else {
null
}
}
else -> throw IllegalArgumentException("uri $uri is not supported")
}
}
Expand Down
72 changes: 70 additions & 2 deletions api/src/main/java/com/ichi2/anki/FlashCardsContract.kt
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ import com.ichi2.anki.api.Ease
* models | All models as JSONObjects.
* | Supports query(). For code examples see class description of [Model].
* --------------------------------------------------------------------------------------------------------------------
* model/<model_id> | Direct access to model `model_id` as JSONObject.
* models/<model_id> | Direct access to model `model_id` as JSONObject.
* | Supports query(). For code examples see class description of [Model].
* --------------------------------------------------------------------------------------------------------------------
* ```
Expand Down Expand Up @@ -779,7 +779,7 @@ public object FlashCardsContract {
*/
public const val EASE: String = "answer_ease"

/*
/**
* Time it took to answer the card (in ms)
*/
public const val TIME_TAKEN: String = "time_taken"
Expand Down Expand Up @@ -987,4 +987,72 @@ public object FlashCardsContract {
*/
public const val PREFERRED_NAME: String = "preferred_name"
}

public object TrueCard {
@JvmField // required for Java API
public val CONTENT_URI: Uri = Uri.withAppendedPath(AUTHORITY_URI, "cards")

@Suppress("ConstPropertyName")
public const val _ID: String = "_id"

public const val NID: String = "nid"

public const val DID: String = "did"

public const val ORD: String = "ord"

public const val MOD: String = "mod"

public const val USN: String = "usn"

public const val TYPE: String = "type"

public const val QUEUE: String = "queue"

public const val DUE: String = "due"

public const val IVL: String = "ivl"

public const val FACTOR: String = "factor"

public const val REPS: String = "reps"

public const val LAPSES: String = "lapses"

public const val LEFT: String = "left"

public const val ODUE: String = "odue"

public const val ODID: String = "odid"

public const val FLAGS: String = "flags"

public const val DATA: String = "data"

@JvmField // required for Java API
public val DEFAULT_PROJECTION: Array<String> = arrayOf(
_ID,
NID,
DID,
ORD,
MOD,
USN,
TYPE,
QUEUE,
DUE,
IVL,
FACTOR,
REPS,
LAPSES,
LEFT,
ODUE,
ODID,
FLAGS,
DATA
)

public const val CONTENT_ITEM_TYPE: String = "vnd.android.cursor.item/vnd.com.ichi2.anki.card"

public const val CONTENT_TYPE: String = "vnd.android.cursor.dir/vnd.com.ichi2.anki.card"
}
}