|
|
|
@ -12,6 +12,7 @@ import android.net.Uri |
|
|
|
|
import com.google.gson.Gson |
|
|
|
|
import io.legado.app.api.controller.BookController |
|
|
|
|
import io.legado.app.api.controller.BookSourceController |
|
|
|
|
import io.legado.app.api.controller.RssSourceController |
|
|
|
|
import java.util.* |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
@ -19,20 +20,26 @@ import java.util.* |
|
|
|
|
*/ |
|
|
|
|
class ReaderProvider : ContentProvider() { |
|
|
|
|
private enum class RequestCode { |
|
|
|
|
SaveSource, SaveSources, SaveBook, DeleteSources, GetSource, GetSources, |
|
|
|
|
GetBookshelf, RefreshToc, GetChapterList, GetBookContent, GetBookCover |
|
|
|
|
SaveBookSource, SaveBookSources, DeleteBookSources, GetBookSource, GetBookSources, |
|
|
|
|
SaveRssSource, SaveRssSources, DeleteRssSources, GetRssSource, GetRssSources, |
|
|
|
|
SaveBook, GetBookshelf, RefreshToc, GetChapterList, GetBookContent, GetBookCover |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private val postBodyKey = "json" |
|
|
|
|
private val sMatcher by lazy { |
|
|
|
|
UriMatcher(UriMatcher.NO_MATCH).apply { |
|
|
|
|
"${context?.applicationInfo?.packageName}.readerProvider".also { authority -> |
|
|
|
|
addURI(authority, "source/insert", RequestCode.SaveSource.ordinal) |
|
|
|
|
addURI(authority, "sources/insert", RequestCode.SaveSources.ordinal) |
|
|
|
|
addURI(authority, "bookSource/insert", RequestCode.SaveBookSource.ordinal) |
|
|
|
|
addURI(authority, "bookSources/insert", RequestCode.SaveBookSources.ordinal) |
|
|
|
|
addURI(authority, "bookSources/delete", RequestCode.DeleteBookSources.ordinal) |
|
|
|
|
addURI(authority, "bookSource/query", RequestCode.GetBookSource.ordinal) |
|
|
|
|
addURI(authority, "bookSources/query", RequestCode.GetBookSources.ordinal) |
|
|
|
|
addURI(authority, "rssSource/insert", RequestCode.SaveBookSource.ordinal) |
|
|
|
|
addURI(authority, "rssSources/insert", RequestCode.SaveBookSources.ordinal) |
|
|
|
|
addURI(authority, "rssSources/delete", RequestCode.DeleteBookSources.ordinal) |
|
|
|
|
addURI(authority, "rssSource/query", RequestCode.GetBookSource.ordinal) |
|
|
|
|
addURI(authority, "rssSources/query", RequestCode.GetBookSources.ordinal) |
|
|
|
|
addURI(authority, "book/insert", RequestCode.SaveBook.ordinal) |
|
|
|
|
addURI(authority, "sources/delete", RequestCode.DeleteSources.ordinal) |
|
|
|
|
addURI(authority, "source/query", RequestCode.GetSource.ordinal) |
|
|
|
|
addURI(authority, "sources/query", RequestCode.GetSources.ordinal) |
|
|
|
|
addURI(authority, "books/query", RequestCode.GetBookshelf.ordinal) |
|
|
|
|
addURI(authority, "book/refreshToc/query", RequestCode.RefreshToc.ordinal) |
|
|
|
|
addURI(authority, "book/chapter/query", RequestCode.GetChapterList.ordinal) |
|
|
|
@ -51,7 +58,8 @@ class ReaderProvider : ContentProvider() { |
|
|
|
|
): Int { |
|
|
|
|
if (sMatcher.match(uri) < 0) return -1 |
|
|
|
|
when (RequestCode.values()[sMatcher.match(uri)]) { |
|
|
|
|
RequestCode.DeleteSources -> BookSourceController.deleteSources(selection) |
|
|
|
|
RequestCode.DeleteBookSources -> BookSourceController.deleteSources(selection) |
|
|
|
|
RequestCode.DeleteRssSources -> BookSourceController.deleteSources(selection) |
|
|
|
|
else -> throw IllegalStateException( |
|
|
|
|
"Unexpected value: " + RequestCode.values()[sMatcher.match(uri)].name |
|
|
|
|
) |
|
|
|
@ -64,15 +72,21 @@ class ReaderProvider : ContentProvider() { |
|
|
|
|
override fun insert(uri: Uri, values: ContentValues?): Uri? { |
|
|
|
|
if (sMatcher.match(uri) < 0) return null |
|
|
|
|
when (RequestCode.values()[sMatcher.match(uri)]) { |
|
|
|
|
RequestCode.SaveSource -> values?.let { |
|
|
|
|
RequestCode.SaveBookSource -> values?.let { |
|
|
|
|
BookSourceController.saveSource(values.getAsString(postBodyKey)) |
|
|
|
|
} |
|
|
|
|
RequestCode.SaveBookSources -> values?.let { |
|
|
|
|
BookSourceController.saveSources(values.getAsString(postBodyKey)) |
|
|
|
|
} |
|
|
|
|
RequestCode.SaveRssSource -> values?.let { |
|
|
|
|
RssSourceController.saveSource(values.getAsString(postBodyKey)) |
|
|
|
|
} |
|
|
|
|
RequestCode.SaveRssSources -> values?.let { |
|
|
|
|
RssSourceController.saveSources(values.getAsString(postBodyKey)) |
|
|
|
|
} |
|
|
|
|
RequestCode.SaveBook -> values?.let { |
|
|
|
|
BookController.saveBook(values.getAsString(postBodyKey)) |
|
|
|
|
} |
|
|
|
|
RequestCode.SaveSources -> values?.let { |
|
|
|
|
BookSourceController.saveSources(values.getAsString(postBodyKey)) |
|
|
|
|
} |
|
|
|
|
else -> throw IllegalStateException( |
|
|
|
|
"Unexpected value: " + RequestCode.values()[sMatcher.match(uri)].name |
|
|
|
|
) |
|
|
|
@ -95,8 +109,10 @@ class ReaderProvider : ContentProvider() { |
|
|
|
|
map["path"] = arrayListOf(it) |
|
|
|
|
} |
|
|
|
|
return if (sMatcher.match(uri) < 0) null else when (RequestCode.values()[sMatcher.match(uri)]) { |
|
|
|
|
RequestCode.GetSource -> SimpleCursor(BookSourceController.getSource(map)) |
|
|
|
|
RequestCode.GetSources -> SimpleCursor(BookSourceController.sources) |
|
|
|
|
RequestCode.GetBookSource -> SimpleCursor(BookSourceController.getSource(map)) |
|
|
|
|
RequestCode.GetBookSources -> SimpleCursor(BookSourceController.sources) |
|
|
|
|
RequestCode.GetRssSource -> SimpleCursor(RssSourceController.getSource(map)) |
|
|
|
|
RequestCode.GetRssSources -> SimpleCursor(RssSourceController.sources) |
|
|
|
|
RequestCode.GetBookshelf -> SimpleCursor(BookController.bookshelf) |
|
|
|
|
RequestCode.GetBookContent -> SimpleCursor(BookController.getBookContent(map)) |
|
|
|
|
RequestCode.RefreshToc -> SimpleCursor(BookController.refreshToc(map)) |
|
|
|
|