Merge pull request 'crunchyroll support' (#49) from feature/crunchyroll into develop

Reviewed-on: #49
This commit is contained in:
Jannik 2022-03-19 20:42:54 +01:00
commit 0aece1d8fa
49 changed files with 2485 additions and 1406 deletions

View File

@ -1,14 +1,13 @@
# Teapod
Teapod is a unofficial App for Anime on Demand (AoD). It allows you to watch all your favourite animes from AoD on your android device. To use Teapod you need to have a subscription to AoD.
Teapod is a unofficial App for Crunchyroll. It allows you to watch all your favourite animes from Crunchyroll on your android device. To use Teapod you need to have a account at Crunchyroll.
[<img src="https://fdroid.gitlab.io/artwork/badge/get-it-on.png" height="75">](https://f-droid.org/de/packages/org.mosad.teapod/)
## Features
* Watch all animes from AoD on your Android device
* Watch all animes from Crunchyroll on your Android device
* Native Player based on ExoPayer
* Prefer the OmU version via the app settings
* Save your favorite animes to "My List"
## Screenshots
[<img src="https://www.mosad.xyz/images/Teapod/Teapod_Home.webp" width=180>](https://www.mosad.xyz/images/Teapod/Teapod_Home.webp)
@ -17,14 +16,14 @@ Teapod is a unofficial App for Anime on Demand (AoD). It allows you to watch all
[<img src="https://www.mosad.xyz/images/Teapod/Teapod_Search.webp" width=180>](https://www.mosad.xyz/images/Teapod/Teapod_Search.webp)
### License
Teapod is licensed under the terms and conditions of GPL 3. This Project is not associated with Anime on Demand in any way. But they allow open source apps for their service.
Teapod is licensed under the terms and conditions of GPL 3. This Project is not associated with Crunchyroll in any way.
### Contributing
Currentl you need to have an AoD account to contrtibut to Teapod. Contributing without on is kind of impossible.If you want to contribute to Teapod and need an account on this gitea instance, please write me an email.
Currently you need to have an Crunchyroll account to contribute to Teapod. Contributing without one is impossible.If you want to contribute to Teapod and need an account on this gitea instance, please write an email.
### [FAQ](https://git.mosad.xyz/Seil0/teapod/wiki#hilfe)
#### Why is it called Teapod?
Teapod is a Acronym for "The ultimate anime app on demand", hence this project is called Teapod and not Teapot.
Teapod © 2020-2021 [@Seil0](https://git.mosad.xyz/Seil0)
Teapod © 2020-2022 [@Seil0](https://git.mosad.xyz/Seil0)

View File

@ -1,6 +1,9 @@
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
plugins {
id 'com.android.application'
id 'kotlin-android'
id 'kotlin-android-extensions'
id 'org.jetbrains.kotlin.plugin.serialization' version "$kotlin_version"
}
android {
compileSdkVersion 30
@ -10,8 +13,8 @@ android {
applicationId "org.mosad.teapod"
minSdkVersion 23
targetSdkVersion 30
versionCode 4200 //00.04.200
versionName "0.5.0-alpha2"
versionCode 9000 //00.09.000
versionName "1.0.0-beta1"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
resValue "string", "build_time", buildTime()
@ -43,6 +46,7 @@ dependencies {
implementation fileTree(dir: "libs", include: ["*.jar"])
implementation "org.jetbrains.kotlin:kotlin-reflect:$kotlin_version"
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.5.2'
implementation 'org.jetbrains.kotlinx:kotlinx-serialization-json:1.3.1'
implementation 'androidx.core:core-ktx:1.6.0'
implementation 'androidx.appcompat:appcompat:1.3.1'
@ -55,18 +59,21 @@ dependencies {
implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.3.1'
implementation 'com.google.android.material:material:1.4.0'
implementation 'com.google.code.gson:gson:2.8.8'
implementation 'com.google.code.gson:gson:2.8.8' // TODO remove, still used by metadb
implementation 'com.google.android.exoplayer:exoplayer-core:2.15.0'
implementation 'com.google.android.exoplayer:exoplayer-hls:2.15.0'
implementation 'com.google.android.exoplayer:exoplayer-dash:2.15.0'
implementation 'com.google.android.exoplayer:exoplayer-ui:2.15.0'
implementation 'com.google.android.exoplayer:extension-mediasession:2.15.0'
implementation 'org.jsoup:jsoup:1.14.2'
implementation 'com.github.bumptech.glide:glide:4.12.0'
implementation 'jp.wasabeef:glide-transformations:4.3.0'
implementation 'com.afollestad.material-dialogs:core:3.3.0'
implementation 'com.afollestad.material-dialogs:bottomsheets:3.3.0'
implementation 'com.afollestad.material-dialogs:core:3.3.0' // TODO remove once unused
implementation 'com.afollestad.material-dialogs:bottomsheets:3.3.0' // TODO remove once unused
implementation "io.ktor:ktor-client-core:$ktor_version"
implementation "io.ktor:ktor-client-android:$ktor_version"
implementation "io.ktor:ktor-client-serialization:$ktor_version"
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'

View File

@ -22,10 +22,40 @@
#-renamesourcefileattribute SourceFile
-keep class org.mosad.teapod.util.** { <fields>; }
-keep class org.json.** { *; }
#Gson
-keepattributes Signature
-dontwarn sun.misc.**
# kotlinx.serialization
# Keep `Companion` object fields of serializable classes.
# This avoids serializer lookup through `getDeclaredClasses` as done for named companion objects.
-if @kotlinx.serialization.Serializable class **
-keepclassmembers class <1> {
static <1>$Companion Companion;
}
# Keep `serializer()` on companion objects (both default and named) of serializable classes.
-if @kotlinx.serialization.Serializable class ** {
static **$* *;
}
-keepclassmembers class <1>$<3> {
kotlinx.serialization.KSerializer serializer(...);
}
# Keep `INSTANCE.serializer()` of serializable objects.
-if @kotlinx.serialization.Serializable class ** {
public static ** INSTANCE;
}
-keepclassmembers class <1> {
public static <1> INSTANCE;
kotlinx.serialization.KSerializer serializer(...);
}
# @Serializable and @Polymorphic are used at runtime for polymorphic serialization.
-keepattributes RuntimeVisibleAnnotations,AnnotationDefault
#misc
-dontwarn java.lang.instrument.ClassFileTransformer
-dontwarn java.lang.ClassValue

View File

@ -1,472 +0,0 @@
/**
* Teapod
*
* Copyright 2020-2021 <seil0@mosad.xyz>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301, USA.
*
*/
package org.mosad.teapod.parser
import android.util.Log
import com.google.gson.JsonParser
import kotlinx.coroutines.*
import org.jsoup.Connection
import org.jsoup.Jsoup
import org.mosad.teapod.preferences.EncryptedPreferences
import org.mosad.teapod.util.*
import org.mosad.teapod.util.DataTypes.MediaType
import java.io.IOException
import java.net.CookieStore
import java.util.*
import kotlin.random.Random
import kotlin.reflect.jvm.jvmName
object AoDParser {
private const val baseUrl = "https://www.anime-on-demand.de"
private const val loginPath = "/users/sign_in"
private const val libraryPath = "/animes"
private const val subscriptionPath = "/mypools"
private const val userAgent = "Mozilla/5.0 (X11; Linux x86_64; rv:84.0) Gecko/20100101 Firefox/84.0"
private lateinit var cookieStore: CookieStore
private var csrfToken: String = ""
private var loginSuccess = false
private val aodMediaList = arrayListOf<AoDMedia>() // actual media (data)
// gui media
val guiMediaList = arrayListOf<ItemMedia>()
val highlightsList = arrayListOf<ItemMedia>()
val newEpisodesList = arrayListOf<ItemMedia>()
val newSimulcastsList = arrayListOf<ItemMedia>()
val newTitlesList = arrayListOf<ItemMedia>()
val topTenList = arrayListOf<ItemMedia>()
fun login(): Boolean = runBlocking {
withContext(Dispatchers.IO) {
// get the authenticity token and cookies
val conAuth = Jsoup.connect(baseUrl + loginPath)
.header("User-Agent", userAgent)
cookieStore = conAuth.cookieStore()
csrfToken = conAuth.execute().parse().select("meta[name=csrf-token]").attr("content")
Log.d(AoDParser::class.jvmName, "Received authenticity token: $csrfToken")
Log.d(AoDParser::class.jvmName, "Received authenticity cookies: $cookieStore")
val data = mapOf(
Pair("user[login]", EncryptedPreferences.login),
Pair("user[password]", EncryptedPreferences.password),
Pair("user[remember_me]", "1"),
Pair("commit", "Einloggen"),
Pair("authenticity_token", csrfToken)
)
val resLogin = Jsoup.connect(baseUrl + loginPath)
.method(Connection.Method.POST)
.timeout(60000) // login can take some time default is 60000 (60 sec)
.data(data)
.postDataCharset("UTF-8")
.cookieStore(cookieStore)
.execute()
//println(resLogin.body())
loginSuccess = resLogin.body().contains("Hallo, du bist jetzt angemeldet.")
Log.i(AoDParser::class.jvmName, "Status: ${resLogin.statusCode()} (${resLogin.statusMessage()}), login successful: $loginSuccess")
loginSuccess
}
}
/**
* initially load all media and home screen data
*/
suspend fun initialLoading() {
coroutineScope {
launch { loadHome() }
launch { listAnimes() }
}
}
/**
* get a media by it's ID (int)
* @param aodId The AoD ID of the requested media
* @return returns a AoDMedia of type Movie or TVShow if found, else return AoDMediaNone
*/
suspend fun getMediaById(aodId: Int): AoDMedia {
return aodMediaList.firstOrNull { it.aodId == aodId } ?:
try {
loadMediaAsync(aodId).await().apply {
aodMediaList.add(this)
}
} catch (exn:NullPointerException) {
Log.e(AoDParser::class.jvmName, "Error while loading media $aodId", exn)
AoDMediaNone
}
}
/**
* get subscription info from aod website, remove "Anime-Abo" Prefix and trim
*/
suspend fun getSubscriptionInfoAsync(): Deferred<String> {
return coroutineScope {
async(Dispatchers.IO) {
val res = Jsoup.connect(baseUrl + subscriptionPath)
.cookieStore(cookieStore)
.get()
return@async res.select("a:contains(Anime-Abo)").text()
.removePrefix("Anime-Abo").trim()
}
}
}
fun getSubscriptionUrl(): String {
return baseUrl + subscriptionPath
}
suspend fun markAsWatched(aodId: Int, episodeId: Int) {
val episode = getMediaById(aodId).getEpisodeById(episodeId)
episode.watched = true
sendCallback(episode.watchedCallback)
Log.d(AoDParser::class.jvmName, "Marked episode ${episode.mediaId} as watched")
}
// TODO don't use jsoup here
private suspend fun sendCallback(callbackPath: String) = coroutineScope {
launch(Dispatchers.IO) {
val headers = mutableMapOf(
Pair("Accept", "application/json, text/javascript, */*; q=0.01"),
Pair("Accept-Language", "de,en-US;q=0.7,en;q=0.3"),
Pair("Accept-Encoding", "gzip, deflate, br"),
Pair("X-CSRF-Token", csrfToken),
Pair("X-Requested-With", "XMLHttpRequest"),
)
try {
Jsoup.connect(baseUrl + callbackPath)
.ignoreContentType(true)
.cookieStore(cookieStore)
.headers(headers)
.execute()
} catch (ex: IOException) {
Log.e(AoDParser::class.jvmName, "Callback for $callbackPath failed.", ex)
}
}
}
/**
* load all media from aod into itemMediaList and mediaList
* TODO private suspend fun listAnimes() = withContext(Dispatchers.IO) should also work, maybe a bug in android studio?
*/
private suspend fun listAnimes() = withContext(Dispatchers.IO) {
launch(Dispatchers.IO) {
val resAnimes = Jsoup.connect(baseUrl + libraryPath).get()
//println(resAnimes)
guiMediaList.clear()
val animes = resAnimes.select("div.animebox")
guiMediaList.addAll(
animes.map {
ItemMedia(
id = it.select("p.animebox-link").select("a")
.attr("href").substringAfterLast("/").toInt(),
title = it.select("h3.animebox-title").text(),
posterUrl = it.select("p.animebox-image").select("img")
.attr("src")
)
}
)
Log.i(AoDParser::class.jvmName, "Total library size is: ${guiMediaList.size}")
}
}
/**
* load new episodes, titles and highlights
*/
private suspend fun loadHome() = withContext(Dispatchers.IO) {
launch(Dispatchers.IO) {
val resHome = Jsoup.connect(baseUrl).get()
// get highlights from AoD
highlightsList.clear()
resHome.select("#aod-highlights").select("div.news-item").forEach {
val mediaId = it.select("div.news-item-text").select("a.serienlink")
.attr("href").substringAfterLast("/").toIntOrNull()
val mediaTitle = it.select("div.news-title").select("h2").text()
val mediaImage = it.select("img").attr("src")
if (mediaId != null) {
highlightsList.add(ItemMedia(mediaId, mediaTitle, mediaImage))
}
}
// get all new episodes from AoD
newEpisodesList.clear()
resHome.select("h2:contains(Neue Episoden)").next().select("li").forEach {
val mediaId = it.select("a.thumbs").attr("href")
.substringAfterLast("/").toIntOrNull()
val mediaImage = it.select("a.thumbs > img").attr("src")
val mediaTitle = "${it.select("a").text()} - ${it.select("span.neweps").text()}"
if (mediaId != null) {
newEpisodesList.add(ItemMedia(mediaId, mediaTitle, mediaImage))
}
}
// get new simulcasts from AoD
newSimulcastsList.clear()
resHome.select("h2:contains(Neue Simulcasts)").next().select("li").forEach {
val mediaId = it.select("a.thumbs").attr("href")
.substringAfterLast("/").toIntOrNull()
val mediaImage = it.select("a.thumbs > img").attr("src")
val mediaTitle = it.select("a").text()
if (mediaId != null) {
newSimulcastsList.add(ItemMedia(mediaId, mediaTitle, mediaImage))
}
}
// get new titles from AoD
newTitlesList.clear()
resHome.select("h2:contains(Neue Anime-Titel)").next().select("li").forEach {
val mediaId = it.select("a.thumbs").attr("href")
.substringAfterLast("/").toIntOrNull()
val mediaImage = it.select("a.thumbs > img").attr("src")
val mediaTitle = it.select("a").text()
if (mediaId != null) {
newTitlesList.add(ItemMedia(mediaId, mediaTitle, mediaImage))
}
}
// get top ten from AoD
topTenList.clear()
resHome.select("h2:contains(Anime Top 10)").next().select("li").forEach {
val mediaId = it.select("a.thumbs").attr("href")
.substringAfterLast("/").toIntOrNull()
val mediaImage = it.select("a.thumbs > img").attr("src")
val mediaTitle = it.select("a").text()
if (mediaId != null) {
topTenList.add(ItemMedia(mediaId, mediaTitle, mediaImage))
}
}
// if highlights is empty, add a random new title
if (highlightsList.isEmpty()) {
if (newTitlesList.isNotEmpty()) {
highlightsList.add(newTitlesList[Random.nextInt(0, newTitlesList.size)])
} else {
highlightsList.add(ItemMedia(0,"", ""))
}
}
Log.i(AoDParser::class.jvmName, "loaded home")
}
}
/**
* TODO catch SocketTimeoutException from loading to show a waring dialog
* Load media async. Every media has a playlist.
* @param aodId The AoD ID of the requested media
*/
private suspend fun loadMediaAsync(aodId: Int): Deferred<AoDMedia> = coroutineScope {
return@coroutineScope async (Dispatchers.IO) {
if (cookieStore.cookies.isEmpty()) login() // TODO is this needed?
// return none object, if login wasn't successful
if (!loginSuccess) {
Log.w(AoDParser::class.jvmName, "Login was not successful")
return@async AoDMediaNone
}
// get the media page
val res = Jsoup.connect("$baseUrl/anime/$aodId")
.cookieStore(cookieStore)
.get()
// println(res)
if (csrfToken.isEmpty()) {
csrfToken = res.select("meta[name=csrf-token]").attr("content")
Log.d(AoDParser::class.jvmName, "New csrf token is $csrfToken")
}
// playlist parsing TODO can this be async to the general info parsing?
val besides = res.select("div.besides").first()!!
val aodPlaylists = besides.select("input.streamstarter_html5").map { streamstarter ->
parsePlaylistAsync(
streamstarter.attr("data-playlist"),
streamstarter.attr("data-lang")
)
}
/**
* generic aod media data
*/
val title = res.select("h1[itemprop=name]").text()
val description = res.select("div[itemprop=description]").text()
val posterURL = res.select("img.fullwidth-image").attr("src")
val type = when {
posterURL.contains("films") -> MediaType.MOVIE
posterURL.contains("series") -> MediaType.TVSHOW
else -> MediaType.OTHER
}
var year = 0
var age = 0
res.select("table.vertical-table").select("tr").forEach { row ->
when (row.select("th").text().lowercase(Locale.ROOT)) {
"produktionsjahr" -> year = row.select("td").text().toInt()
"fsk" -> age = row.select("td").text().toInt()
}
}
// similar titles from media page
val similar = res.select("h2:contains(Ähnliche Animes)").next().select("li").mapNotNull {
val mediaId = it.select("a.thumbs").attr("href")
.substringAfterLast("/").toIntOrNull()
val mediaImage = it.select("a.thumbs > img").attr("src")
val mediaTitle = it.select("a").text()
if (mediaId != null) {
ItemMedia(mediaId, mediaTitle, mediaImage)
} else {
Log.i(AoDParser::class.jvmName, "MediaId for similar to $aodId was null")
null
}
}
/**
* additional information for episodes:
* description: a short description of the episode
* watched: indicates if the episodes has been watched
* watched callback: url to set watched in aod
*/
val episodesInfo: Map<Int, AoDEpisodeInfo> = if (type == MediaType.TVSHOW) {
res.select("div.three-box-container > div.episodebox").mapNotNull { episodeBox ->
// make sure the episode has a streaming link
if (episodeBox.select("input.streamstarter_html5").isNotEmpty()) {
val mediaId = episodeBox.select("div.flip-front").attr("id").substringAfter("-").toInt()
val episodeShortDesc = episodeBox.select("p.episodebox-shorttext").text()
val episodeWatched = episodeBox.select("div.episodebox-icons > div").hasClass("status-icon-orange")
val episodeWatchedCallback = episodeBox.select("input.streamstarter_html5").eachAttr("data-playlist").first()
AoDEpisodeInfo(mediaId, episodeShortDesc, episodeWatched, episodeWatchedCallback)
} else {
Log.i(AoDParser::class.jvmName, "Episode info for $aodId has empty streamstarter_html5 ")
null
}
}.associateBy { it.aodMediaId }
} else {
mapOf()
}
// map the aod api playlist to a teapod playlist
val playlist: List<AoDEpisode> = aodPlaylists.awaitAll().flatMap { aodPlaylist ->
aodPlaylist.list.mapIndexed { index, episode ->
AoDEpisode(
mediaId = episode.mediaid,
title = episode.title,
description = episode.description,
shortDesc = episodesInfo[episode.mediaid]?.shortDesc ?: "",
imageURL = episode.image,
numberStr = episode.title.substringAfter(", Ep. ", ""), // TODO move to parsePalylist
index = index,
watched = episodesInfo[episode.mediaid]?.watched ?: false,
watchedCallback = episodesInfo[episode.mediaid]?.watchedCallback ?: "",
streams = mutableListOf(Stream(episode.sources.first().file, aodPlaylist.language))
)
}
}.groupingBy { it.mediaId }.reduce{ _, accumulator, element ->
accumulator.copy().also {
it.streams.addAll(element.streams)
}
}.values.toList()
return@async AoDMedia(
aodId = aodId,
type = type,
title = title,
shortText = description,
posterURL = posterURL,
year = year,
age = age,
similar = similar,
playlist = playlist
)
}
}
/**
* don't use Gson().fromJson() as we don't have any control over the api and it may change
*/
private fun parsePlaylistAsync(playlistPath: String, language: String): Deferred<AoDPlaylist> {
if (playlistPath == "[]") {
return CompletableDeferred(AoDPlaylist(listOf(), Locale.ROOT))
}
return CoroutineScope(Dispatchers.IO).async(Dispatchers.IO) {
val headers = mutableMapOf(
Pair("Accept", "application/json, text/javascript, */*; q=0.01"),
Pair("Accept-Language", "de,en-US;q=0.7,en;q=0.3"),
Pair("Accept-Encoding", "gzip, deflate, br"),
Pair("X-CSRF-Token", csrfToken),
Pair("X-Requested-With", "XMLHttpRequest"),
)
//println("loading streaminfo with cstf: $csrfToken")
val res = Jsoup.connect(baseUrl + playlistPath)
.ignoreContentType(true)
.cookieStore(cookieStore)
.headers(headers)
.timeout(120000) // loading the playlist can take some time
.execute()
//Gson().fromJson(res.body(), AoDObject::class.java)
return@async AoDPlaylist(JsonParser.parseString(res.body()).asJsonObject
.get("playlist").asJsonArray.map {
Playlist(
sources = it.asJsonObject.get("sources").asJsonArray.map { source ->
Source(source.asJsonObject.get("file").asString)
},
image = it.asJsonObject.get("image").asString,
title = it.asJsonObject.get("title").asString,
description = it.asJsonObject.get("description").asString,
mediaid = it.asJsonObject.get("mediaid").asInt
)
},
// TODO improve language handling (via display language etc.)
language = when (language) {
"ger" -> Locale.GERMAN
"jap" -> Locale.JAPANESE
else -> Locale.ROOT
}
)
}
}
}

View File

@ -0,0 +1,588 @@
/**
* Teapod
*
* Copyright 2020-2022 <seil0@mosad.xyz>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301, USA.
*
*/
package org.mosad.teapod.parser.crunchyroll
import android.util.Log
import io.ktor.client.*
import io.ktor.client.call.*
import io.ktor.client.features.json.*
import io.ktor.client.features.json.serializer.*
import io.ktor.client.request.*
import io.ktor.client.request.forms.*
import io.ktor.client.statement.*
import io.ktor.http.*
import kotlinx.coroutines.*
import kotlinx.serialization.SerializationException
import kotlinx.serialization.json.Json
import kotlinx.serialization.json.JsonObject
import kotlinx.serialization.json.buildJsonObject
import kotlinx.serialization.json.put
import org.mosad.teapod.preferences.EncryptedPreferences
import org.mosad.teapod.preferences.Preferences
import org.mosad.teapod.util.concatenate
private val json = Json { ignoreUnknownKeys = true }
object Crunchyroll {
private val TAG = javaClass.name
private val client = HttpClient {
install(JsonFeature) {
serializer = KotlinxSerializer(json)
}
}
private const val baseUrl = "https://beta-api.crunchyroll.com"
private const val basicApiTokenUrl = "https://gitlab.com/-/snippets/2274956/raw/main/snippetfile1.txt"
private var basicApiToken: String = ""
private lateinit var token: Token
private var tokenValidUntil: Long = 0
private var accountID = ""
private var policy = ""
private var signature = ""
private var keyPairID = ""
private val browsingCache = arrayListOf<Item>()
/**
* Load the pai token, see:
* https://git.mosad.xyz/mosad/NonePublicIssues/issues/1
*
* TODO handle empty file
*/
fun initBasicApiToken() = runBlocking {
withContext(Dispatchers.IO) {
basicApiToken = (client.get(basicApiTokenUrl) as HttpResponse).readText()
Log.i(TAG, "basic auth token: $basicApiToken")
}
}
/**
* Login to the crunchyroll API.
*
* @param username The Username/Email of the user to log in
* @param password The Accounts Password
*
* @return Boolean: True if login was successful, else false
*/
fun login(username: String, password: String): Boolean = runBlocking {
val tokenEndpoint = "/auth/v1/token"
val formData = Parameters.build {
append("username", username)
append("password", password)
append("grant_type", "password")
append("scope", "offline_access")
}
var success = false// is false
withContext(Dispatchers.IO) {
// TODO handle exceptions
val response: HttpResponse = client.submitForm("$baseUrl$tokenEndpoint", formParameters = formData) {
header("Authorization", "Basic $basicApiToken")
}
token = response.receive()
tokenValidUntil = System.currentTimeMillis() + (token.expiresIn * 1000)
Log.i(TAG, "login complete with code ${response.status}")
success = (response.status == HttpStatusCode.OK)
}
return@runBlocking success
}
private fun refreshToken() {
login(EncryptedPreferences.login, EncryptedPreferences.password)
}
/**
* Requests: get, post, delete
*/
private suspend inline fun <reified T> request(
url: String,
httpMethod: HttpMethod,
params: List<Pair<String, Any?>> = listOf(),
bodyObject: Any = Any()
): T = coroutineScope {
if (System.currentTimeMillis() > tokenValidUntil) refreshToken()
return@coroutineScope (Dispatchers.IO) {
val response: T = client.request(url) {
method = httpMethod
header("Authorization", "${token.tokenType} ${token.accessToken}")
params.forEach {
parameter(it.first, it.second)
}
// for json set body and content type
if (bodyObject is JsonObject) {
body = bodyObject
contentType(ContentType.Application.Json)
}
}
response
}
}
private suspend inline fun <reified T> requestGet(
endpoint: String,
params: List<Pair<String, Any?>> = listOf(),
url: String = ""
): T {
val path = url.ifEmpty { "$baseUrl$endpoint" }
return request(path, HttpMethod.Get, params)
}
private suspend fun requestPost(
endpoint: String,
params: List<Pair<String, Any?>> = listOf(),
bodyObject: JsonObject
) {
val path = "$baseUrl$endpoint"
val response: HttpResponse = request(path, HttpMethod.Post, params, bodyObject)
Log.i(TAG, "Response: $response")
}
private suspend fun requestPatch(
endpoint: String,
params: List<Pair<String, Any?>> = listOf(),
bodyObject: JsonObject
) {
val path = "$baseUrl$endpoint"
val response: HttpResponse = request(path, HttpMethod.Patch, params, bodyObject)
Log.i(TAG, "Response: $response")
}
private suspend fun requestDelete(
endpoint: String,
params: List<Pair<String, Any?>> = listOf(),
url: String = ""
) = coroutineScope {
val path = url.ifEmpty { "$baseUrl$endpoint" }
val response: HttpResponse = request(path, HttpMethod.Delete, params)
Log.i(TAG, "Response: $response")
}
/**
* Basic functions: index, account
* Needed for other functions to work properly!
*/
/**
* Retrieve the identifiers necessary for streaming. If the identifiers are
* retrieved, set the corresponding global var. The identifiers are valid for 24h.
*/
suspend fun index() {
val indexEndpoint = "/index/v2"
val index: Index = requestGet(indexEndpoint)
policy = index.cms.policy
signature = index.cms.signature
keyPairID = index.cms.keyPairId
Log.i(TAG, "Policy : $policy")
Log.i(TAG, "Signature : $signature")
Log.i(TAG, "Key Pair ID : $keyPairID")
}
/**
* Retrieve the account id and set the corresponding global var.
* The account id is needed for other calls.
*
* This must be execute on every start for teapod to work properly!
*/
suspend fun account() {
val indexEndpoint = "/accounts/v1/me"
val account: Account = try {
requestGet(indexEndpoint)
} catch (ex: SerializationException) {
Log.e(TAG, "SerializationException in account(). This is bad!", ex)
NoneAccount
}
accountID = account.accountId
}
/**
* General element/media functions: browse, search, objects, season_list
*/
// TODO categories
/**
* Browse the media available on crunchyroll.
*
* @param sortBy
* @param n Number of items to return, defaults to 10
*
* @return A **[BrowseResult]** object is returned.
*/
suspend fun browse(
sortBy: SortBy = SortBy.ALPHABETICAL,
seasonTag: String = "",
start: Int = 0,
n: Int = 10
): BrowseResult {
val browseEndpoint = "/content/v1/browse"
val noneOptParams = listOf(
"locale" to Preferences.preferredLocale.toLanguageTag(),
"sort_by" to sortBy.str,
"start" to start,
"n" to n
)
// if a season tag is present add it to the parameters
val parameters = if (seasonTag.isNotEmpty()) {
concatenate(noneOptParams, listOf("season_tag" to seasonTag))
} else {
noneOptParams
}
val browseResult: BrowseResult = try {
requestGet(browseEndpoint, parameters)
}catch (ex: SerializationException) {
Log.e(TAG, "SerializationException in browse().", ex)
NoneBrowseResult
}
// add results to cache TODO improve
browsingCache.clear()
browsingCache.addAll(browseResult.items)
return browseResult
}
/**
* TODO
*/
suspend fun search(query: String, n: Int = 10): SearchResult {
val searchEndpoint = "/content/v1/search"
val parameters = listOf(
"locale" to Preferences.preferredLocale.toLanguageTag(),
"q" to query,
"n" to n,
"type" to "series"
)
// TODO episodes have thumbnails as image, and not poster_tall/poster_tall,
// to work around this, for now only tv shows are supported
return try {
requestGet(searchEndpoint, parameters)
}catch (ex: SerializationException) {
Log.e(TAG, "SerializationException in search(), with query = \"$query\".", ex)
NoneSearchResult
}
}
/**
* Get a collection of series objects.
* Note: episode objects are currently not supported
*
* @param objects The object IDs as list of Strings
* @return A **[Collection]** of Panels
*/
suspend fun objects(objects: List<String>): Collection<Item> {
val episodesEndpoint = "/cms/v2/DE/M3/crunchyroll/objects/${objects.joinToString(",")}"
val parameters = listOf(
"locale" to Preferences.preferredLocale.toLanguageTag(),
"Signature" to signature,
"Policy" to policy,
"Key-Pair-Id" to keyPairID
)
return try {
requestGet(episodesEndpoint, parameters)
}catch (ex: SerializationException) {
Log.e(TAG, "SerializationException in objects().", ex)
NoneCollection
}
}
/**
* List all available seasons as **[SeasonListItem]**.
*/
@Suppress("unused")
suspend fun seasonList(): DiscSeasonList {
val seasonListEndpoint = "/content/v1/season_list"
val parameters = listOf("locale" to Preferences.preferredLocale.toLanguageTag())
return try {
requestGet(seasonListEndpoint, parameters)
}catch (ex: SerializationException) {
Log.e(TAG, "SerializationException in seasonList().", ex)
NoneDiscSeasonList
}
}
/**
* Main media functions: series, season, episodes, playback
*/
/**
* series id == crunchyroll id?
*/
suspend fun series(seriesId: String): Series {
val seriesEndpoint = "/cms/v2/${token.country}/M3/crunchyroll/series/$seriesId"
val parameters = listOf(
"locale" to Preferences.preferredLocale.toLanguageTag(),
"Signature" to signature,
"Policy" to policy,
"Key-Pair-Id" to keyPairID
)
return try {
requestGet(seriesEndpoint, parameters)
}catch (ex: SerializationException) {
Log.e(TAG, "SerializationException in series().", ex)
NoneSeries
}
}
/**
* TODO
*/
suspend fun upNextSeries(seriesId: String): UpNextSeriesItem {
val upNextSeriesEndpoint = "/content/v1/up_next_series"
val parameters = listOf(
"series_id" to seriesId,
"locale" to Preferences.preferredLocale.toLanguageTag()
)
return try {
requestGet(upNextSeriesEndpoint, parameters)
}catch (ex: SerializationException) {
Log.e(TAG, "SerializationException in upNextSeries().", ex)
NoneUpNextSeriesItem
}
}
suspend fun seasons(seriesId: String): Seasons {
val seasonsEndpoint = "/cms/v2/${token.country}/M3/crunchyroll/seasons"
val parameters = listOf(
"series_id" to seriesId,
"locale" to Preferences.preferredLocale.toLanguageTag(),
"Signature" to signature,
"Policy" to policy,
"Key-Pair-Id" to keyPairID
)
return try {
requestGet(seasonsEndpoint, parameters)
}catch (ex: SerializationException) {
Log.e(TAG, "SerializationException in seasons().", ex)
NoneSeasons
}
}
suspend fun episodes(seasonId: String): Episodes {
val episodesEndpoint = "/cms/v2/${token.country}/M3/crunchyroll/episodes"
val parameters = listOf(
"season_id" to seasonId,
"locale" to Preferences.preferredLocale.toLanguageTag(),
"Signature" to signature,
"Policy" to policy,
"Key-Pair-Id" to keyPairID
)
return try {
requestGet(episodesEndpoint, parameters)
}catch (ex: SerializationException) {
Log.e(TAG, "SerializationException in episodes().", ex)
NoneEpisodes
}
}
suspend fun playback(url: String): Playback {
return try {
requestGet("", url = url)
}catch (ex: SerializationException) {
Log.e(TAG, "SerializationException in playback(), with url = $url.", ex)
NonePlayback
}
}
/**
* Additional media functions: watchlist (series), playhead
*/
/**
* Check if a media is in the user's watchlist.
*
* @param seriesId The crunchyroll series id of the media to check
* @return **[Boolean]**: ture if it was found, else false
*/
suspend fun isWatchlist(seriesId: String): Boolean {
val watchlistSeriesEndpoint = "/content/v1/watchlist/$accountID/$seriesId"
val parameters = listOf("locale" to Preferences.preferredLocale.toLanguageTag())
return try {
(requestGet(watchlistSeriesEndpoint, parameters) as JsonObject)
.containsKey(seriesId)
}catch (ex: SerializationException) {
Log.e(TAG, "SerializationException in isWatchlist() with seriesId = $seriesId", ex)
false
}
}
/**
* Add a media to the user's watchlist.
*
* @param seriesId The crunchyroll series id of the media to check
*/
suspend fun postWatchlist(seriesId: String) {
val watchlistPostEndpoint = "/content/v1/watchlist/$accountID"
val parameters = listOf("locale" to Preferences.preferredLocale.toLanguageTag())
val json = buildJsonObject {
put("content_id", seriesId)
}
requestPost(watchlistPostEndpoint, parameters, json)
}
/**
* Remove a media from the user's watchlist.
*
* @param seriesId The crunchyroll series id of the media to check
*/
suspend fun deleteWatchlist(seriesId: String) {
val watchlistDeleteEndpoint = "/content/v1/watchlist/$accountID/$seriesId"
val parameters = listOf("locale" to Preferences.preferredLocale.toLanguageTag())
requestDelete(watchlistDeleteEndpoint, parameters)
}
/**
* Get playhead information for all episodes in episodeIDs.
* The Information returned contains the playhead position, watched state
* and last modified date.
*
* @param episodeIDs A **[List]** of episodes IDs as strings.
* @return A **[Map]**<String, **[PlayheadObject]**> containing playback info.
*/
suspend fun playheads(episodeIDs: List<String>): PlayheadsMap {
val playheadsEndpoint = "/content/v1/playheads/$accountID/${episodeIDs.joinToString(",")}"
val parameters = listOf("locale" to Preferences.preferredLocale.toLanguageTag())
return try {
requestGet(playheadsEndpoint, parameters)
}catch (ex: SerializationException) {
Log.e(TAG, "SerializationException in upNextSeries().", ex)
emptyMap()
}
}
suspend fun postPlayheads(episodeId: String, playhead: Int) {
val playheadsEndpoint = "/content/v1/playheads/$accountID"
val parameters = listOf("locale" to Preferences.preferredLocale.toLanguageTag())
val json = buildJsonObject {
put("content_id", episodeId)
put("playhead", playhead)
}
requestPost(playheadsEndpoint, parameters, json)
}
/**
* Listing functions: watchlist (list), up_next_account
*/
/**
* List items present in the watchlist.
*
* @param n Number of items to return, defaults to 20.
* @return A **[Watchlist]** containing up to n **[Item]**.
*/
suspend fun watchlist(n: Int = 20): Watchlist {
val watchlistEndpoint = "/content/v1/$accountID/watchlist"
val parameters = listOf(
"locale" to Preferences.preferredLocale.toLanguageTag(),
"n" to n
)
val list: ContinueWatchingList = try {
requestGet(watchlistEndpoint, parameters)
}catch (ex: SerializationException) {
Log.e(TAG, "SerializationException in watchlist().", ex)
NoneContinueWatchingList
}
val objects = list.items.map{ it.panel.episodeMetadata.seriesId }
return objects(objects)
}
/**
* List the next up episodes for the logged in account.
*
* @param n Number of items to return, defaults to 20.
* @return A **[ContinueWatchingList]** containing up to n **[ContinueWatchingItem]**.
*/
suspend fun upNextAccount(n: Int = 20): ContinueWatchingList {
val watchlistEndpoint = "/content/v1/$accountID/up_next_account"
val parameters = listOf(
"locale" to Preferences.preferredLocale.toLanguageTag(),
"n" to n
)
return try {
requestGet(watchlistEndpoint, parameters)
}catch (ex: SerializationException) {
Log.e(TAG, "SerializationException in upNextAccount().", ex)
NoneContinueWatchingList
}
}
/**
* Account/Profile functions
*/
suspend fun profile(): Profile {
val profileEndpoint = "/accounts/v1/me/profile"
return try {
requestGet(profileEndpoint)
}catch (ex: SerializationException) {
Log.e(TAG, "SerializationException in profile().", ex)
NoneProfile
}
}
suspend fun postPrefSubLanguage(languageTag: String) {
val profileEndpoint = "/accounts/v1/me/profile"
val json = buildJsonObject {
put("preferred_content_subtitle_language", languageTag)
}
requestPatch(profileEndpoint, bodyObject = json)
}
}

View File

@ -0,0 +1,379 @@
/**
* Teapod
*
* Copyright 2020-2022 <seil0@mosad.xyz>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301, USA.
*
*/
package org.mosad.teapod.parser.crunchyroll
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
import java.util.*
val supportedLocals = listOf(
Locale.forLanguageTag("ar-SA"),
Locale.forLanguageTag("de-DE"),
Locale.forLanguageTag("en-US"),
Locale.forLanguageTag("es-419"),
Locale.forLanguageTag("es-ES"),
Locale.forLanguageTag("fr-FR"),
Locale.forLanguageTag("it-IT"),
Locale.forLanguageTag("pt-BR"),
Locale.forLanguageTag("pt-PT"),
Locale.forLanguageTag("ru-RU"),
Locale.ROOT
)
/**
* data classes for browse
* TODO make class names more clear/possibly overlapping for now
*/
enum class SortBy(val str: String) {
ALPHABETICAL("alphabetical"),
NEWLY_ADDED("newly_added"),
POPULARITY("popularity")
}
/**
* token, index, account. This must pe present for the app to work!
*/
@Serializable
data class Token(
@SerialName("access_token") val accessToken: String,
@SerialName("refresh_token") val refreshToken: String,
@SerialName("expires_in") val expiresIn: Int,
@SerialName("token_type") val tokenType: String,
@SerialName("scope") val scope: String,
@SerialName("country") val country: String,
@SerialName("account_id") val accountId: String,
)
@Serializable
data class Index(
@SerialName("cms") val cms: CMS,
@SerialName("service_available") val serviceAvailable: Boolean,
)
@Serializable
data class CMS(
@SerialName("bucket") val bucket: String,
@SerialName("policy") val policy: String,
@SerialName("signature") val signature: String,
@SerialName("key_pair_id") val keyPairId: String,
@SerialName("expires") val expires: String,
)
@Serializable
data class Account(
@SerialName("account_id") val accountId: String,
@SerialName("external_id") val externalId: String,
@SerialName("email_verified") val emailVerified: Boolean,
@SerialName("created") val created: String,
)
val NoneAccount = Account("", "", false, "")
/**
* search, browse, DiscSeasonList, Watchlist, ContinueWatchingList data types all use Collection
*/
@Serializable
data class Collection<T>(
@SerialName("total") val total: Int,
@SerialName("items") val items: List<T>
)
typealias SearchResult = Collection<SearchCollection>
typealias SearchCollection = Collection<Item>
typealias BrowseResult = Collection<Item>
typealias DiscSeasonList = Collection<SeasonListItem>
typealias Watchlist = Collection<Item>
typealias ContinueWatchingList = Collection<ContinueWatchingItem>
@Serializable
data class UpNextSeriesItem(
@SerialName("playhead") val playhead: Int,
@SerialName("fully_watched") val fullyWatched: Boolean,
@SerialName("never_watched") val neverWatched: Boolean,
@SerialName("panel") val panel: EpisodePanel,
)
/**
* panel data classes
*/
// the data class Item is used in browse and search
// TODO rename to MediaPanel
@Serializable
data class Item(
val id: String,
val title: String,
val type: String,
val channel_id: String,
val description: String,
val images: Images
// TODO series_metadata etc.
)
@Serializable
data class Images(val poster_tall: List<List<Poster>>, val poster_wide: List<List<Poster>>)
// crunchyroll why?
@Serializable
data class Poster(val height: Int, val width: Int, val source: String, val type: String)
/**
* season list data classes
*/
@Serializable
data class SeasonListItem(
@SerialName("id") val id: String,
@SerialName("localization") val localization: SeasonListLocalization
)
@Serializable
data class SeasonListLocalization(
@SerialName("title") val title: String,
@SerialName("description") val description: String,
)
/**
* continue_watching_item data classes
*/
@Serializable
data class ContinueWatchingItem(
@SerialName("panel") val panel: EpisodePanel,
@SerialName("new") val new: Boolean,
@SerialName("new_content") val newContent: Boolean,
// not present in up_next_account -> continue_watching_item
// @SerialName("is_favorite") val isFavorite: Boolean,
// @SerialName("never_watched") val neverWatched: Boolean,
// @SerialName("completion_status") val completionStatus: Boolean,
@SerialName("playhead") val playhead: Int,
// not present in watchlist -> continue_watching_item
@SerialName("fully_watched") val fullyWatched: Boolean = false,
)
// EpisodePanel is used in ContinueWatchingItem
@Serializable
data class EpisodePanel(
@SerialName("id") val id: String,
@SerialName("title") val title: String,
@SerialName("type") val type: String,
@SerialName("channel_id") val channelId: String,
@SerialName("description") val description: String,
@SerialName("episode_metadata") val episodeMetadata: EpisodeMetadata,
@SerialName("images") val images: Thumbnail,
@SerialName("playback") val playback: String,
)
@Serializable
data class EpisodeMetadata(
@SerialName("duration_ms") val durationMs: Int,
@SerialName("season_id") val seasonId: String,
@SerialName("series_id") val seriesId: String,
@SerialName("series_title") val seriesTitle: String,
)
val NoneItem = Item("", "", "", "", "", Images(emptyList(), emptyList()))
val NoneEpisodeMetadata = EpisodeMetadata(0, "", "", "")
val NoneEpisodePanel = EpisodePanel("", "", "", "", "", NoneEpisodeMetadata, Thumbnail(listOf()), "")
val NoneCollection = Collection<Item>(0, emptyList())
val NoneSearchResult = SearchResult(0, emptyList())
val NoneBrowseResult = BrowseResult(0, emptyList())
val NoneDiscSeasonList = DiscSeasonList(0, emptyList())
val NoneContinueWatchingList = ContinueWatchingList(0, emptyList())
val NoneUpNextSeriesItem = UpNextSeriesItem(0, false, false, NoneEpisodePanel)
/**
* Series data type
*/
@Serializable
data class Series(
@SerialName("id") val id: String,
@SerialName("title") val title: String,
@SerialName("description") val description: String,
@SerialName("images") val images: Images,
@SerialName("maturity_ratings") val maturityRatings: List<String>
)
val NoneSeries = Series("", "", "", Images(emptyList(), emptyList()), emptyList())
/**
* Seasons data type
*/
@Serializable
data class Seasons(
@SerialName("total") val total: Int,
@SerialName("items") val items: List<Season>
) {
fun getPreferredSeason(local: Locale): Season {
return items.firstOrNull { season ->
// try to get the the first seasons which matches the preferred local
season.slugTitle.endsWith("${local.getDisplayLanguage(Locale.ENGLISH)}-dub", true)
} ?: items.firstOrNull { season ->
// if there is no season with the preferred local, try to find a subbed season
season.isSubbed
} ?: items.first() // if no preferred language and no sub, use the first season
}
}
@Serializable
data class Season(
@SerialName("id") val id: String,
@SerialName("title") val title: String,
@SerialName("slug_title") val slugTitle: String,
@SerialName("series_id") val seriesId: String,
@SerialName("season_number") val seasonNumber: Int,
@SerialName("is_subbed") val isSubbed: Boolean,
@SerialName("is_dubbed") val isDubbed: Boolean,
)
val NoneSeasons = Seasons(0, emptyList())
val NoneSeason = Season("", "", "", "", 0, isSubbed = false, isDubbed = false)
/**
* Episodes data type
*/
@Serializable
data class Episodes(
@SerialName("total") val total: Int,
@SerialName("items") val items: List<Episode>
)
@Serializable
data class Episode(
@SerialName("id") val id: String,
@SerialName("title") val title: String,
@SerialName("series_id") val seriesId: String,
@SerialName("season_title") val seasonTitle: String,
@SerialName("season_id") val seasonId: String,
@SerialName("season_number") val seasonNumber: Int,
@SerialName("episode") val episode: String,
@SerialName("episode_number") val episodeNumber: Int? = null,
@SerialName("description") val description: String,
@SerialName("next_episode_id") val nextEpisodeId: String? = null, // default/nullable value since optional
@SerialName("next_episode_title") val nextEpisodeTitle: String? = null, // default/nullable value since optional
@SerialName("is_subbed") val isSubbed: Boolean,
@SerialName("is_dubbed") val isDubbed: Boolean,
@SerialName("images") val images: Thumbnail,
@SerialName("duration_ms") val durationMs: Int,
@SerialName("playback") val playback: String,
)
@Serializable
data class Thumbnail(
@SerialName("thumbnail") val thumbnail: List<List<Poster>>
)
val NoneEpisodes = Episodes(0, listOf())
val NoneEpisode = Episode(
id = "",
title = "",
seriesId = "",
seasonId = "",
seasonTitle = "",
seasonNumber = 0,
episode = "",
episodeNumber = 0,
description = "",
nextEpisodeId = "",
nextEpisodeTitle = "",
isSubbed = false,
isDubbed = false,
images = Thumbnail(listOf()),
durationMs = 0,
playback = ""
)
typealias PlayheadsMap = Map<String, PlayheadObject>
@Serializable
data class PlayheadObject(
@SerialName("playhead") val playhead: Int,
@SerialName("content_id") val contentId: String,
@SerialName("fully_watched") val fullyWatched: Boolean,
@SerialName("last_modified") val lastModified: String,
)
/**
* Playback/stream data type
*/
@Serializable
data class Playback(
@SerialName("audio_locale") val audioLocale: String,
@SerialName("subtitles") val subtitles: Map<String, Subtitle>,
@SerialName("streams") val streams: Streams,
)
@Serializable
data class Subtitle(
@SerialName("locale") val locale: String,
@SerialName("url") val url: String,
@SerialName("format") val format: String,
)
@Serializable
data class Streams(
@SerialName("adaptive_dash") val adaptive_dash: Map<String, Stream>,
@SerialName("adaptive_hls") val adaptive_hls: Map<String, Stream>,
@SerialName("download_hls") val download_hls: Map<String, Stream>,
@SerialName("drm_adaptive_dash") val drm_adaptive_dash: Map<String, Stream>,
@SerialName("drm_adaptive_hls") val drm_adaptive_hls: Map<String, Stream>,
@SerialName("drm_download_hls") val drm_download_hls: Map<String, Stream>,
@SerialName("trailer_dash") val trailer_dash: Map<String, Stream>,
@SerialName("trailer_hls") val trailer_hls: Map<String, Stream>,
@SerialName("vo_adaptive_dash") val vo_adaptive_dash: Map<String, Stream>,
@SerialName("vo_adaptive_hls") val vo_adaptive_hls: Map<String, Stream>,
@SerialName("vo_drm_adaptive_dash") val vo_drm_adaptive_dash: Map<String, Stream>,
@SerialName("vo_drm_adaptive_hls") val vo_drm_adaptive_hls: Map<String, Stream>,
)
@Serializable
data class Stream(
@SerialName("hardsub_locale") val hardsubLocale: String,
@SerialName("url") val url: String,
@SerialName("vcodec") val vcodec: String,
)
val NonePlayback = Playback(
"",
mapOf(),
Streams(
mapOf(), mapOf(), mapOf(), mapOf(), mapOf(), mapOf(),
mapOf(), mapOf(), mapOf(), mapOf(), mapOf(), mapOf(),
)
)
@Serializable
data class Profile(
@SerialName("avatar") val avatar: String,
@SerialName("email") val email: String,
@SerialName("maturity_rating") val maturityRating: String,
@SerialName("preferred_content_subtitle_language") val preferredContentSubtitleLanguage: String,
@SerialName("username") val username: String,
)
val NoneProfile = Profile(
avatar = "",
email = "",
maturityRating = "",
preferredContentSubtitleLanguage = "",
username = ""
)

View File

@ -4,10 +4,13 @@ import android.content.Context
import android.content.SharedPreferences
import org.mosad.teapod.R
import org.mosad.teapod.util.DataTypes
import java.util.*
object Preferences {
var preferSecondary = false
var preferredLocale: Locale = Locale.forLanguageTag("en-US") // TODO this should be saved (potential offline usage) but fetched on start
internal set
var preferSubbed = false
internal set
var autoplay = true
internal set
@ -23,13 +26,22 @@ object Preferences {
)
}
fun savePreferSecondary(context: Context, preferSecondary: Boolean) {
fun savePreferredLocal(context: Context, preferredLocale: Locale) {
with(getSharedPref(context).edit()) {
putBoolean(context.getString(R.string.save_key_prefer_secondary), preferSecondary)
putString(context.getString(R.string.save_key_preferred_local), preferredLocale.toLanguageTag())
apply()
}
this.preferSecondary = preferSecondary
this.preferredLocale = preferredLocale
}
fun savePreferSecondary(context: Context, preferSubbed: Boolean) {
with(getSharedPref(context).edit()) {
putBoolean(context.getString(R.string.save_key_prefer_secondary), preferSubbed)
apply()
}
this.preferSubbed = preferSubbed
}
fun saveAutoplay(context: Context, autoplay: Boolean) {
@ -65,7 +77,12 @@ object Preferences {
fun load(context: Context) {
val sharedPref = getSharedPref(context)
preferSecondary = sharedPref.getBoolean(
preferredLocale = Locale.forLanguageTag(
sharedPref.getString(
context.getString(R.string.save_key_preferred_local), "en-US"
) ?: "en-US"
)
preferSubbed = sharedPref.getBoolean(
context.getString(R.string.save_key_prefer_secondary), false
)
autoplay = sharedPref.getBoolean(

View File

@ -1,7 +1,7 @@
/**
* Teapod
*
* Copyright 2020-2021 <seil0@mosad.xyz>
* Copyright 2020-2022 <seil0@mosad.xyz>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
@ -29,13 +29,11 @@ import android.view.MenuItem
import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.Fragment
import androidx.fragment.app.commit
import com.afollestad.materialdialogs.MaterialDialog
import com.afollestad.materialdialogs.callbacks.onDismiss
import com.google.android.material.navigation.NavigationBarView
import kotlinx.coroutines.*
import org.mosad.teapod.R
import org.mosad.teapod.databinding.ActivityMainBinding
import org.mosad.teapod.parser.AoDParser
import org.mosad.teapod.parser.crunchyroll.Crunchyroll
import org.mosad.teapod.preferences.EncryptedPreferences
import org.mosad.teapod.preferences.Preferences
import org.mosad.teapod.ui.activity.main.fragments.AccountFragment
@ -46,19 +44,16 @@ import org.mosad.teapod.ui.activity.onboarding.OnboardingActivity
import org.mosad.teapod.ui.activity.player.PlayerActivity
import org.mosad.teapod.ui.components.LoginDialog
import org.mosad.teapod.util.DataTypes
import org.mosad.teapod.util.MetaDBController
import org.mosad.teapod.util.StorageController
import org.mosad.teapod.util.exitAndRemoveTask
import java.net.SocketTimeoutException
import java.util.*
import kotlin.system.measureTimeMillis
class MainActivity : AppCompatActivity(), NavigationBarView.OnItemSelectedListener {
private val classTag = javaClass.name
private lateinit var binding: ActivityMainBinding
private var activeBaseFragment: Fragment = HomeFragment() // the currently active fragment, home at the start
companion object {
var wasInitialized = false
lateinit var instance: MainActivity
}
@ -69,7 +64,7 @@ class MainActivity : AppCompatActivity(), NavigationBarView.OnItemSelectedListen
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
if (!wasInitialized) { load() }
load() // start the initial loading
theme.applyStyle(getThemeResource(), true)
binding = ActivityMainBinding.inflate(layoutInflater)
@ -138,55 +133,52 @@ class MainActivity : AppCompatActivity(), NavigationBarView.OnItemSelectedListen
*/
private fun load() {
val time = measureTimeMillis {
// start the initial loading
val loadingJob = CoroutineScope(Dispatchers.IO + CoroutineName("InitialLoadingScope"))
.async {
launch { AoDParser.initialLoading() }
launch { MetaDBController.list() }
}
// load all saved stuff here
Preferences.load(this)
EncryptedPreferences.readCredentials(this)
StorageController.load(this)
// show onboarding
if (EncryptedPreferences.password.isEmpty()) {
// always initialize the api token
Crunchyroll.initBasicApiToken()
// show onboarding if no password is set, or login fails
if (EncryptedPreferences.password.isEmpty() || !Crunchyroll.login(
EncryptedPreferences.login,
EncryptedPreferences.password
)
) {
showOnboarding()
} else {
try {
if (!AoDParser.login()) {
showLoginDialog()
}
} catch (ex: SocketTimeoutException) {
Log.w(javaClass.name, "Timeout during login!")
// show waring dialog before finishing
MaterialDialog(this).show {
title(R.string.dialog_timeout_head)
message(R.string.dialog_timeout_desc)
onDismiss { exitAndRemoveTask() }
}
}
runBlocking { initCrunchyroll().joinAll() }
}
runBlocking { loadingJob.await() } // wait for initial loading to finish
}
Log.i(javaClass.name, "loading and login in $time ms")
Log.i(classTag, "loading in $time ms")
}
wasInitialized = true
private fun initCrunchyroll(): List<Job> {
val scope = CoroutineScope(Dispatchers.IO + CoroutineName("InitialLoadingScope"))
return listOf(
scope.launch { Crunchyroll.index() },
scope.launch { Crunchyroll.account() },
scope.launch {
// update the local preferred content language, since it may have changed
val locale = Locale.forLanguageTag(Crunchyroll.profile().preferredContentSubtitleLanguage)
Preferences.savePreferredLocal(this@MainActivity, locale)
}
)
}
private fun showLoginDialog() {
LoginDialog(this, false).positiveButton {
EncryptedPreferences.saveCredentials(login, password, context)
if (!AoDParser.login()) {
showLoginDialog()
Log.w(javaClass.name, "Login failed, please try again.")
}
// TODO
// if (!AoDParser.login()) {
// showLoginDialog()
// Log.w(javaClass.name, "Login failed, please try again.")
// }
}.negativeButton {
Log.i(javaClass.name, "Login canceled, exiting.")
Log.i(classTag, "Login canceled, exiting.")
finish()
}.show()
}
@ -202,9 +194,9 @@ class MainActivity : AppCompatActivity(), NavigationBarView.OnItemSelectedListen
/**
* start the player as new activity
*/
fun startPlayer(mediaId: Int, episodeId: Int) {
fun startPlayer(seasonId: String, episodeId: String) {
val intent = Intent(this, PlayerActivity::class.java).apply {
putExtra(getString(R.string.intent_media_id), mediaId)
putExtra(getString(R.string.intent_season_id), seasonId)
putExtra(getString(R.string.intent_episode_id), episodeId)
}
startActivity(intent)

View File

@ -9,7 +9,7 @@ import android.view.ViewGroup
import android.widget.Toast
import androidx.annotation.RawRes
import androidx.fragment.app.Fragment
import com.afollestad.materialdialogs.MaterialDialog
import com.google.android.material.dialog.MaterialAlertDialogBuilder
import org.mosad.teapod.BuildConfig
import org.mosad.teapod.R
import org.mosad.teapod.databinding.FragmentAboutBinding
@ -68,9 +68,9 @@ class AboutFragment : Fragment() {
}
binding.linearLicense.setOnClickListener {
MaterialDialog(requireContext())
.title(text = License.GPL3.long)
.message(text = parseLicense(R.raw.gpl_3_full))
MaterialAlertDialogBuilder(requireContext())
.setTitle(License.GPL3.long)
.setMessage(parseLicense(R.raw.gpl_3_full))
.show()
}
}
@ -113,9 +113,9 @@ class AboutFragment : Fragment() {
"https://github.com/google/material-design-icons", License.APACHE2),
ThirdPartyComponent("Material Dialogs", "", "Aidan Follestad",
"https://github.com/afollestad/material-dialogs", License.APACHE2),
ThirdPartyComponent("Jsoup", "2009 - 2020", "Jonathan Hedley",
"https://jsoup.org/", License.MIT),
ThirdPartyComponent("kotlinx.coroutines", "2016 - 2019", "JetBrains",
ThirdPartyComponent("Ktor", "2014-2021", "JetBrains s.r.o and contributors",
"https://ktor.io/", License.APACHE2),
ThirdPartyComponent("kotlinx.coroutines", "2016-2021", "JetBrains s.r.o",
"https://github.com/Kotlin/kotlinx.coroutines", License.APACHE2),
ThirdPartyComponent("Glide", "2014", "Google Inc.",
"https://github.com/bumptech/glide", License.BSD2),
@ -132,9 +132,9 @@ class AboutFragment : Fragment() {
License.MIT -> parseLicense(R.raw.mit_full)
}
MaterialDialog(requireContext())
.title(text = license.long)
.message(text = licenseText)
MaterialAlertDialogBuilder(requireContext())
.setTitle(license.long)
.setMessage(licenseText)
.show()
}
@ -152,4 +152,4 @@ class AboutFragment : Fragment() {
return sb.toString()
}
}
}

View File

@ -2,40 +2,45 @@ package org.mosad.teapod.ui.activity.main.fragments
import android.app.Activity
import android.content.Intent
import android.net.Uri
import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Toast
import androidx.activity.result.contract.ActivityResultContracts
import androidx.core.view.isVisible
import androidx.fragment.app.Fragment
import androidx.lifecycle.lifecycleScope
import com.afollestad.materialdialogs.MaterialDialog
import com.afollestad.materialdialogs.list.listItemsSingleChoice
import com.google.android.material.dialog.MaterialAlertDialogBuilder
import kotlinx.coroutines.Deferred
import kotlinx.coroutines.async
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking
import org.mosad.teapod.BuildConfig
import org.mosad.teapod.R
import org.mosad.teapod.databinding.FragmentAccountBinding
import org.mosad.teapod.parser.AoDParser
import org.mosad.teapod.parser.crunchyroll.Crunchyroll
import org.mosad.teapod.parser.crunchyroll.Profile
import org.mosad.teapod.parser.crunchyroll.supportedLocals
import org.mosad.teapod.preferences.EncryptedPreferences
import org.mosad.teapod.preferences.Preferences
import org.mosad.teapod.ui.activity.main.MainActivity
import org.mosad.teapod.ui.components.LoginDialog
import org.mosad.teapod.util.DataTypes.Theme
import org.mosad.teapod.util.StorageController
import org.mosad.teapod.util.showFragment
import org.mosad.teapod.util.toDisplayString
import java.util.*
class AccountFragment : Fragment() {
private lateinit var binding: FragmentAccountBinding
private var profile: Deferred<Profile> = lifecycleScope.async {
Crunchyroll.profile()
}
private val getUriExport = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result ->
if (result.resultCode == Activity.RESULT_OK) {
result.data?.data?.also { uri ->
StorageController.exportMyList(requireContext(), uri)
//StorageController.exportMyList(requireContext(), uri)
}
}
}
@ -43,13 +48,13 @@ class AccountFragment : Fragment() {
private val getUriImport = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result ->
if (result.resultCode == Activity.RESULT_OK) {
result.data?.data?.also { uri ->
val success = StorageController.importMyList(requireContext(), uri)
if (success == 0) {
Toast.makeText(
context, getString(R.string.import_data_success),
Toast.LENGTH_SHORT
).show()
}
// val success = StorageController.importMyList(requireContext(), uri)
// if (success == 0) {
// Toast.makeText(
// context, getString(R.string.import_data_success),
// Toast.LENGTH_SHORT
// ).show()
// }
}
}
}
@ -62,27 +67,35 @@ class AccountFragment : Fragment() {
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
binding.textAccountLogin.text = EncryptedPreferences.login
// TODO reimplement for cr, if possible (maybe account status would be better? (premium))
// load subscription (async) info before anything else
binding.textAccountSubscription.text = getString(R.string.account_subscription, getString(R.string.loading))
lifecycleScope.launch {
binding.textAccountSubscription.text = getString(
R.string.account_subscription,
AoDParser.getSubscriptionInfoAsync().await()
"TODO"
)
}
binding.textAccountLogin.text = EncryptedPreferences.login
binding.textInfoAboutDesc.text = getString(R.string.info_about_desc, BuildConfig.VERSION_NAME, getString(R.string.build_time))
// add preferred subtitles
lifecycleScope.launch {
binding.textSettingsContentLanguageDesc.text = Locale.forLanguageTag(
profile.await().preferredContentSubtitleLanguage
).displayLanguage
}
binding.switchSecondary.isChecked = Preferences.preferSubbed
binding.switchAutoplay.isChecked = Preferences.autoplay
binding.textThemeSelected.text = when (Preferences.theme) {
Theme.DARK -> getString(R.string.theme_dark)
else -> getString(R.string.theme_light)
}
binding.switchSecondary.isChecked = Preferences.preferSecondary
binding.switchAutoplay.isChecked = Preferences.autoplay
binding.linearDevSettings.isVisible = Preferences.devSettings
binding.textInfoAboutDesc.text = getString(R.string.info_about_desc, BuildConfig.VERSION_NAME, getString(R.string.build_time))
initActions()
}
@ -92,15 +105,13 @@ class AccountFragment : Fragment() {
}
binding.linearAccountSubscription.setOnClickListener {
startActivity(Intent(Intent.ACTION_VIEW, Uri.parse(AoDParser.getSubscriptionUrl())))
// TODO
//startActivity(Intent(Intent.ACTION_VIEW, Uri.parse(AoDParser.getSubscriptionUrl())))
}
binding.linearTheme.setOnClickListener {
showThemeDialog()
}
binding.linearInfo.setOnClickListener {
activity?.showFragment(AboutFragment())
binding.linearSettingsContentLanguage.setOnClickListener {
showContentLanguageSelection()
}
binding.switchSecondary.setOnClickListener {
@ -111,6 +122,14 @@ class AccountFragment : Fragment() {
Preferences.saveAutoplay(requireContext(), binding.switchAutoplay.isChecked)
}
binding.linearTheme.setOnClickListener {
showThemeDialog()
}
binding.linearInfo.setOnClickListener {
activity?.showFragment(AboutFragment())
}
binding.linearExportData.setOnClickListener {
val i = Intent(Intent.ACTION_CREATE_DOCUMENT).apply {
addCategory(Intent.CATEGORY_OPENABLE)
@ -133,34 +152,78 @@ class AccountFragment : Fragment() {
LoginDialog(requireContext(), firstTry).positiveButton {
EncryptedPreferences.saveCredentials(login, password, context)
if (!AoDParser.login()) {
showLoginDialog(false)
Log.w(javaClass.name, "Login failed, please try again.")
}
// TODO
// if (!AoDParser.login()) {
// showLoginDialog(false)
// Log.w(javaClass.name, "Login failed, please try again.")
// }
}.show {
login = EncryptedPreferences.login
password = ""
}
}
private fun showThemeDialog() {
val themes = listOf(
resources.getString(R.string.theme_light),
resources.getString(R.string.theme_dark)
)
private fun showContentLanguageSelection() {
// we should be able to use the index of supportedLocals for language selection, items is GUI only
val items = supportedLocals.map {
it.toDisplayString(getString(R.string.settings_content_language_none))
}.toTypedArray()
MaterialDialog(requireContext()).show {
title(R.string.theme)
listItemsSingleChoice(items = themes, initialSelection = Preferences.theme.ordinal) { _, index, _ ->
when(index) {
0 -> Preferences.saveTheme(context, Theme.LIGHT)
1 -> Preferences.saveTheme(context, Theme.DARK)
else -> Preferences.saveTheme(context, Theme.DARK)
}
var initialSelection: Int
// profile should be completed here, therefore blocking
runBlocking {
initialSelection = supportedLocals.indexOf(Locale.forLanguageTag(
profile.await().preferredContentSubtitleLanguage))
if (initialSelection < 0) initialSelection = supportedLocals.lastIndex
}
(activity as MainActivity).restart()
MaterialAlertDialogBuilder(requireContext())
.setTitle(R.string.settings_content_language)
.setSingleChoiceItems(items, initialSelection){ dialog, which ->
updatePrefContentLanguage(supportedLocals[which])
dialog.dismiss()
}
.show()
}
@kotlinx.coroutines.ExperimentalCoroutinesApi
private fun updatePrefContentLanguage(preferredLocale: Locale) {
lifecycleScope.launch {
Crunchyroll.postPrefSubLanguage(preferredLocale.toLanguageTag())
}.invokeOnCompletion {
// update the local preferred content language
Preferences.savePreferredLocal(requireContext(), preferredLocale)
// update profile since the language selection might have changed
profile = lifecycleScope.async { Crunchyroll.profile() }
profile.invokeOnCompletion {
// update language once loading profile is completed
binding.textSettingsContentLanguageDesc.text = Locale.forLanguageTag(
profile.getCompleted().preferredContentSubtitleLanguage
).displayLanguage
}
}
}
private fun showThemeDialog() {
val items = arrayOf(
resources.getString(R.string.theme_light),
resources.getString(R.string.theme_dark)
)
MaterialAlertDialogBuilder(requireContext())
.setTitle(R.string.settings_content_language)
.setSingleChoiceItems(items, Preferences.theme.ordinal){ _, which ->
when(which) {
0 -> Preferences.saveTheme(requireContext(), Theme.LIGHT)
1 -> Preferences.saveTheme(requireContext(), Theme.DARK)
else -> Preferences.saveTheme(requireContext(), Theme.DARK)
}
(activity as MainActivity).restart()
}
.show()
}
}

View File

@ -1,35 +1,34 @@
package org.mosad.teapod.ui.activity.main.fragments
import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.lifecycle.lifecycleScope
import com.bumptech.glide.Glide
import kotlinx.coroutines.Job
import kotlinx.coroutines.joinAll
import kotlinx.coroutines.launch
import org.mosad.teapod.R
import org.mosad.teapod.databinding.FragmentHomeBinding
import org.mosad.teapod.parser.AoDParser
import org.mosad.teapod.ui.activity.main.MainActivity
import org.mosad.teapod.util.ItemMedia
import org.mosad.teapod.util.StorageController
import org.mosad.teapod.parser.crunchyroll.Crunchyroll
import org.mosad.teapod.parser.crunchyroll.Item
import org.mosad.teapod.parser.crunchyroll.SortBy
import org.mosad.teapod.util.adapter.MediaItemAdapter
import org.mosad.teapod.util.decoration.MediaItemDecoration
import org.mosad.teapod.util.setDrawableTop
import org.mosad.teapod.util.showFragment
import org.mosad.teapod.util.toItemMediaList
import kotlin.random.Random
class HomeFragment : Fragment() {
private lateinit var binding: FragmentHomeBinding
private lateinit var adapterMyList: MediaItemAdapter
private lateinit var adapterNewEpisodes: MediaItemAdapter
private lateinit var adapterNewSimulcasts: MediaItemAdapter
private lateinit var adapterUpNext: MediaItemAdapter
private lateinit var adapterWatchlist: MediaItemAdapter
private lateinit var adapterNewTitles: MediaItemAdapter
private lateinit var adapterTopTen: MediaItemAdapter
private lateinit var highlightMedia: ItemMedia
private lateinit var highlightMedia: Item
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {
binding = FragmentHomeBinding.inflate(inflater, container, false)
@ -49,86 +48,105 @@ class HomeFragment : Fragment() {
}
private fun initHighlight() {
if (AoDParser.highlightsList.isNotEmpty()) {
highlightMedia = AoDParser.highlightsList[0]
lifecycleScope.launch {
val newTitles = Crunchyroll.browse(sortBy = SortBy.NEWLY_ADDED, n = 10)
// FIXME crashes on newTitles.items.size == 0
highlightMedia = newTitles.items[Random.nextInt(newTitles.items.size)]
// add media item to gui
binding.textHighlightTitle.text = highlightMedia.title
Glide.with(requireContext()).load(highlightMedia.posterUrl)
Glide.with(requireContext()).load(highlightMedia.images.poster_wide[0][3].source)
.into(binding.imageHighlight)
if (StorageController.myList.contains(highlightMedia.id)) {
binding.textHighlightMyList.setDrawableTop(R.drawable.ic_baseline_check_24)
} else {
binding.textHighlightMyList.setDrawableTop(R.drawable.ic_baseline_add_24)
}
// TODO watchlist indicator
// if (StorageController.myList.contains(0)) {
// binding.textHighlightMyList.setDrawableTop(R.drawable.ic_baseline_check_24)
// } else {
// binding.textHighlightMyList.setDrawableTop(R.drawable.ic_baseline_add_24)
// }
}
}
private fun initRecyclerViews() {
binding.recyclerMyList.addItemDecoration(MediaItemDecoration(9))
/**
* Suspend, since adapters need to be initialized before we can initialize the actions.
*/
private suspend fun initRecyclerViews() {
binding.recyclerWatchlist.addItemDecoration(MediaItemDecoration(9))
binding.recyclerNewEpisodes.addItemDecoration(MediaItemDecoration(9))
binding.recyclerNewSimulcasts.addItemDecoration(MediaItemDecoration(9))
binding.recyclerNewTitles.addItemDecoration(MediaItemDecoration(9))
binding.recyclerTopTen.addItemDecoration(MediaItemDecoration(9))
// my list
adapterMyList = MediaItemAdapter(mapMyListToItemMedia())
binding.recyclerMyList.adapter = adapterMyList
val asyncJobList = arrayListOf<Job>()
// new episodes
adapterNewEpisodes = MediaItemAdapter(AoDParser.newEpisodesList)
binding.recyclerNewEpisodes.adapter = adapterNewEpisodes
// continue watching
val upNextJob = lifecycleScope.launch {
// TODO create EpisodeItemAdapter, which will start the playback of the selected episode immediately
adapterUpNext = MediaItemAdapter(Crunchyroll.upNextAccount().items
.filter { !it.fullyWatched }.toItemMediaList())
binding.recyclerNewEpisodes.adapter = adapterUpNext
}
asyncJobList.add(upNextJob)
// watchlist
val watchlistJob = lifecycleScope.launch {
adapterWatchlist = MediaItemAdapter(Crunchyroll.watchlist(50).toItemMediaList())
binding.recyclerWatchlist.adapter = adapterWatchlist
}
asyncJobList.add(watchlistJob)
// new simulcasts
adapterNewSimulcasts = MediaItemAdapter(AoDParser.newSimulcastsList)
binding.recyclerNewSimulcasts.adapter = adapterNewSimulcasts
val simulcastsJob = lifecycleScope.launch {
// val latestSeasonTag = Crunchyroll.seasonList().items.first().id
// val newSimulcasts = Crunchyroll.browse(seasonTag = latestSeasonTag, n = 50)
val newSimulcasts = Crunchyroll.browse(sortBy = SortBy.NEWLY_ADDED, n = 50)
// new titles
adapterNewTitles = MediaItemAdapter(AoDParser.newTitlesList)
binding.recyclerNewTitles.adapter = adapterNewTitles
adapterNewTitles = MediaItemAdapter(newSimulcasts.toItemMediaList())
binding.recyclerNewTitles.adapter = adapterNewTitles
}
asyncJobList.add(simulcastsJob)
// top ten
adapterTopTen = MediaItemAdapter(AoDParser.topTenList)
binding.recyclerTopTen.adapter = adapterTopTen
// newly added / top ten
val newlyAddedJob = lifecycleScope.launch {
adapterTopTen = MediaItemAdapter(Crunchyroll.browse(sortBy = SortBy.POPULARITY, n = 10).toItemMediaList())
binding.recyclerTopTen.adapter = adapterTopTen
}
asyncJobList.add(newlyAddedJob)
asyncJobList.joinAll()
}
private fun initActions() {
binding.buttonPlayHighlight.setOnClickListener {
// TODO get next episode
// TODO implement
lifecycleScope.launch {
val media = AoDParser.getMediaById(highlightMedia.id)
//val media = AoDParser.getMediaById(0)
Log.d(javaClass.name, "Starting Player with mediaId: ${media.aodId}")
(activity as MainActivity).startPlayer(media.aodId, media.playlist.first().mediaId)
// Log.d(javaClass.name, "Starting Player with mediaId: ${media.aodId}")
//(activity as MainActivity).startPlayer(media.aodId, media.playlist.first().mediaId)
}
}
binding.textHighlightMyList.setOnClickListener {
if (StorageController.myList.contains(highlightMedia.id)) {
StorageController.myList.remove(highlightMedia.id)
binding.textHighlightMyList.setDrawableTop(R.drawable.ic_baseline_add_24)
} else {
StorageController.myList.add(highlightMedia.id)
binding.textHighlightMyList.setDrawableTop(R.drawable.ic_baseline_check_24)
}
StorageController.saveMyList(requireContext())
updateMyListMedia() // update my list, since it has changed
// TODO implement
// if (StorageController.myList.contains(0)) {
// StorageController.myList.remove(0)
// binding.textHighlightMyList.setDrawableTop(R.drawable.ic_baseline_add_24)
// } else {
// StorageController.myList.add(0)
// binding.textHighlightMyList.setDrawableTop(R.drawable.ic_baseline_check_24)
// }
// StorageController.saveMyList(requireContext())
}
binding.textHighlightInfo.setOnClickListener {
activity?.showFragment(MediaFragment(highlightMedia.id))
}
adapterMyList.onItemClick = { id, _ ->
adapterUpNext.onItemClick = { id, _ ->
activity?.showFragment(MediaFragment(id))
}
adapterNewEpisodes.onItemClick = { id, _ ->
activity?.showFragment(MediaFragment(id))
}
adapterNewSimulcasts.onItemClick = { id, _ ->
adapterWatchlist.onItemClick = { id, _ ->
activity?.showFragment(MediaFragment(id))
}
@ -137,30 +155,8 @@ class HomeFragment : Fragment() {
}
adapterTopTen.onItemClick = { id, _ ->
activity?.showFragment(MediaFragment(id))
activity?.showFragment(MediaFragment(id)) //(mediaId))
}
}
/**
* update my media list
* TODO
* * auto call when StorageController.myList is changed
* * only update actual change and not all data (performance)
*/
fun updateMyListMedia() {
adapterMyList.updateMediaList(mapMyListToItemMedia())
adapterMyList.notifyDataSetChanged()
}
private fun mapMyListToItemMedia(): List<ItemMedia> {
return StorageController.myList.mapNotNull { elementId ->
AoDParser.guiMediaList.firstOrNull { it.id == elementId }.also {
// it the my list entry wasn't found in itemMediaList Log it
if (it == null) {
Log.w(javaClass.name, "The element with the id $elementId was not found.")
}
}
}
}
}
}

View File

@ -6,9 +6,12 @@ import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import kotlinx.coroutines.launch
import org.mosad.teapod.databinding.FragmentLibraryBinding
import org.mosad.teapod.parser.AoDParser
import org.mosad.teapod.parser.crunchyroll.Crunchyroll
import org.mosad.teapod.util.ItemMedia
import org.mosad.teapod.util.adapter.MediaItemAdapter
import org.mosad.teapod.util.decoration.MediaItemDecoration
import org.mosad.teapod.util.showFragment
@ -18,6 +21,10 @@ class LibraryFragment : Fragment() {
private lateinit var binding: FragmentLibraryBinding
private lateinit var adapter: MediaItemAdapter
private val itemList = arrayListOf<ItemMedia>()
private val pageSize = 30
private var nextItemIndex = 0
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {
binding = FragmentLibraryBinding.inflate(inflater, container, false)
return binding.root
@ -30,15 +37,53 @@ class LibraryFragment : Fragment() {
lifecycleScope.launch {
// create and set the adapter, needs context
context?.let {
adapter = MediaItemAdapter(AoDParser.guiMediaList)
adapter.onItemClick = { mediaId, _ ->
activity?.showFragment(MediaFragment(mediaId))
val initialResults = Crunchyroll.browse(n = pageSize)
itemList.addAll(initialResults.items.map { item ->
ItemMedia(item.id, item.title, item.images.poster_wide[0][0].source)
})
nextItemIndex += pageSize
adapter = MediaItemAdapter(itemList)
adapter.onItemClick = { mediaIdStr, _ ->
activity?.showFragment(MediaFragment(mediaIdStr))
}
binding.recyclerMediaLibrary.adapter = adapter
binding.recyclerMediaLibrary.addItemDecoration(MediaItemDecoration(9))
// TODO replace with pagination3
// https://medium.com/swlh/paging3-recyclerview-pagination-made-easy-333c7dfa8797
binding.recyclerMediaLibrary.addOnScrollListener(PaginationScrollListener())
}
}
}
inner class PaginationScrollListener: RecyclerView.OnScrollListener() {
private var isLoading = false
override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
super.onScrolled(recyclerView, dx, dy)
val layoutManager = recyclerView.layoutManager as GridLayoutManager?
if (!isLoading) layoutManager?.let {
// itemList.size - 5 to start loading a bit earlier than the actual end
if (layoutManager.findLastCompletelyVisibleItemPosition() >= (itemList.size - 5)) {
// load new browse results async
isLoading = true
lifecycleScope.launch {
val firstNewItemIndex = itemList.lastIndex + 1
val results = Crunchyroll.browse(start = nextItemIndex, n = pageSize)
itemList.addAll(results.items.map { item ->
ItemMedia(item.id, item.title, item.images.poster_wide[0][0].source)
})
nextItemIndex += pageSize
adapter.notifyItemRangeInserted(firstNewItemIndex, pageSize)
isLoading = false
}
}
}
}
}
}

View File

@ -20,27 +20,30 @@ import jp.wasabeef.glide.transformations.BlurTransformation
import kotlinx.coroutines.launch
import org.mosad.teapod.R
import org.mosad.teapod.databinding.FragmentMediaBinding
import org.mosad.teapod.parser.crunchyroll.NoneUpNextSeriesItem
import org.mosad.teapod.ui.activity.main.MainActivity
import org.mosad.teapod.ui.activity.main.viewmodel.MediaFragmentViewModel
import org.mosad.teapod.util.DataTypes.MediaType
import org.mosad.teapod.util.StorageController
import org.mosad.teapod.util.tmdb.TMDBMovie
import org.mosad.teapod.util.tmdb.TMDBApiController
import org.mosad.teapod.util.tmdb.TMDBMovie
import org.mosad.teapod.util.tmdb.TMDBTVShow
/**
* The media detail fragment.
* Note: the fragment is created only once, when selecting a similar title etc.
* therefore fragments may be not empty and model may be the old one
*/
class MediaFragment(private val mediaId: Int) : Fragment() {
class MediaFragment(private val mediaIdStr: String) : Fragment() {
private lateinit var binding: FragmentMediaBinding
private lateinit var pagerAdapter: FragmentStateAdapter
private val fragments = arrayListOf<Fragment>()
private val model: MediaFragmentViewModel by activityViewModels()
private val fragments = arrayListOf<Fragment>()
private var watchlistJobRunning = false
private var runOnResume = false
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {
binding = FragmentMediaBinding.inflate(inflater, container, false)
return binding.root
@ -55,16 +58,17 @@ class MediaFragment(private val mediaId: Int) : Fragment() {
// fix material components issue #1878, if more tabs are added increase
binding.pagerEpisodesSimilar.offscreenPageLimit = 2
binding.pagerEpisodesSimilar.adapter = pagerAdapter
// TODO is position 0 always episodes? (and 1 always similar titles)
TabLayoutMediator(binding.tabEpisodesSimilar, binding.pagerEpisodesSimilar) { tab, position ->
tab.text = if (model.media.type == MediaType.TVSHOW && position == 0) {
getString(R.string.episodes)
} else {
getString(R.string.similar_titles)
tab.text = when(position) {
0 -> getString(R.string.episodes)
1 -> getString(R.string.similar_titles)
else -> ""
}
}.attach()
lifecycleScope.launch {
model.load(mediaId) // load the streams and tmdb for the selected media
model.loadCrunchy(mediaIdStr)
updateGUI()
initActions()
@ -74,9 +78,21 @@ class MediaFragment(private val mediaId: Int) : Fragment() {
override fun onResume() {
super.onResume()
// update the next ep text if there is one, since it may have changed
if (model.media.getEpisodeById(model.nextEpisodeId).title.isNotEmpty()) {
binding.textTitle.text = model.media.getEpisodeById(model.nextEpisodeId).title
if (runOnResume) {
lifecycleScope.launch {
model.updateOnResume()
if (model.upNextSeries != NoneUpNextSeriesItem) {
binding.textTitle.text = model.upNextSeries.panel.title
}
// needs to be called after model.updateOnResume()
if (fragments.elementAtOrNull(0) is MediaFragmentEpisodes) {
(fragments[0] as MediaFragmentEpisodes).updateWatchedState()
}
}
} else {
runOnResume = true
}
}
@ -85,10 +101,10 @@ class MediaFragment(private val mediaId: Int) : Fragment() {
*/
private fun updateGUI() = with(model) {
// generic gui
val backdropUrl = tmdbResult?.backdropPath?.let { TMDBApiController.imageUrl + it }
?: media.posterURL
val posterUrl = tmdbResult?.posterPath?.let { TMDBApiController.imageUrl + it }
?: media.posterURL
val backdropUrl = tmdbResult.backdropPath?.let { TMDBApiController.imageUrl + it }
?: seriesCrunchy.images.poster_wide[0][2].source
val posterUrl = tmdbResult.posterPath?.let { TMDBApiController.imageUrl + it }
?: seriesCrunchy.images.poster_tall[0][2].source
// load poster and backdrop
Glide.with(requireContext()).load(posterUrl)
@ -98,65 +114,69 @@ class MediaFragment(private val mediaId: Int) : Fragment() {
.apply(RequestOptions.bitmapTransform(BlurTransformation(20, 3)))
.into(binding.imageBackdrop)
binding.textTitle.text = media.title
binding.textYear.text = media.year.toString()
binding.textAge.text = media.age.toString()
binding.textOverview.text = media.shortText
// set "my list" indicator
if (StorageController.myList.contains(media.aodId)) {
Glide.with(requireContext()).load(R.drawable.ic_baseline_check_24).into(binding.imageMyListAction)
} else {
Glide.with(requireContext()).load(R.drawable.ic_baseline_add_24).into(binding.imageMyListAction)
binding.textYear.text = when(tmdbResult) {
is TMDBTVShow -> (tmdbResult as TMDBTVShow).firstAirDate.substring(0, 4)
is TMDBMovie -> (tmdbResult as TMDBMovie).releaseDate.substring(0, 4)
else -> ""
}
binding.textAge.text = seriesCrunchy.maturityRatings.firstOrNull()
binding.textTitle.text = if (upNextSeries != NoneUpNextSeriesItem) {
upNextSeries.panel.title
} else seriesCrunchy.title
binding.textOverview.text = seriesCrunchy.description
// set "watchlist" indicator
val watchlistIcon = if (isWatchlist) R.drawable.ic_baseline_check_24 else R.drawable.ic_baseline_add_24
Glide.with(requireContext()).load(watchlistIcon).into(binding.imageMyListAction)
// clear fragments, since it lives in onCreate scope (don't do this in onPause/onStop -> FragmentManager transaction)
val fragmentsSize = if (fragments.lastIndex < 0) 0 else fragments.lastIndex
fragments.clear()
pagerAdapter.notifyItemRangeRemoved(0, fragmentsSize)
// specific gui
if (media.type == MediaType.TVSHOW) {
// get next episode
nextEpisodeId = media.playlist.firstOrNull{ !it.watched }?.mediaId
?: media.playlist.first().mediaId
// add the episodes fragment (as tab). Note: Movies are tv shows!
MediaFragmentEpisodes().also {
fragments.add(it)
pagerAdapter.notifyItemInserted(fragments.indexOf(it))
}
// title is the next episodes title
binding.textTitle.text = media.getEpisodeById(nextEpisodeId).title
// episodes count
binding.textEpisodesOrRuntime.text = resources.getQuantityString(
R.plurals.text_episodes_count,
media.playlist.size,
media.playlist.size
)
// episodes
MediaFragmentEpisodes().also {
fragments.add(it)
pagerAdapter.notifyItemInserted(fragments.indexOf(it))
}
} else if (media.type == MediaType.MOVIE) {
val tmdbMovie = (tmdbResult as TMDBMovie?)
if (tmdbMovie?.runtime != null) {
// specific gui (via tmdb)
when (tmdbResult) {
is TMDBTVShow -> {
// episodes count
binding.textEpisodesOrRuntime.text = resources.getQuantityString(
R.plurals.text_runtime,
tmdbMovie.runtime,
tmdbMovie.runtime
R.plurals.text_episodes_count,
episodesCrunchy.total,
episodesCrunchy.total
)
} else {
}
is TMDBMovie -> {
val tmdbMovie = (tmdbResult as TMDBMovie?)
if (tmdbMovie?.runtime != null) {
binding.textEpisodesOrRuntime.text = resources.getQuantityString(
R.plurals.text_runtime,
tmdbMovie.runtime,
tmdbMovie.runtime
)
} else {
binding.textEpisodesOrRuntime.visibility = View.GONE
}
}
else -> {
binding.textEpisodesOrRuntime.visibility = View.GONE
}
}
// if has similar titles
if (media.similar.isNotEmpty()) {
MediaFragmentSimilar().also {
fragments.add(it)
pagerAdapter.notifyItemInserted(fragments.indexOf(it))
}
}
// TODO reimplement
// if (media.similar.isNotEmpty()) {
// MediaFragmentSimilar().also {
// fragments.add(it)
// pagerAdapter.notifyItemInserted(fragments.indexOf(it))
// }
// }
// disable scrolling on appbar, if no tabs where added
if(fragments.isEmpty()) {
@ -169,27 +189,24 @@ class MediaFragment(private val mediaId: Int) : Fragment() {
private fun initActions() = with(model) {
binding.buttonPlay.setOnClickListener {
when (media.type) {
MediaType.MOVIE -> playEpisode(media.playlist.first().mediaId)
MediaType.TVSHOW -> playEpisode(nextEpisodeId)
else -> Log.e(javaClass.name, "Wrong Type: ${media.type}")
if (upNextSeries != NoneUpNextSeriesItem) {
playEpisode(upNextSeries.panel.episodeMetadata.seasonId, upNextSeries.panel.id)
}
}
// add or remove media from myList
binding.linearMyListAction.setOnClickListener {
if (StorageController.myList.contains(media.aodId)) {
StorageController.myList.remove(media.aodId)
Glide.with(requireContext()).load(R.drawable.ic_baseline_add_24).into(binding.imageMyListAction)
} else {
StorageController.myList.add(media.aodId)
Glide.with(requireContext()).load(R.drawable.ic_baseline_check_24).into(binding.imageMyListAction)
}
StorageController.saveMyList(requireContext())
// don't allow parallel execution
if (!watchlistJobRunning) {
watchlistJobRunning = true
lifecycleScope.launch {
setWatchlist()
// notify home fragment on change
parentFragmentManager.findFragmentByTag("HomeFragment")?.let {
(it as HomeFragment).updateMyListMedia()
// update "watchlist" indicator
val watchlistIcon = if (isWatchlist) R.drawable.ic_baseline_check_24 else R.drawable.ic_baseline_add_24
Glide.with(requireContext()).load(watchlistIcon).into(binding.imageMyListAction)
watchlistJobRunning = false
}
}
}
}
@ -198,11 +215,11 @@ class MediaFragment(private val mediaId: Int) : Fragment() {
* play the current episode
* TODO this is also used in MediaFragmentEpisode, we should only have on implementation
*/
private fun playEpisode(episodeId: Int) {
(activity as MainActivity).startPlayer(model.media.aodId, episodeId)
private fun playEpisode(seasonId: String, episodeId: String) {
(activity as MainActivity).startPlayer(seasonId, episodeId)
Log.d(javaClass.name, "Started Player with episodeId: $episodeId")
model.updateNextEpisode(episodeId) // set the correct next episode
//model.updateNextEpisode(episodeId) // set the correct next episode
}
/**

View File

@ -1,15 +1,20 @@
package org.mosad.teapod.ui.activity.main.fragments
import android.annotation.SuppressLint
import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.appcompat.widget.PopupMenu
import androidx.fragment.app.Fragment
import androidx.fragment.app.activityViewModels
import androidx.lifecycle.lifecycleScope
import kotlinx.coroutines.launch
import org.mosad.teapod.R
import org.mosad.teapod.databinding.FragmentMediaEpisodesBinding
import org.mosad.teapod.ui.activity.main.MainActivity
import org.mosad.teapod.ui.activity.main.viewmodel.MediaFragmentViewModel
import org.mosad.teapod.databinding.FragmentMediaEpisodesBinding
import org.mosad.teapod.util.adapter.EpisodeItemAdapter
class MediaFragmentEpisodes : Fragment() {
@ -27,34 +32,84 @@ class MediaFragmentEpisodes : Fragment() {
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
adapterRecEpisodes = EpisodeItemAdapter(model.media.playlist, model.tmdbTVSeason?.episodes)
adapterRecEpisodes = EpisodeItemAdapter(
model.currentEpisodesCrunchy,
model.tmdbTVSeason.episodes,
model.currentPlayheads
)
binding.recyclerEpisodes.adapter = adapterRecEpisodes
// set onItemClick only in adapter is initialized
if (this::adapterRecEpisodes.isInitialized) {
adapterRecEpisodes.onImageClick = { _, position ->
playEpisode(model.media.playlist[position].mediaId)
// set onItemClick, adapter is initialized
adapterRecEpisodes.onImageClick = { seasonId, episodeId ->
playEpisode(seasonId, episodeId)
}
// don't show season selection if only one season is present
if (model.seasonsCrunchy.total < 2) {
binding.buttonSeasonSelection.visibility = View.GONE
} else {
binding.buttonSeasonSelection.text = getString(
R.string.season_number_title,
model.currentSeasonCrunchy.seasonNumber,
model.currentSeasonCrunchy.title
)
binding.buttonSeasonSelection.setOnClickListener { v ->
showSeasonSelection(v)
}
}
}
override fun onResume() {
super.onResume()
@SuppressLint("NotifyDataSetChanged")
fun updateWatchedState() {
// model.currentPlayheads is a val mutable map -> notify dataset changed
adapterRecEpisodes.notifyDataSetChanged()
}
// if adapterRecEpisodes is initialized, update the watched state for the episodes
if (this::adapterRecEpisodes.isInitialized) {
model.media.playlist.forEachIndexed { index, episodeInfo ->
adapterRecEpisodes.updateWatchedState(episodeInfo.watched, index)
private fun showSeasonSelection(v: View) {
// TODO replace with Exposed dropdown menu: https://material.io/components/menus/android#exposed-dropdown-menus
val popup = PopupMenu(requireContext(), v)
model.seasonsCrunchy.items.forEach { season ->
popup.menu.add(getString(
R.string.season_number_title,
season.seasonNumber,
season.title
)
).also {
it.setOnMenuItemClickListener {
onSeasonSelected(season.id)
false
}
}
}
popup.show()
}
/**
* Call model to load a new season.
* Once loaded update buttonSeasonSelection text and adapterRecEpisodes.
*
* Suppress waring since invalid.
*/
@SuppressLint("NotifyDataSetChanged")
private fun onSeasonSelected(seasonId: String) {
// load the new season
lifecycleScope.launch {
model.setCurrentSeason(seasonId)
binding.buttonSeasonSelection.text = getString(
R.string.season_number_title,
model.currentSeasonCrunchy.seasonNumber,
model.currentSeasonCrunchy.title
)
adapterRecEpisodes.notifyDataSetChanged()
}
}
private fun playEpisode(episodeId: Int) {
(activity as MainActivity).startPlayer(model.media.aodId, episodeId)
private fun playEpisode(seasonId: String, episodeId: String) {
(activity as MainActivity).startPlayer(seasonId, episodeId)
Log.d(javaClass.name, "Started Player with episodeId: $episodeId")
model.updateNextEpisode(episodeId) // set the correct next episode
//model.updateNextEpisode(episodeId) // set the correct next episode
}
}

View File

@ -27,14 +27,14 @@ class MediaFragmentSimilar : Fragment() {
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
adapterSimilar = MediaItemAdapter(model.media.similar)
adapterSimilar = MediaItemAdapter(emptyList()) //(model.media.similar)
binding.recyclerMediaSimilar.adapter = adapterSimilar
binding.recyclerMediaSimilar.addItemDecoration(MediaItemDecoration(9))
// set onItemClick only in adapter is initialized
if (this::adapterSimilar.isInitialized) {
adapterSimilar.onItemClick = { mediaId, _ ->
activity?.showFragment(MediaFragment(mediaId))
activity?.showFragment(MediaFragment("")) //(mediaId))
}
}
}

View File

@ -7,17 +7,24 @@ import android.view.ViewGroup
import android.widget.SearchView
import androidx.fragment.app.Fragment
import androidx.lifecycle.lifecycleScope
import kotlinx.coroutines.Job
import kotlinx.coroutines.async
import kotlinx.coroutines.launch
import org.mosad.teapod.databinding.FragmentSearchBinding
import org.mosad.teapod.parser.AoDParser
import org.mosad.teapod.util.decoration.MediaItemDecoration
import org.mosad.teapod.parser.crunchyroll.Crunchyroll
import org.mosad.teapod.util.ItemMedia
import org.mosad.teapod.util.adapter.MediaItemAdapter
import org.mosad.teapod.util.decoration.MediaItemDecoration
import org.mosad.teapod.util.showFragment
class SearchFragment : Fragment() {
private lateinit var binding: FragmentSearchBinding
private var adapter : MediaItemAdapter? = null
private lateinit var adapter: MediaItemAdapter
private val itemList = arrayListOf<ItemMedia>()
private var searchJob: Job? = null
private var oldSearchQuery = ""
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {
binding = FragmentSearchBinding.inflate(inflater, container, false)
@ -30,10 +37,10 @@ class SearchFragment : Fragment() {
lifecycleScope.launch {
// create and set the adapter, needs context
context?.let {
adapter = MediaItemAdapter(AoDParser.guiMediaList)
adapter!!.onItemClick = { mediaId, _ ->
adapter = MediaItemAdapter(itemList)
adapter.onItemClick = { mediaIdStr, _ ->
binding.searchText.clearFocus()
activity?.showFragment(MediaFragment(mediaId))
activity?.showFragment(MediaFragment(mediaIdStr))
}
binding.recyclerMediaSearch.adapter = adapter
@ -47,16 +54,65 @@ class SearchFragment : Fragment() {
private fun initActions() {
binding.searchText.setOnQueryTextListener(object : SearchView.OnQueryTextListener {
override fun onQueryTextSubmit(query: String?): Boolean {
adapter?.filter?.filter(query)
adapter?.notifyDataSetChanged()
query?.let { search(it) }
return false
}
override fun onQueryTextChange(newText: String?): Boolean {
adapter?.filter?.filter(newText)
adapter?.notifyDataSetChanged()
newText?.let { search(it) }
return false
}
})
}
private fun search(query: String) {
// if the query hasn't changed since the last successful search, return
if (query == oldSearchQuery) return
// cancel search job if one is already running
if (searchJob?.isActive == true) searchJob?.cancel()
searchJob = lifecycleScope.async {
// TODO maybe wait a few ms (500ms?) before searching, if the user inputs any other chars
val results = Crunchyroll.search(query, 50)
itemList.clear() // TODO needs clean up
// TODO add top results first heading
itemList.addAll(results.items[0].items.map { item ->
ItemMedia(item.id, item.title, item.images.poster_wide[0][0].source)
})
// TODO currently only tv shows are supported, hence only the first items array
// should be always present
// // TODO add tv shows heading
// if (results.items.size >= 2) {
// itemList.addAll(results.items[1].items.map { item ->
// ItemMedia(item.id, item.title, item.images.poster_wide[0][0].source)
// })
// }
//
// // TODO add movies heading
// if (results.items.size >= 3) {
// itemList.addAll(results.items[2].items.map { item ->
// ItemMedia(item.id, item.title, item.images.poster_wide[0][0].source)
// })
// }
//
// // TODO add episodes heading
// if (results.items.size >= 4) {
// itemList.addAll(results.items[3].items.map { item ->
// ItemMedia(item.id, item.title, item.images.poster_wide[0][0].source)
// })
// }
adapter.notifyDataSetChanged()
//adapter.notifyItemRangeInserted(0, itemList.size)
// after successfully searching the query term, add it as old query, to make sure we
// don't search again if the query hasn't changed
oldSearchQuery = query
}
}
}

View File

@ -1,14 +1,15 @@
package org.mosad.teapod.ui.activity.main.viewmodel
import android.app.Application
import android.util.Log
import androidx.lifecycle.AndroidViewModel
import org.mosad.teapod.parser.AoDParser
import org.mosad.teapod.util.*
import androidx.lifecycle.viewModelScope
import kotlinx.coroutines.joinAll
import kotlinx.coroutines.launch
import org.mosad.teapod.parser.crunchyroll.*
import org.mosad.teapod.preferences.Preferences
import org.mosad.teapod.util.DataTypes.MediaType
import org.mosad.teapod.util.tmdb.TMDBApiController
import org.mosad.teapod.util.tmdb.TMDBResult
import org.mosad.teapod.util.tmdb.TMDBTVSeason
import org.mosad.teapod.util.Meta
import org.mosad.teapod.util.tmdb.*
/**
* handle media, next ep and tmdb
@ -16,62 +17,149 @@ import org.mosad.teapod.util.tmdb.TMDBTVSeason
*/
class MediaFragmentViewModel(application: Application) : AndroidViewModel(application) {
var media = AoDMediaNone
// var mediaCrunchy = NoneItem
// internal set
var seriesCrunchy = NoneSeries // movies are also series
internal set
var nextEpisodeId = -1
var seasonsCrunchy = NoneSeasons
internal set
var currentSeasonCrunchy = NoneSeason
internal set
var episodesCrunchy = NoneEpisodes
internal set
val currentEpisodesCrunchy = arrayListOf<Episode>() // used for EpisodeItemAdapter (easier updates)
var tmdbResult: TMDBResult? = null // TODO rename
// additional media info
val currentPlayheads: MutableMap<String, PlayheadObject> = mutableMapOf()
var isWatchlist = false
internal set
var tmdbTVSeason: TMDBTVSeason? =null
var upNextSeries = NoneUpNextSeriesItem
// TMDB stuff
var mediaType = MediaType.OTHER
internal set
var tmdbResult: TMDBResult = NoneTMDB // TODO rename
internal set
var tmdbTVSeason: TMDBTVSeason = NoneTMDBTVSeason
internal set
var mediaMeta: Meta? = null
internal set
/**
* set media, tmdb and nextEpisode
* TODO run aod and tmdb load parallel
* @param crunchyId the crunchyroll series id
*/
suspend fun load(aodId: Int) {
suspend fun loadCrunchy(crunchyId: String) {
// load series and seasons info in parallel
listOf(
viewModelScope.launch { seriesCrunchy = Crunchyroll.series(crunchyId) },
viewModelScope.launch { seasonsCrunchy = Crunchyroll.seasons(crunchyId) },
viewModelScope.launch { isWatchlist = Crunchyroll.isWatchlist(crunchyId) },
viewModelScope.launch { upNextSeries = Crunchyroll.upNextSeries(crunchyId) }
).joinAll()
// println("series: $seriesCrunchy")
// println("seasons: $seasonsCrunchy")
println(upNextSeries)
// load the preferred season (preferred language, language per season, not per stream)
currentSeasonCrunchy = seasonsCrunchy.getPreferredSeason(Preferences.preferredLocale)
// load episodes and metaDB in parallel (tmdb needs mediaType, which is set via episodes)
listOf(
viewModelScope.launch { episodesCrunchy = Crunchyroll.episodes(currentSeasonCrunchy.id) },
viewModelScope.launch { mediaMeta = null }, // TODO metaDB
).joinAll()
// println("episodes: $episodesCrunchy")
currentEpisodesCrunchy.clear()
currentEpisodesCrunchy.addAll(episodesCrunchy.items)
// set media type
mediaType = episodesCrunchy.items.firstOrNull()?.let {
if (it.episodeNumber != null) MediaType.TVSHOW else MediaType.MOVIE
} ?: MediaType.OTHER
// load playheads and tmdb in parallel
listOf(
viewModelScope.launch {
// get playheads (including fully watched state)
val episodeIDs = episodesCrunchy.items.map { it.id }
currentPlayheads.clear()
currentPlayheads.putAll(Crunchyroll.playheads(episodeIDs))
},
viewModelScope.launch { loadTmdbInfo() } // use tmdb search to get media info
).joinAll()
}
/**
* Load the tmdb info for the selected media.
* The TMDB search return a media type, use this to get the details (movie/tv show and season)
*/
private suspend fun loadTmdbInfo() {
val tmdbApiController = TMDBApiController()
media = AoDParser.getMediaById(aodId)
// check if metaDB knows the title
val tmdbId: Int = if (MetaDBController.mediaList.media.contains(aodId)) {
// load media info from metaDB
val metaDB = MetaDBController()
mediaMeta = when (media.type) {
MediaType.MOVIE -> metaDB.getMovieMetadata(media.aodId)
MediaType.TVSHOW -> metaDB.getTVShowMetadata(media.aodId)
else -> null
val tmdbSearchResult = when(mediaType) {
MediaType.MOVIE -> tmdbApiController.searchMovie(seriesCrunchy.title)
MediaType.TVSHOW -> tmdbApiController.searchTVShow(seriesCrunchy.title)
else -> NoneTMDBSearch
}
println(tmdbSearchResult)
tmdbResult = if (tmdbSearchResult.results.isNotEmpty()) {
when (val result = tmdbSearchResult.results.first()) {
is TMDBSearchResultMovie -> tmdbApiController.getMovieDetails(result.id)
is TMDBSearchResultTVShow -> tmdbApiController.getTVShowDetails(result.id)
else -> NoneTMDB
}
} else NoneTMDB
mediaMeta?.tmdbId ?: -1
println(tmdbResult)
// currently not used
// tmdbTVSeason = if (tmdbResult is TMDBTVShow) {
// tmdbApiController.getTVSeasonDetails(tmdbResult.id, 0)
// } else NoneTMDBTVSeason
}
/**
* Set currentSeasonCrunchy based on the season id. Also set the new seasons episodes.
*
* @param seasonId the id of the season to set
*/
suspend fun setCurrentSeason(seasonId: String) {
// return if the id hasn't changed (performance)
if (currentSeasonCrunchy.id == seasonId) return
// set currentSeasonCrunchy to the new season with id == seasonId, if the id isn't found,
// don't change the current season (this should/can never happen)
currentSeasonCrunchy = seasonsCrunchy.items.firstOrNull {
it.id == seasonId
} ?: currentSeasonCrunchy
episodesCrunchy = Crunchyroll.episodes(currentSeasonCrunchy.id)
currentEpisodesCrunchy.clear()
currentEpisodesCrunchy.addAll(episodesCrunchy.items)
}
suspend fun setWatchlist() {
isWatchlist = if (isWatchlist) {
Crunchyroll.deleteWatchlist(seriesCrunchy.id)
false
} else {
// use tmdb search to get media info
mediaMeta = null // set mediaMeta to null, if metaDB doesn't know the media
tmdbApiController.search(stripTitleInfo(media.title), media.type)
Crunchyroll.postWatchlist(seriesCrunchy.id)
true
}
}
tmdbResult = when (media.type) {
MediaType.MOVIE -> tmdbApiController.getMovieDetails(tmdbId)
MediaType.TVSHOW -> tmdbApiController.getTVShowDetails(tmdbId)
else -> null
}
// get season info, if metaDB knows the tv show
tmdbTVSeason = if (media.type == MediaType.TVSHOW && mediaMeta != null) {
val tvShowMeta = mediaMeta as TVShowMeta
tmdbApiController.getTVSeasonDetails(tvShowMeta.tmdbId, tvShowMeta.tmdbSeasonNumber)
} else {
null
}
if (media.type == MediaType.TVSHOW) {
//nextEpisode = media.episodes.firstOrNull{ !it.watched } ?: media.episodes.first()
nextEpisodeId = media.playlist.firstOrNull { !it.watched }?.mediaId
?: media.playlist.first().mediaId
}
suspend fun updateOnResume() {
joinAll(
viewModelScope.launch {
val episodeIDs = episodesCrunchy.items.map { it.id }
currentPlayheads.clear()
currentPlayheads.putAll(Crunchyroll.playheads(episodeIDs))
},
viewModelScope.launch { upNextSeries = Crunchyroll.upNextSeries(seriesCrunchy.id) }
)
}
/**
@ -79,36 +167,11 @@ class MediaFragmentViewModel(application: Application) : AndroidViewModel(applic
* if no matching is found, use first episode
*/
fun updateNextEpisode(episodeId: Int) {
if (media.type == MediaType.MOVIE) return // return if movie
nextEpisodeId = media.playlist.firstOrNull { it.index > media.getEpisodeById(episodeId).index }?.mediaId
?: media.playlist.first().mediaId
// TODO reimplement if needed
// if (media.type == MediaType.MOVIE) return // return if movie
//
// nextEpisodeId = media.playlist.firstOrNull { it.index > media.getEpisodeById(episodeId).index }?.mediaId
// ?: media.playlist.first().mediaId
}
// remove unneeded info from the media title before searching
private fun stripTitleInfo(title: String): String {
return title.replace("(Sub)", "")
.replace(Regex("-?\\s?[0-9]+.\\s?(Staffel|Season)"), "")
.replace(Regex("(Staffel|Season)\\s?[0-9]+"), "")
.trim()
}
/** guess Season from title
* if the title ends with a number, that could be the season
* if the title ends with Regex("-?\\s?[0-9]+.\\s?(Staffel|Season)") or
* Regex("(Staffel|Season)\\s?[0-9]+"), that is the season information
*/
private fun guessSeasonFromTitle(title: String): Int {
val helpTitle = title.replace("(Sub)", "").trim()
Log.d("test", "helpTitle: $helpTitle")
return if (helpTitle.last().isDigit()) {
helpTitle.last().digitToInt()
} else {
Regex("([0-9]+.\\s?(Staffel|Season))|((Staffel|Season)\\s?[0-9]+)")
.find(helpTitle)
?.value?.filter { it.isDigit() }?.toInt() ?: 1
}
}
}
}

View File

@ -4,18 +4,18 @@ import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.inputmethod.EditorInfo
import androidx.fragment.app.Fragment
import androidx.lifecycle.lifecycleScope
import kotlinx.coroutines.*
import org.mosad.teapod.R
import org.mosad.teapod.databinding.FragmentOnLoginBinding
import org.mosad.teapod.parser.AoDParser
import org.mosad.teapod.parser.crunchyroll.Crunchyroll
import org.mosad.teapod.preferences.EncryptedPreferences
class OnLoginFragment: Fragment() {
private lateinit var binding: FragmentOnLoginBinding
private var loginJob: Job? = null
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {
binding = FragmentOnLoginBinding.inflate(inflater, container, false)
@ -29,24 +29,40 @@ class OnLoginFragment: Fragment() {
private fun initActions() {
binding.buttonLogin.setOnClickListener {
// get login credentials from gui
val email = binding.editTextLogin.text.toString()
val password = binding.editTextPassword.text.toString()
onLogin()
}
EncryptedPreferences.saveCredentials(email, password, requireContext()) // save the credentials
binding.editTextPassword.setOnEditorActionListener { _, actionId, _ ->
return@setOnEditorActionListener when (actionId) {
EditorInfo.IME_ACTION_DONE -> {
onLogin()
false // false will hide the keyboards
}
else -> false
}
}
binding.buttonLogin.isClickable = false
loginJob = lifecycleScope.launch {
if (AoDParser.login()) {
// if login was successful, switch to main
if (activity is OnboardingActivity) {
(activity as OnboardingActivity).launchMainActivity()
}
} else {
withContext(Dispatchers.Main) {
binding.textLoginDesc.text = getString(R.string.on_login_failed)
binding.buttonLogin.isClickable = true
}
}
private fun onLogin() {
// get login credentials from gui
val email = binding.editTextLogin.text.toString()
val password = binding.editTextPassword.text.toString()
binding.buttonLogin.isClickable = false
// FIXME, this seems to run blocking
lifecycleScope.launch {
// try login credentials
val login = Crunchyroll.login(email, password)
if (login) {
// save the credentials and show the main activity
EncryptedPreferences.saveCredentials(email, password, requireContext())
if (activity is OnboardingActivity) (activity as OnboardingActivity).launchMainActivity()
} else {
withContext(Dispatchers.Main) {
binding.textLoginDesc.text = getString(R.string.on_login_failed)
binding.buttonLogin.isClickable = true
}
}
}

View File

@ -16,7 +16,7 @@ class OnboardingActivity : AppCompatActivity() {
private lateinit var binding: ActivityOnboardingBinding
private lateinit var pagerAdapter: FragmentStateAdapter
private val fragments = arrayOf(OnLoginFragment())
private val fragments = arrayOf(OnWelcomeFragment(), OnLoginFragment())
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)

View File

@ -1,3 +1,25 @@
/**
* Teapod
*
* Copyright 2020-2022 <seil0@mosad.xyz>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301, USA.
*
*/
package org.mosad.teapod.ui.activity.player
import android.animation.Animator
@ -29,6 +51,7 @@ import kotlinx.android.synthetic.main.activity_player.*
import kotlinx.android.synthetic.main.player_controls.*
import kotlinx.coroutines.launch
import org.mosad.teapod.R
import org.mosad.teapod.parser.crunchyroll.NoneEpisode
import org.mosad.teapod.preferences.Preferences
import org.mosad.teapod.ui.components.EpisodesListPlayer
import org.mosad.teapod.ui.components.LanguageSettingsPlayer
@ -57,9 +80,9 @@ class PlayerActivity : AppCompatActivity() {
setContentView(R.layout.activity_player)
hideBars() // Initial hide the bars
model.loadMedia(
intent.getIntExtra(getString(R.string.intent_media_id), 0),
intent.getIntExtra(getString(R.string.intent_episode_id), 0)
model.loadMediaAsync(
intent.getStringExtra(getString(R.string.intent_season_id)) ?: "",
intent.getStringExtra(getString(R.string.intent_episode_id)) ?: ""
)
model.currentEpisodeChangedListener.add { onMediaChanged() }
gestureDetector = GestureDetectorCompat(this, PlayerGestureListener())
@ -120,11 +143,11 @@ class PlayerActivity : AppCompatActivity() {
// when the intent changed, load the new media and play it
intent?.let {
model.loadMedia(
it.getIntExtra(getString(R.string.intent_media_id), 0),
it.getIntExtra(getString(R.string.intent_episode_id), 0)
model.loadMediaAsync(
it.getStringExtra(getString(R.string.intent_season_id)) ?: "",
it.getStringExtra(getString(R.string.intent_episode_id)) ?: ""
)
model.playEpisode(model.currentEpisode.mediaId, replace = true)
model.playCurrentMedia()
}
}
@ -171,11 +194,6 @@ class PlayerActivity : AppCompatActivity() {
}
private fun initPlayer() {
if (model.media.aodId < 0) {
Log.e(javaClass.name, "No media was set.")
this.finish()
}
initVideoView()
initTimeUpdates()
@ -206,14 +224,15 @@ class PlayerActivity : AppCompatActivity() {
else -> View.VISIBLE
}
if (state == ExoPlayer.STATE_ENDED && model.nextEpisodeId != null && Preferences.autoplay) {
if (state == ExoPlayer.STATE_ENDED && hasNextEpisode() && Preferences.autoplay) {
playNextEpisode()
}
}
})
// revert back to the old behaviour (blocking init) in case there are any issues with async init
// start playing the current episode, after all needed player components have been initialized
model.playEpisode(model.currentEpisode.mediaId, true)
//model.playCurrentMedia(model.currentPlayhead)
}
@SuppressLint("ClickableViewAccessibility")
@ -251,9 +270,10 @@ class PlayerActivity : AppCompatActivity() {
}
private fun initGUI() {
if (model.media.type == DataTypes.MediaType.MOVIE) {
button_episodes.visibility = View.GONE
}
// TODO reimplement for cr
// if (model.media.type == DataTypes.MediaType.MOVIE) {
// button_episodes.visibility = View.GONE
// }
}
private fun initTimeUpdates() {
@ -277,7 +297,7 @@ class PlayerActivity : AppCompatActivity() {
// if remaining time < 20 sec, a next ep is set, autoplay is enabled and not in pip:
// show next ep button
if (remainingTime in 1..20000) {
if (!btnNextEpIsVisible && model.nextEpisodeId != null && Preferences.autoplay && !isInPiPMode()) {
if (!btnNextEpIsVisible && hasNextEpisode() && Preferences.autoplay && !isInPiPMode()) {
showButtonNextEp()
}
} else if (btnNextEpIsVisible) {
@ -329,24 +349,29 @@ class PlayerActivity : AppCompatActivity() {
}
/**
* update title text and next ep button visibility, set ignoreNextStateEnded
* This methode is called, if the current episode has changed.
* Update title text and next ep button visibility.
* If the currentEpisode changed to NoneEpisode, exit the activity.
*/
private fun onMediaChanged() {
if (model.currentEpisode == NoneEpisode) {
Log.e(javaClass.name, "No media was set.")
this.finish()
}
exo_text_title.text = model.getMediaTitle()
// hide the next ep button, if there is none
button_next_ep_c.visibility = if (model.nextEpisodeId == null) {
View.GONE
} else {
View.VISIBLE
}
// hide the next episode button, if there is none
button_next_ep_c.visibility = if (hasNextEpisode()) View.VISIBLE else View.GONE
}
// hide the episodes button, if the media type changed
button_episodes.visibility = if (model.media.type == DataTypes.MediaType.MOVIE) {
View.GONE
} else {
View.VISIBLE
}
/**
* Check if the current episode has a next episode.
*
* @return Boolean: true if there is a next episode, else false.
*/
private fun hasNextEpisode(): Boolean {
return (model.currentEpisode.nextEpisodeId != null && !model.currentEpisodeIsLastEpisode())
}
/**

View File

@ -1,3 +1,25 @@
/**
* Teapod
*
* Copyright 2020-2022 <seil0@mosad.xyz>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301, USA.
*
*/
package org.mosad.teapod.ui.activity.player
import android.app.Application
@ -6,24 +28,27 @@ import android.support.v4.media.session.MediaSessionCompat
import android.util.Log
import androidx.lifecycle.AndroidViewModel
import androidx.lifecycle.viewModelScope
import com.google.android.exoplayer2.C
import com.google.android.exoplayer2.ExoPlayer
import com.google.android.exoplayer2.MediaItem
import com.google.android.exoplayer2.Player
import com.google.android.exoplayer2.SimpleExoPlayer
import com.google.android.exoplayer2.ext.mediasession.MediaSessionConnector
import com.google.android.exoplayer2.source.MediaSource
import com.google.android.exoplayer2.source.hls.HlsMediaSource
import com.google.android.exoplayer2.upstream.DefaultDataSourceFactory
import com.google.android.exoplayer2.util.Util
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.joinAll
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking
import org.mosad.teapod.R
import org.mosad.teapod.parser.AoDParser
import org.mosad.teapod.parser.crunchyroll.Crunchyroll
import org.mosad.teapod.parser.crunchyroll.NoneEpisode
import org.mosad.teapod.parser.crunchyroll.NoneEpisodes
import org.mosad.teapod.parser.crunchyroll.NonePlayback
import org.mosad.teapod.preferences.Preferences
import org.mosad.teapod.util.*
import org.mosad.teapod.util.tmdb.TMDBApiController
import org.mosad.teapod.util.EpisodeMeta
import org.mosad.teapod.util.tmdb.TMDBTVSeason
import java.util.*
import kotlin.collections.ArrayList
/**
* PlayerViewModel handles all stuff related to media/episodes.
@ -37,25 +62,44 @@ class PlayerViewModel(application: Application) : AndroidViewModel(application)
private val mediaSession = MediaSessionCompat(application, "TEAPOD_PLAYER_SESSION")
val currentEpisodeChangedListener = ArrayList<() -> Unit>()
private val preferredLanguage = if (Preferences.preferSecondary) Locale.JAPANESE else Locale.GERMAN
private var currentPlayhead: Long = 0
var media: AoDMedia = AoDMediaNone
internal set
var mediaMeta: Meta? = null
internal set
// tmdb/meta data
// TODO meta data currently not implemented for cr
// var mediaMeta: Meta? = null
// internal set
var tmdbTVSeason: TMDBTVSeason? =null
internal set
var currentEpisode = AoDEpisodeNone
internal set
var currentEpisodeMeta: EpisodeMeta? = null
internal set
var nextEpisodeId: Int? = null
// crunchyroll episodes/playback
var episodes = NoneEpisodes
internal set
var currentLanguage: Locale = Locale.ROOT
var currentEpisode = NoneEpisode
internal set
var currentPlayback = NonePlayback
// current playback settings
var currentLanguage: Locale = Preferences.preferredLocale
internal set
init {
initMediaSession()
player.addListener(object : Player.Listener {
override fun onPlaybackStateChanged(state: Int) {
super.onPlaybackStateChanged(state)
if (state == ExoPlayer.STATE_ENDED) updatePlayhead()
}
override fun onIsPlayingChanged(isPlaying: Boolean) {
super.onIsPlayingChanged(isPlaying)
if (!isPlaying) updatePlayhead()
}
})
}
override fun onCleared() {
@ -78,35 +122,29 @@ class PlayerViewModel(application: Application) : AndroidViewModel(application)
mediaSession.isActive = true
}
fun loadMedia(mediaId: Int, episodeId: Int) {
runBlocking {
media = AoDParser.getMediaById(mediaId)
mediaMeta = loadMediaMeta(media.aodId) // can be done blocking, since it should be cached
}
fun loadMediaAsync(seasonId: String, episodeId: String) = viewModelScope.launch {
episodes = Crunchyroll.episodes(seasonId)
setCurrentEpisode(episodeId)
playCurrentMedia(currentPlayhead)
// TODO reimplement for cr
// run async as it should be loaded by the time the episodes a
viewModelScope.launch {
// get season info, if metaDB knows the tv show
if (media.type == DataTypes.MediaType.TVSHOW && mediaMeta != null) {
val tvShowMeta = mediaMeta as TVShowMeta
tmdbTVSeason = TMDBApiController().getTVSeasonDetails(tvShowMeta.tmdbId, tvShowMeta.tmdbSeasonNumber)
}
}
currentEpisode = media.getEpisodeById(episodeId)
nextEpisodeId = selectNextEpisode()
currentEpisodeMeta = getEpisodeMetaByAoDMediaId(currentEpisode.mediaId)
currentLanguage = currentEpisode.getPreferredStream(preferredLanguage).language
// viewModelScope.launch {
// // get tmdb season info, if metaDB knows the tv show
// if (media.type == DataTypes.MediaType.TVSHOW && mediaMeta != null) {
// val tvShowMeta = mediaMeta as TVShowMeta
// tmdbTVSeason = TMDBApiController().getTVSeasonDetails(tvShowMeta.tmdbId, tvShowMeta.tmdbSeasonNumber)
// }
// }
//
// currentEpisodeMeta = getEpisodeMetaByAoDMediaId(currentEpisodeAoD.mediaId)
// currentLanguage = currentEpisodeAoD.getPreferredStream(preferredLanguage).language
}
fun setLanguage(language: Locale) {
currentLanguage = language
val seekTime = player.currentPosition
val mediaSource = HlsMediaSource.Factory(dataSourceFactory).createMediaSource(
MediaItem.fromUri(Uri.parse(currentEpisode.getPreferredStream(language).url))
)
playMedia(mediaSource, true, seekTime)
playCurrentMedia(player.currentPosition)
}
// player actions
@ -120,92 +158,144 @@ class PlayerViewModel(application: Application) : AndroidViewModel(application)
}
/**
* play the next episode, if nextEpisode is not null
* play the next episode, if nextEpisodeId is not null
*/
fun playNextEpisode() = nextEpisodeId?.let { it ->
playEpisode(it, replace = true)
fun playNextEpisode() = currentEpisode.nextEpisodeId?.let { nextEpisodeId ->
updatePlayhead() // update playhead before switching to new episode
setCurrentEpisode(nextEpisodeId, startPlayback = true)
}
/**
* Set currentEpisode and start playing it.
* Update nextEpisode to reflect the change and update
* the watched state for the now playing episode.
*
* @param episodeId The aod media id of the episode to play.
* @param replace (default = false)
* @param seekPosition The seek position for the episode (default = 0).
* Set currentEpisodeCr to the episode of the given ID
* @param episodeId The ID of the episode you want to set currentEpisodeCr to
*/
fun playEpisode(episodeId: Int, replace: Boolean = false, seekPosition: Long = 0) {
currentEpisode = media.getEpisodeById(episodeId)
currentLanguage = currentEpisode.getPreferredStream(currentLanguage).language
currentEpisodeMeta = getEpisodeMetaByAoDMediaId(currentEpisode.mediaId)
nextEpisodeId = selectNextEpisode()
fun setCurrentEpisode(episodeId: String, startPlayback: Boolean = false) {
currentEpisode = episodes.items.find { episode ->
episode.id == episodeId
} ?: NoneEpisode
// update player gui (title, next ep button) after nextEpisodeId has been set
// update player gui (title, next ep button) after currentEpisode has changed
currentEpisodeChangedListener.forEach { it() }
val mediaSource = HlsMediaSource.Factory(dataSourceFactory).createMediaSource(
MediaItem.fromUri(Uri.parse(currentEpisode.getPreferredStream(currentLanguage).url))
)
playMedia(mediaSource, replace, seekPosition)
// needs to be blocking, currentPlayback must be present when calling playCurrentMedia()
runBlocking {
joinAll(
viewModelScope.launch(Dispatchers.IO) {
currentPlayback = Crunchyroll.playback(currentEpisode.playback)
},
viewModelScope.launch(Dispatchers.IO) {
Crunchyroll.playheads(listOf(currentEpisode.id))[currentEpisode.id]?.let {
// if the episode was fully watched, start at the beginning
currentPlayhead = if (it.fullyWatched) {
0
} else {
(it.playhead.times(1000)).toLong()
}
}
}
)
}
println("loaded playback ${currentEpisode.playback}")
// if episodes has not been watched, mark as watched
if (!currentEpisode.watched) {
viewModelScope.launch {
AoDParser.markAsWatched(media.aodId, currentEpisode.mediaId)
}
// TODO update metadata and language (it should not be needed to update the language here!)
if (startPlayback) {
playCurrentMedia()
}
}
/**
* change the players media source and start playback
* Play the current media from currentPlaybackCr.
*
* @param seekPosition The seek position for the episode (default = 0).
*/
fun playMedia(source: MediaSource, replace: Boolean = false, seekPosition: Long = 0) {
if (replace || player.contentDuration == C.TIME_UNSET) {
player.setMediaSource(source)
player.prepare()
if (seekPosition > 0) player.seekTo(seekPosition)
player.playWhenReady = true
fun playCurrentMedia(seekPosition: Long = 0) {
// get preferred stream url, set current language if it differs from the preferred one
val preferredLocale = currentLanguage
val fallbackLocal = Locale.US
val url = when {
currentPlayback.streams.adaptive_hls.containsKey(preferredLocale.toLanguageTag()) -> {
currentPlayback.streams.adaptive_hls[preferredLocale.toLanguageTag()]?.url
}
currentPlayback.streams.adaptive_hls.containsKey(fallbackLocal.toLanguageTag()) -> {
currentLanguage = fallbackLocal
currentPlayback.streams.adaptive_hls[fallbackLocal.toLanguageTag()]?.url
}
else -> {
// if no language tag is present use the first entry
currentLanguage = Locale.ROOT
currentPlayback.streams.adaptive_hls.entries.first().value.url
}
}
println("stream url: $url")
// create the media source object
val mediaSource = HlsMediaSource.Factory(dataSourceFactory).createMediaSource(
MediaItem.fromUri(Uri.parse(url))
)
// the actual player playback code
player.setMediaSource(mediaSource)
player.prepare()
if (seekPosition > 0) player.seekTo(seekPosition)
player.playWhenReady = true
}
/**
* Returns the current episode title (with episode number, if it's a tv show)
*/
fun getMediaTitle(): String {
return if (media.type == DataTypes.MediaType.TVSHOW) {
// currentEpisode.episodeNumber defines the media type (tv show = none null, movie = null)
return if (currentEpisode.episodeNumber != null) {
getApplication<Application>().getString(
R.string.component_episode_title,
currentEpisode.numberStr,
currentEpisode.description
currentEpisode.episode,
currentEpisode.title
)
} else {
currentEpisode.title
}
}
fun getEpisodeMetaByAoDMediaId(aodMediaId: Int): EpisodeMeta? {
val meta = mediaMeta
return if (meta is TVShowMeta) {
meta.episodes.firstOrNull { it.aodMediaId == aodMediaId }
} else {
null
}
/**
* Check if the current episode is the last in the episodes list.
*
* @return Boolean: true if it is the last, else false.
*/
fun currentEpisodeIsLastEpisode(): Boolean {
return episodes.items.lastOrNull()?.id == currentEpisode.id
}
private suspend fun loadMediaMeta(aodId: Int): Meta? {
return if (media.type == DataTypes.MediaType.TVSHOW) {
MetaDBController().getTVShowMetadata(aodId)
} else {
null
}
}
// TODO reimplement for cr
// fun getEpisodeMetaByAoDMediaId(aodMediaId: Int): EpisodeMeta? {
// val meta = mediaMeta
// return if (meta is TVShowMeta) {
// meta.episodes.firstOrNull { it.aodMediaId == aodMediaId }
// } else {
// null
// }
// }
//
// private suspend fun loadMediaMeta(aodId: Int): Meta? {
// return if (media.type == DataTypes.MediaType.TVSHOW) {
// MetaDBController().getTVShowMetadata(aodId)
// } else {
// null
// }
//
// return null
// }
/**
* Based on the current episodes index, get the next episode.
* @return The next episode or null if there is none.
* Update the playhead of the current episode, if currentPosition > 1000ms.
*/
private fun selectNextEpisode(): Int? {
return media.playlist.firstOrNull {
it.index > media.getEpisodeById(currentEpisode.mediaId).index
}?.mediaId
private fun updatePlayhead() {
val playhead = (player.currentPosition / 1000)
if (playhead > 0) {
viewModelScope.launch { Crunchyroll.postPlayheads(currentEpisode.id, playhead.toInt()) }
Log.i(javaClass.name, "Set playhead for episode ${currentEpisode.id} to $playhead sec.")
}
}
}
}

View File

@ -28,15 +28,16 @@ class EpisodesListPlayer @JvmOverloads constructor(
}
model?.let {
adapterRecEpisodes = PlayerEpisodeItemAdapter(model.media.playlist, model.tmdbTVSeason?.episodes)
adapterRecEpisodes.onImageClick = { _, position ->
adapterRecEpisodes = PlayerEpisodeItemAdapter(model.episodes, model.tmdbTVSeason?.episodes)
adapterRecEpisodes.onImageClick = {_, episodeId ->
(this.parent as ViewGroup).removeView(this)
model.playEpisode(model.media.playlist[position].mediaId, replace = true)
model.setCurrentEpisode(episodeId, startPlayback = true)
}
adapterRecEpisodes.currentSelected = model.currentEpisode.index
// episodeNumber starts at 1, we need the episode index -> - 1
adapterRecEpisodes.currentSelected = model.currentEpisode.episodeNumber?.minus(1) ?: 0
binding.recyclerEpisodesPlayer.adapter = adapterRecEpisodes
binding.recyclerEpisodesPlayer.scrollToPosition(model.currentEpisode.index)
binding.recyclerEpisodesPlayer.scrollToPosition(adapterRecEpisodes.currentSelected)
}
}

View File

@ -5,13 +5,15 @@ import android.animation.AnimatorListenerAdapter
import android.animation.ObjectAnimator
import android.content.Context
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.View
import android.widget.FrameLayout
import kotlinx.android.synthetic.main.button_fast_forward.view.*
import org.mosad.teapod.R
import org.mosad.teapod.databinding.ButtonFastForwardBinding
class FastForwardButton(context: Context, attrs: AttributeSet?): FrameLayout(context, attrs) {
private val binding = ButtonFastForwardBinding.inflate(LayoutInflater.from(context))
private val animationDuration: Long = 800
private val buttonAnimation: ObjectAnimator
private val labelAnimation: ObjectAnimator
@ -19,30 +21,30 @@ class FastForwardButton(context: Context, attrs: AttributeSet?): FrameLayout(con
var onAnimationEndCallback: (() -> Unit)? = null
init {
inflate(context, R.layout.button_fast_forward, this)
addView(binding.root)
buttonAnimation = ObjectAnimator.ofFloat(imageButton, View.ROTATION, 0f, 50f).apply {
buttonAnimation = ObjectAnimator.ofFloat(binding.imageButton, View.ROTATION, 0f, 50f).apply {
duration = animationDuration / 4
repeatCount = 1
repeatMode = ObjectAnimator.REVERSE
addListener(object : AnimatorListenerAdapter() {
override fun onAnimationStart(animation: Animator?) {
imageButton.isEnabled = false // disable button
imageButton.setBackgroundResource(R.drawable.ic_baseline_forward_24)
binding.imageButton.isEnabled = false // disable button
binding.imageButton.setBackgroundResource(R.drawable.ic_baseline_forward_24)
}
})
}
labelAnimation = ObjectAnimator.ofFloat(textView, View.TRANSLATION_X, 35f).apply {
labelAnimation = ObjectAnimator.ofFloat(binding.textView, View.TRANSLATION_X, 35f).apply {
duration = animationDuration
addListener(object : AnimatorListenerAdapter() {
// the label animation takes longer then the button animation, reset stuff in here
override fun onAnimationEnd(animation: Animator?) {
imageButton.isEnabled = true // enable button
imageButton.setBackgroundResource(R.drawable.ic_baseline_forward_10_24)
binding.imageButton.isEnabled = true // enable button
binding.imageButton.setBackgroundResource(R.drawable.ic_baseline_forward_10_24)
textView.visibility = View.GONE
textView.animate().translationX(0f)
binding.textView.visibility = View.GONE
binding.textView.animate().translationX(0f)
onAnimationEndCallback?.invoke()
}
@ -51,7 +53,7 @@ class FastForwardButton(context: Context, attrs: AttributeSet?): FrameLayout(con
}
fun setOnButtonClickListener(func: FastForwardButton.() -> Unit) {
imageButton.setOnClickListener {
binding.imageButton.setOnClickListener {
func()
}
}
@ -61,7 +63,7 @@ class FastForwardButton(context: Context, attrs: AttributeSet?): FrameLayout(con
buttonAnimation.start()
// run lbl animation
textView.visibility = View.VISIBLE
binding.textView.visibility = View.VISIBLE
labelAnimation.start()
}

View File

@ -16,6 +16,7 @@ import org.mosad.teapod.databinding.PlayerLanguageSettingsBinding
import org.mosad.teapod.ui.activity.player.PlayerViewModel
import java.util.*
// TODO port to DialogFragment
class LanguageSettingsPlayer @JvmOverloads constructor(
context: Context,
attrs: AttributeSet? = null,
@ -24,16 +25,17 @@ class LanguageSettingsPlayer @JvmOverloads constructor(
) : LinearLayout(context, attrs, defStyleAttr) {
private val binding = PlayerLanguageSettingsBinding.inflate(LayoutInflater.from(context), this, true)
var onViewRemovedAction: (() -> Unit)? = null // TODO find a better solution for this
var onViewRemovedAction: (() -> Unit)? = null
private var currentLanguage = model?.currentLanguage ?: Locale.ROOT
private var selectedLocale = model?.currentLanguage ?: Locale.ROOT
init {
model?.let {
model.currentEpisode.streams.forEach { stream ->
addLanguage(stream.language.displayName, stream.language == currentLanguage) {
currentLanguage = stream.language
updateSelectedLanguage(it as TextView)
model?.let { m ->
m.currentPlayback.streams.adaptive_hls.keys.forEach { languageTag ->
val locale = Locale.forLanguageTag(languageTag)
addLanguage(locale, locale == m.currentLanguage) { v ->
selectedLocale = locale
updateSelectedLanguage(v as TextView)
}
}
}
@ -41,16 +43,16 @@ class LanguageSettingsPlayer @JvmOverloads constructor(
binding.buttonCloseLanguageSettings.setOnClickListener { close() }
binding.buttonCancel.setOnClickListener { close() }
binding.buttonSelect.setOnClickListener {
model?.setLanguage(currentLanguage)
model?.setLanguage(selectedLocale)
close()
}
}
private fun addLanguage(str: String, isSelected: Boolean, onClick: OnClickListener) {
private fun addLanguage(locale: Locale, isSelected: Boolean, onClick: OnClickListener) {
val text = TextView(context).apply {
height = 96
gravity = Gravity.CENTER_VERTICAL
text = str
text = if (locale == Locale.ROOT) context.getString(R.string.no_subtitles) else locale.displayLanguage
setTextSize(TypedValue.COMPLEX_UNIT_SP, 16f)
if (isSelected) {

View File

@ -31,6 +31,7 @@ import com.afollestad.materialdialogs.customview.customView
import com.afollestad.materialdialogs.customview.getCustomView
import org.mosad.teapod.R
// TODO rework and port away from MaterialDialog
class LoginDialog(val context: Context, firstTry: Boolean) {
private val dialog = MaterialDialog(context, BottomSheet())

View File

@ -5,13 +5,15 @@ import android.animation.AnimatorListenerAdapter
import android.animation.ObjectAnimator
import android.content.Context
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.View
import android.widget.FrameLayout
import kotlinx.android.synthetic.main.button_rewind.view.*
import org.mosad.teapod.R
import org.mosad.teapod.databinding.ButtonRewindBinding
class RewindButton(context: Context, attrs: AttributeSet): FrameLayout(context, attrs) {
private val binding = ButtonRewindBinding.inflate(LayoutInflater.from(context))
private val animationDuration: Long = 800
private val buttonAnimation: ObjectAnimator
private val labelAnimation: ObjectAnimator
@ -19,29 +21,29 @@ class RewindButton(context: Context, attrs: AttributeSet): FrameLayout(context,
var onAnimationEndCallback: (() -> Unit)? = null
init {
inflate(context, R.layout.button_rewind, this)
addView(binding.root)
buttonAnimation = ObjectAnimator.ofFloat(imageButton, View.ROTATION, 0f, -50f).apply {
buttonAnimation = ObjectAnimator.ofFloat(binding.imageButton, View.ROTATION, 0f, -50f).apply {
duration = animationDuration / 4
repeatCount = 1
repeatMode = ObjectAnimator.REVERSE
addListener(object : AnimatorListenerAdapter() {
override fun onAnimationStart(animation: Animator?) {
imageButton.isEnabled = false // disable button
imageButton.setBackgroundResource(R.drawable.ic_baseline_rewind_24)
binding.imageButton.isEnabled = false // disable button
binding.imageButton.setBackgroundResource(R.drawable.ic_baseline_rewind_24)
}
})
}
labelAnimation = ObjectAnimator.ofFloat(textView, View.TRANSLATION_X, -35f).apply {
labelAnimation = ObjectAnimator.ofFloat(binding.textView, View.TRANSLATION_X, -35f).apply {
duration = animationDuration
addListener(object : AnimatorListenerAdapter() {
override fun onAnimationEnd(animation: Animator?) {
imageButton.isEnabled = true // enable button
imageButton.setBackgroundResource(R.drawable.ic_baseline_rewind_10_24)
binding.imageButton.isEnabled = true // enable button
binding.imageButton.setBackgroundResource(R.drawable.ic_baseline_rewind_10_24)
textView.visibility = View.GONE
textView.animate().translationX(0f)
binding.textView.visibility = View.GONE
binding.textView.animate().translationX(0f)
onAnimationEndCallback?.invoke()
}
@ -50,7 +52,7 @@ class RewindButton(context: Context, attrs: AttributeSet): FrameLayout(context,
}
fun setOnButtonClickListener(func: RewindButton.() -> Unit) {
imageButton.setOnClickListener {
binding.imageButton.setOnClickListener {
func()
}
}
@ -60,7 +62,7 @@ class RewindButton(context: Context, attrs: AttributeSet): FrameLayout(context,
buttonAnimation.start()
// run lbl animation
textView.visibility = View.VISIBLE
binding.textView.visibility = View.VISIBLE
labelAnimation.start()
}

View File

@ -3,10 +3,10 @@ package org.mosad.teapod.util
import java.util.Locale
class DataTypes {
enum class MediaType {
OTHER,
MOVIE,
TVSHOW
enum class MediaType(val str: String) {
OTHER("other"),
MOVIE("movie"), // TODO
TVSHOW("series")
}
enum class Theme(val str: String) {
@ -35,9 +35,9 @@ data class ThirdPartyComponent(
* it is uses in the ItemMediaAdapter (RecyclerView)
*/
data class ItemMedia(
val id: Int, // aod path id
val id: String,
val title: String,
val posterUrl: String
val posterUrl: String,
)
// TODO replace playlist: List<AoDEpisode> with a map?
@ -75,7 +75,7 @@ data class AoDEpisode(
* @return the preferred stream, if not present use the first stream
*/
fun getPreferredStream(language: Locale) = streams.firstOrNull { it.language == language }
?: streams.first()
?: Stream("", Locale.ROOT)
}
data class Stream(
@ -111,7 +111,7 @@ val AoDEpisodeNone = AoDEpisode(
"",
"",
-1,
false,
true,
"",
mutableListOf()
)

View File

@ -1,7 +1,7 @@
/**
* Teapod
*
* Copyright 2020-2021 <seil0@mosad.xyz>
* Copyright 2020-2022 <seil0@mosad.xyz>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
@ -29,6 +29,9 @@ import kotlinx.coroutines.*
import java.io.FileNotFoundException
import java.net.URL
/**
* TODO remove gson usage
*/
class MetaDBController {
companion object {

View File

@ -1,89 +0,0 @@
package org.mosad.teapod.util
import android.content.Context
import android.net.Uri
import android.util.Log
import com.google.gson.Gson
import com.google.gson.JsonParser
import kotlinx.coroutines.*
import java.io.File
import java.io.FileReader
import java.io.FileWriter
/**
* This controller contains the logic for permanently saved data.
* On load, it loads the saved files into the variables
*/
object StorageController {
private const val fileNameMyList = "my_list.json"
val myList = ArrayList<Int>() // a list of saved mediaIds
fun load(context: Context) {
loadMyList(context)
}
fun loadMyList(context: Context) {
val file = File(context.filesDir, fileNameMyList)
if (!file.exists()) runBlocking { saveMyList(context).join() }
try {
myList.clear()
myList.addAll(JsonParser.parseString(file.readText()).asJsonArray.map { it.asInt }.distinct())
} catch (ex: Exception) {
myList.clear()
Log.e(javaClass.name, "Parsing of My-List failed.")
}
}
fun saveMyList(context: Context): Job {
val file = File(context.filesDir, fileNameMyList)
return CoroutineScope(Dispatchers.IO).launch {
file.writeText(Gson().toJson(myList.distinct()))
}
}
fun exportMyList(context: Context, uri: Uri) {
try {
context.contentResolver.openFileDescriptor(uri, "w")?.use {
FileWriter(it.fileDescriptor).use { writer ->
writer.write(Gson().toJson(myList.distinct()))
}
}
} catch (ex: Exception) {
Log.e(javaClass.name, "Exporting my list failed.", ex)
}
}
/**
* import my list from a (previously exported) json file
* @param context the current context
* @param uri the uri of the selected file
* @return 0 if import was successfull, else 1
*/
fun importMyList(context: Context, uri: Uri): Int {
try {
val text = context.contentResolver.openFileDescriptor(uri, "r")?.use {
FileReader(it.fileDescriptor).use { reader ->
reader.readText()
}
}
myList.clear()
myList.addAll(JsonParser.parseString(text).asJsonArray.map { it.asInt }.distinct())
// after the list has been imported also save it
saveMyList(context)
} catch (ex: Exception) {
myList.clear()
Log.e(javaClass.name, "Importing my list failed.", ex)
return 1
}
return 0
}
}

View File

@ -1,7 +1,45 @@
package org.mosad.teapod.util
import android.widget.TextView
import org.mosad.teapod.parser.crunchyroll.Collection
import org.mosad.teapod.parser.crunchyroll.ContinueWatchingItem
import org.mosad.teapod.parser.crunchyroll.Item
import java.util.*
fun TextView.setDrawableTop(drawable: Int) {
this.setCompoundDrawablesWithIntrinsicBounds(0, drawable, 0, 0)
}
fun <T> concatenate(vararg lists: List<T>): List<T> {
return listOf(*lists).flatten()
}
// TODO move to correct location
fun Collection<Item>.toItemMediaList(): List<ItemMedia> {
return this.items.map {
ItemMedia(it.id, it.title, it.images.poster_wide[0][0].source)
}
}
@JvmName("toItemMediaListContinueWatchingItem")
fun Collection<ContinueWatchingItem>.toItemMediaList(): List<ItemMedia> {
return items.map {
ItemMedia(it.panel.episodeMetadata.seriesId, it.panel.title, it.panel.images.thumbnail[0][0].source)
}
}
fun List<ContinueWatchingItem>.toItemMediaList(): List<ItemMedia> {
return this.map {
ItemMedia(it.panel.episodeMetadata.seriesId, it.panel.title, it.panel.images.thumbnail[0][0].source)
}
}
fun Locale.toDisplayString(fallback: String): String {
return if (this.displayLanguage.isNotEmpty() && this.displayCountry.isNotEmpty()) {
"${this.displayLanguage} (${this.displayCountry})"
} else if (this.displayCountry.isNotEmpty()) {
this.displayLanguage
} else {
fallback
}
}

View File

@ -2,6 +2,7 @@ package org.mosad.teapod.util.adapter
import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.graphics.drawable.Drawable
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.core.content.ContextCompat
@ -11,12 +12,17 @@ import com.bumptech.glide.request.RequestOptions
import jp.wasabeef.glide.transformations.RoundedCornersTransformation
import org.mosad.teapod.R
import org.mosad.teapod.databinding.ItemEpisodeBinding
import org.mosad.teapod.util.AoDEpisode
import org.mosad.teapod.parser.crunchyroll.Episode
import org.mosad.teapod.parser.crunchyroll.PlayheadsMap
import org.mosad.teapod.util.tmdb.TMDBTVEpisode
class EpisodeItemAdapter(private val episodes: List<AoDEpisode>, private val tmdbEpisodes: List<TMDBTVEpisode>?) : RecyclerView.Adapter<EpisodeItemAdapter.EpisodeViewHolder>() {
class EpisodeItemAdapter(
private val episodes: List<Episode>,
private val tmdbEpisodes: List<TMDBTVEpisode>?,
private val playheads: PlayheadsMap
) : RecyclerView.Adapter<EpisodeItemAdapter.EpisodeViewHolder>() {
var onImageClick: ((String, Int) -> Unit)? = null
var onImageClick: ((seasonId: String, episodeId: String) -> Unit)? = null
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): EpisodeViewHolder {
return EpisodeViewHolder(ItemEpisodeBinding.inflate(LayoutInflater.from(parent.context), parent, false))
@ -26,35 +32,41 @@ class EpisodeItemAdapter(private val episodes: List<AoDEpisode>, private val tmd
val context = holder.binding.root.context
val ep = episodes[position]
val titleText = if (ep.hasDub()) {
context.getString(R.string.component_episode_title, ep.numberStr, ep.description)
val titleText = if (ep.episodeNumber != null) {
// for tv shows add ep prefix and episode number
if (ep.isDubbed) {
context.getString(R.string.component_episode_title, ep.episode, ep.title)
} else {
context.getString(R.string.component_episode_title_sub, ep.episode, ep.title)
}
} else {
context.getString(R.string.component_episode_title_sub, ep.numberStr, ep.description)
ep.title
}
holder.binding.textEpisodeTitle.text = titleText
holder.binding.textEpisodeDesc.text = if (ep.shortDesc.isNotEmpty()) {
ep.shortDesc
holder.binding.textEpisodeDesc.text = if (ep.description.isNotEmpty()) {
ep.description
} else if (tmdbEpisodes != null && position < tmdbEpisodes.size){
tmdbEpisodes[position].overview
} else {
""
}
if (ep.imageURL.isNotEmpty()) {
Glide.with(context).load(ep.imageURL)
// TODO is isNotEmpty() needed? also in PlayerEpisodeItemAdapter
if (ep.images.thumbnail[0][0].source.isNotEmpty()) {
Glide.with(context).load(ep.images.thumbnail[0][0].source)
.apply(RequestOptions.placeholderOf(ColorDrawable(Color.DKGRAY)))
.apply(RequestOptions.bitmapTransform(RoundedCornersTransformation(10, 0)))
.into(holder.binding.imageEpisode)
}
if (ep.watched) {
holder.binding.imageWatched.setImageDrawable(
ContextCompat.getDrawable(context, R.drawable.ic_baseline_check_circle_24)
)
// add watched icon to episode, if the episode id is present in playheads and fullyWatched
val watchedImage: Drawable? = if (playheads[ep.id]?.fullyWatched == true) {
ContextCompat.getDrawable(context, R.drawable.ic_baseline_check_circle_24)
} else {
holder.binding.imageWatched.setImageDrawable(null)
null
}
holder.binding.imageWatched.setImageDrawable(watchedImage)
}
override fun getItemCount(): Int {
@ -63,13 +75,20 @@ class EpisodeItemAdapter(private val episodes: List<AoDEpisode>, private val tmd
fun updateWatchedState(watched: Boolean, position: Int) {
// use getOrNull as there could be a index out of bound when running this in onResume()
episodes.getOrNull(position)?.watched = watched
// TODO
//episodes.getOrNull(position)?.watched = watched
}
inner class EpisodeViewHolder(val binding: ItemEpisodeBinding) : RecyclerView.ViewHolder(binding.root) {
inner class EpisodeViewHolder(val binding: ItemEpisodeBinding) :
RecyclerView.ViewHolder(binding.root) {
init {
// on image click return the episode id and index (within the adapter)
binding.imageEpisode.setOnClickListener {
onImageClick?.invoke(episodes[adapterPosition].title, adapterPosition)
onImageClick?.invoke(
episodes[bindingAdapterPosition].seasonId,
episodes[bindingAdapterPosition].id
)
}
}
}

View File

@ -2,19 +2,14 @@ package org.mosad.teapod.util.adapter
import android.view.LayoutInflater
import android.view.ViewGroup
import android.widget.Filter
import android.widget.Filterable
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import org.mosad.teapod.databinding.ItemMediaBinding
import org.mosad.teapod.util.ItemMedia
import java.util.*
class MediaItemAdapter(private val initMedia: List<ItemMedia>) : RecyclerView.Adapter<MediaItemAdapter.MediaViewHolder>(), Filterable {
class MediaItemAdapter(private val items: List<ItemMedia>) : RecyclerView.Adapter<MediaItemAdapter.MediaViewHolder>() {
var onItemClick: ((Int, Int) -> Unit)? = null
private val filter = MediaFilter()
private var filteredMedia = initMedia.map { it.copy() }
var onItemClick: ((id: String, position: Int) -> Unit)? = null
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MediaItemAdapter.MediaViewHolder {
return MediaViewHolder(ItemMediaBinding.inflate(LayoutInflater.from(parent.context), parent, false))
@ -22,58 +17,25 @@ class MediaItemAdapter(private val initMedia: List<ItemMedia>) : RecyclerView.Ad
override fun onBindViewHolder(holder: MediaItemAdapter.MediaViewHolder, position: Int) {
holder.binding.root.apply {
holder.binding.textTitle.text = filteredMedia[position].title
Glide.with(context).load(filteredMedia[position].posterUrl).into(holder.binding.imagePoster)
holder.binding.textTitle.text = items[position].title
Glide.with(context).load(items[position].posterUrl).into(holder.binding.imagePoster)
}
}
override fun getItemCount(): Int {
return filteredMedia.size
return items.size
}
override fun getFilter(): Filter {
return filter
}
fun updateMediaList(mediaList: List<ItemMedia>) {
filteredMedia = mediaList
}
inner class MediaViewHolder(val binding: ItemMediaBinding) : RecyclerView.ViewHolder(binding.root) {
inner class MediaViewHolder(val binding: ItemMediaBinding) :
RecyclerView.ViewHolder(binding.root) {
init {
binding.root.setOnClickListener {
onItemClick?.invoke(filteredMedia[adapterPosition].id, adapterPosition)
onItemClick?.invoke(
items[bindingAdapterPosition].id,
bindingAdapterPosition
)
}
}
}
inner class MediaFilter : Filter() {
override fun performFiltering(constraint: CharSequence?): FilterResults {
val filterTerm = constraint.toString().lowercase(Locale.ROOT)
val results = FilterResults()
val filteredList = if (filterTerm.isEmpty()) {
initMedia
} else {
initMedia.filter {
it.title.lowercase(Locale.ROOT).contains(filterTerm)
}
}
results.values = filteredList
results.count = filteredList.size
return results
}
@Suppress("unchecked_cast")
/**
* suppressing unchecked cast is safe, since we only use Media
*/
override fun publishResults(constraint: CharSequence?, results: FilterResults?) {
filteredMedia = results?.values as List<ItemMedia>
notifyDataSetChanged()
}
}
}

View File

@ -9,12 +9,12 @@ import com.bumptech.glide.request.RequestOptions
import jp.wasabeef.glide.transformations.RoundedCornersTransformation
import org.mosad.teapod.R
import org.mosad.teapod.databinding.ItemEpisodePlayerBinding
import org.mosad.teapod.util.AoDEpisode
import org.mosad.teapod.parser.crunchyroll.Episodes
import org.mosad.teapod.util.tmdb.TMDBTVEpisode
class PlayerEpisodeItemAdapter(private val episodes: List<AoDEpisode>, private val tmdbEpisodes: List<TMDBTVEpisode>?) : RecyclerView.Adapter<PlayerEpisodeItemAdapter.EpisodeViewHolder>() {
class PlayerEpisodeItemAdapter(private val episodes: Episodes, private val tmdbEpisodes: List<TMDBTVEpisode>?) : RecyclerView.Adapter<PlayerEpisodeItemAdapter.EpisodeViewHolder>() {
var onImageClick: ((String, Int) -> Unit)? = null
var onImageClick: ((seasonId: String, episodeId: String) -> Unit)? = null
var currentSelected: Int = -1 // -1, since position should never be < 0
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): EpisodeViewHolder {
@ -23,25 +23,30 @@ class PlayerEpisodeItemAdapter(private val episodes: List<AoDEpisode>, private v
override fun onBindViewHolder(holder: EpisodeViewHolder, position: Int) {
val context = holder.binding.root.context
val ep = episodes[position]
val ep = episodes.items[position]
val titleText = if (ep.hasDub()) {
context.getString(R.string.component_episode_title, ep.numberStr, ep.description)
val titleText = if (ep.episodeNumber != null) {
// for tv shows add ep prefix and episode number
if (ep.isDubbed) {
context.getString(R.string.component_episode_title, ep.episode, ep.title)
} else {
context.getString(R.string.component_episode_title_sub, ep.episode, ep.title)
}
} else {
context.getString(R.string.component_episode_title_sub, ep.numberStr, ep.description)
ep.title
}
holder.binding.textEpisodeTitle2.text = titleText
holder.binding.textEpisodeDesc2.text = if (ep.shortDesc.isNotEmpty()) {
ep.shortDesc
holder.binding.textEpisodeDesc2.text = if (ep.description.isNotEmpty()) {
ep.description
} else if (tmdbEpisodes != null && position < tmdbEpisodes.size){
tmdbEpisodes[position].overview
} else {
""
}
if (ep.imageURL.isNotEmpty()) {
Glide.with(context).load(ep.imageURL)
if (ep.images.thumbnail[0][0].source.isNotEmpty()) {
Glide.with(context).load(ep.images.thumbnail[0][0].source)
.apply(RequestOptions.bitmapTransform(RoundedCornersTransformation(10, 0)))
.into(holder.binding.imageEpisode)
}
@ -55,15 +60,18 @@ class PlayerEpisodeItemAdapter(private val episodes: List<AoDEpisode>, private v
}
override fun getItemCount(): Int {
return episodes.size
return episodes.items.size
}
inner class EpisodeViewHolder(val binding: ItemEpisodePlayerBinding) : RecyclerView.ViewHolder(binding.root) {
init {
binding.imageEpisode.setOnClickListener {
// don't execute, if it's the current episode
if (currentSelected != adapterPosition) {
onImageClick?.invoke(episodes[adapterPosition].title, adapterPosition)
if (currentSelected != bindingAdapterPosition) {
onImageClick?.invoke(
episodes.items[bindingAdapterPosition].seasonId,
episodes.items[bindingAdapterPosition].id
)
}
}
}

View File

@ -1,7 +1,7 @@
/**
* Teapod
*
* Copyright 2020-2021 <seil0@mosad.xyz>
* Copyright 2020-2022 <seil0@mosad.xyz>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
@ -23,115 +23,152 @@
package org.mosad.teapod.util.tmdb
import android.util.Log
import com.google.gson.Gson
import com.google.gson.JsonParser
import kotlinx.coroutines.*
import org.mosad.teapod.util.DataTypes.MediaType
import java.io.FileNotFoundException
import java.net.URL
import java.net.URLEncoder
import io.ktor.client.*
import io.ktor.client.call.*
import io.ktor.client.features.json.*
import io.ktor.client.features.json.serializer.*
import io.ktor.client.request.*
import io.ktor.client.statement.*
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.coroutineScope
import kotlinx.coroutines.invoke
import kotlinx.serialization.SerializationException
import kotlinx.serialization.json.Json
import org.mosad.teapod.preferences.Preferences
import org.mosad.teapod.util.concatenate
/**
* Controller for tmdb api integration.
* Data types are in TMDBDataTypes. For the type definitions see:
* https://developers.themoviedb.org/3/getting-started/introduction
*
* TODO evaluate Klaxon
*/
class TMDBApiController {
private val classTag = javaClass.name
private val json = Json { ignoreUnknownKeys = true }
private val client = HttpClient {
install(JsonFeature) {
serializer = KotlinxSerializer(json)
}
}
private val apiUrl = "https://api.themoviedb.org/3"
private val searchMovieUrl = "$apiUrl/search/movie"
private val searchTVUrl = "$apiUrl/search/tv"
private val detailsMovieUrl = "$apiUrl/movie"
private val detailsTVUrl = "$apiUrl/tv"
private val apiKey = "de959cf9c07a08b5ca7cb51cda9a40c2"
private val language = "de"
private val preparedParameters = "?api_key=$apiKey&language=$language"
companion object{
const val imageUrl = "https://image.tmdb.org/t/p/w500"
}
@Suppress("BlockingMethodInNonBlockingContext")
/**
* Search for a media(movie or tv show) in tmdb
* @param query The query text
* @param type The media type (movie or tv show)
* @return The media tmdb id, or -1 if not found
*/
suspend fun search(query: String, type: MediaType): Int = withContext(Dispatchers.IO) {
val searchUrl = when (type) {
MediaType.MOVIE -> searchMovieUrl
MediaType.TVSHOW -> searchTVUrl
else -> {
Log.e(javaClass.name, "Wrong Type: $type")
return@withContext -1
private suspend inline fun <reified T> request(
endpoint: String,
parameters: List<Pair<String, Any?>> = emptyList()
): T = coroutineScope {
val path = "$apiUrl$endpoint"
val params = concatenate(
listOf("api_key" to apiKey, "language" to Preferences.preferredLocale.language),
parameters
)
// TODO handle FileNotFoundException
return@coroutineScope (Dispatchers.IO) {
val response: HttpResponse = client.get(path) {
params.forEach {
parameter(it.first, it.second)
}
}
}
val url = URL("$searchUrl$preparedParameters&query=${URLEncoder.encode(query, "UTF-8")}")
val response = JsonParser.parseString(url.readText()).asJsonObject
val sortedResults = response.get("results").asJsonArray.toList().sortedBy {
it.asJsonObject.get("title")?.asString
response.receive<T>()
}
}
/**
* Search for a movie in tmdb
* @param query The query text (movie title)
* @return A TMDBSearch<TMDBSearchResultMovie> object, or
* NoneTMDBSearchMovie if nothing was found
*/
suspend fun searchMovie(query: String): TMDBSearch<TMDBSearchResultMovie> {
val searchEndpoint = "/search/multi"
val parameters = listOf("query" to query, "include_adult" to false)
return try {
request(searchEndpoint, parameters)
}catch (ex: SerializationException) {
Log.e(classTag, "SerializationException in searchMovie(), with query = $query.", ex)
NoneTMDBSearchMovie
}
}
/**
* Search for a tv show in tmdb
* @param query The query text (tv show title)
* @return A TMDBSearch<TMDBSearchResultTVShow> object, or
* NoneTMDBSearchTVShow if nothing was found
*/
suspend fun searchTVShow(query: String): TMDBSearch<TMDBSearchResultTVShow> {
val searchEndpoint = "/search/tv"
val parameters = listOf("query" to query, "include_adult" to false)
return try {
request(searchEndpoint, parameters)
}catch (ex: SerializationException) {
Log.e(classTag, "SerializationException in searchTVShow(), with query = $query.", ex)
NoneTMDBSearchTVShow
}
return@withContext sortedResults.firstOrNull()?.asJsonObject?.get("id")?.asInt ?: -1
}
@Suppress("BlockingMethodInNonBlockingContext")
/**
* Get details for a movie from tmdb
* @param movieId The tmdb ID of the movie
* @return A tmdb movie object, or null if not found
* @return A TMDBMovie object, or NoneTMDBMovie if not found
*/
suspend fun getMovieDetails(movieId: Int): TMDBMovie? = withContext(Dispatchers.IO) {
val url = URL("$detailsMovieUrl/$movieId?api_key=$apiKey&language=$language")
suspend fun getMovieDetails(movieId: Int): TMDBMovie {
val movieEndpoint = "/movie/$movieId"
return@withContext try {
val json = url.readText()
Gson().fromJson(json, TMDBMovie::class.java)
} catch (ex: FileNotFoundException) {
Log.w(javaClass.name, "Waring: The requested media was not found. Requested ID: $movieId", ex)
null
// TODO is FileNotFoundException handling needed?
return try {
request(movieEndpoint)
}catch (ex: SerializationException) {
Log.e(classTag, "SerializationException in getMovieDetails(), with movieId = $movieId.", ex)
NoneTMDBMovie
}
}
@Suppress("BlockingMethodInNonBlockingContext")
/**
* Get details for a tv show from tmdb
* @param tvId The tmdb ID of the tv show
* @return A tmdb tv show object, or null if not found
* @return A TMDBTVShow object, or NoneTMDBTVShow if not found
*/
suspend fun getTVShowDetails(tvId: Int): TMDBTVShow? = withContext(Dispatchers.IO) {
val url = URL("$detailsTVUrl/$tvId?api_key=$apiKey&language=$language")
suspend fun getTVShowDetails(tvId: Int): TMDBTVShow {
val tvShowEndpoint = "/tv/$tvId"
return@withContext try {
val json = url.readText()
Gson().fromJson(json, TMDBTVShow::class.java)
} catch (ex: FileNotFoundException) {
Log.w(javaClass.name, "Waring: The requested media was not found. Requested ID: $tvId", ex)
null
// TODO is FileNotFoundException handling needed?
return try {
request(tvShowEndpoint)
}catch (ex: SerializationException) {
Log.e(classTag, "SerializationException in getTVShowDetails(), with tvId = $tvId.", ex)
NoneTMDBTVShow
}
}
@Suppress("BlockingMethodInNonBlockingContext")
@Suppress("unused")
/**
* Get details for a tv show season from tmdb
* @param tvId The tmdb ID of the tv show
* @param seasonNumber The tmdb season number
* @return A tmdb tv season object, or null if not found
* @return A TMDBTVSeason object, or NoneTMDBTVSeason if not found
*/
suspend fun getTVSeasonDetails(tvId: Int, seasonNumber: Int): TMDBTVSeason? = withContext(Dispatchers.IO) {
val url = URL("$detailsTVUrl/$tvId/season/$seasonNumber?api_key=$apiKey&language=$language")
suspend fun getTVSeasonDetails(tvId: Int, seasonNumber: Int): TMDBTVSeason {
val tvShowSeasonEndpoint = "/tv/$tvId/season/$seasonNumber"
return@withContext try {
val json = url.readText()
Gson().fromJson(json, TMDBTVSeason::class.java)
} catch (ex: FileNotFoundException) {
Log.w(javaClass.name, "Waring: The requested media was not found. Requested ID: $tvId, Season: $seasonNumber", ex)
null
// TODO is FileNotFoundException handling needed?
return try {
request(tvShowSeasonEndpoint)
}catch (ex: SerializationException) {
Log.e(classTag, "SerializationException in getTVSeasonDetails(), with tvId = $tvId, seasonNumber = $seasonNumber.", ex)
NoneTMDBTVSeason
}
}
}
}

View File

@ -1,7 +1,7 @@
/**
* Teapod
*
* Copyright 2020-2021 <seil0@mosad.xyz>
* Copyright 2020-2022 <seil0@mosad.xyz>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
@ -22,71 +22,116 @@
package org.mosad.teapod.util.tmdb
import com.google.gson.annotations.SerializedName
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
/**
* These data classes represent the tmdb api json objects.
* Fields which are nullable in the tmdb api are also nullable here.
*/
abstract class TMDBResult{
abstract val id: Int
abstract val name: String
abstract val overview: String? // for movies tmdb return string or null
abstract val posterPath: String?
abstract val backdropPath: String?
interface TMDBResult {
val id: Int
val name: String
val overview: String? // for movies tmdb return string or null
val posterPath: String?
val backdropPath: String?
}
data class TMDBMovie(
data class TMDBBase(
override val id: Int,
override val name: String,
override val overview: String?,
@SerializedName("poster_path")
override val posterPath: String?,
@SerializedName("backdrop_path")
override val backdropPath: String?,
@SerializedName("release_date")
val releaseDate: String,
@SerializedName("runtime")
val runtime: Int?,
// TODO generes
): TMDBResult()
override val backdropPath: String?
) : TMDBResult
data class TMDBTVShow(
override val id: Int,
override val name: String,
override val overview: String,
@SerializedName("poster_path")
override val posterPath: String?,
@SerializedName("backdrop_path")
override val backdropPath: String?,
@SerializedName("first_air_date")
val firstAirDate: String,
@SerializedName("status")
val status: String,
// TODO generes
): TMDBResult()
/**
* search results for movie and tv show
*/
data class TMDBTVSeason(
val id: Int,
val name: String,
val overview: String,
@SerializedName("poster_path")
val posterPath: String?,
@SerializedName("air_date")
val airDate: String,
@SerializedName("episodes")
val episodes: List<TMDBTVEpisode>,
@SerializedName("season_number")
val seasonNumber: Int
@Serializable
data class TMDBSearch<T>(
val page: Int,
val results: List<T>
)
@Serializable
data class TMDBSearchResultMovie(
@SerialName("id") override val id: Int,
@SerialName("title") override val name: String,
@SerialName("overview") override val overview: String?,
@SerialName("poster_path") override val posterPath: String?,
@SerialName("backdrop_path") override val backdropPath: String?,
) : TMDBResult
@Serializable
data class TMDBSearchResultTVShow(
@SerialName("id") override val id: Int,
@SerialName("name") override val name: String,
@SerialName("overview") override val overview: String?,
@SerialName("poster_path") override val posterPath: String?,
@SerialName("backdrop_path") override val backdropPath: String?,
) : TMDBResult
val NoneTMDBSearch = TMDBSearch<TMDBBase>(0, emptyList())
val NoneTMDBSearchMovie = TMDBSearch<TMDBSearchResultMovie>(0, emptyList())
val NoneTMDBSearchTVShow = TMDBSearch<TMDBSearchResultTVShow>(0, emptyList())
/**
* detail return data types
*/
@Serializable
data class TMDBMovie(
@SerialName("id") override val id: Int,
@SerialName("title") override val name: String, // for movies the name is in the field title
@SerialName("overview") override val overview: String?,
@SerialName("poster_path") override val posterPath: String?,
@SerialName("backdrop_path") override val backdropPath: String?,
@SerialName("release_date") val releaseDate: String,
@SerialName("runtime") val runtime: Int?,
@SerialName("status") val status: String,
// TODO generes
) : TMDBResult
@Serializable
data class TMDBTVShow(
@SerialName("id")override val id: Int,
@SerialName("name")override val name: String,
@SerialName("overview")override val overview: String,
@SerialName("poster_path") override val posterPath: String?,
@SerialName("backdrop_path") override val backdropPath: String?,
@SerialName("first_air_date") val firstAirDate: String,
@SerialName("last_air_date") val lastAirDate: String,
@SerialName("status") val status: String,
// TODO generes
) : TMDBResult
// use null for nullable types, the gui needs to handle/implement a fallback for null values
val NoneTMDB = TMDBBase(0, "", "", null, null)
val NoneTMDBMovie = TMDBMovie(0, "", "", null, null, "1970-01-01", null, "")
val NoneTMDBTVShow = TMDBTVShow(0, "", "", null, null, "1970-01-01", "1970-01-01", "")
@Serializable
data class TMDBTVSeason(
@SerialName("id") val id: Int,
@SerialName("name") val name: String,
@SerialName("overview") val overview: String,
@SerialName("poster_path") val posterPath: String?,
@SerialName("air_date") val airDate: String,
@SerialName("episodes") val episodes: List<TMDBTVEpisode>,
@SerialName("season_number") val seasonNumber: Int
)
@Serializable
data class TMDBTVEpisode(
val id: Int,
val name: String,
val overview: String,
@SerializedName("air_date")
val airDate: String,
@SerializedName("episode_number")
val episodeNumber: Int
)
@SerialName("id") val id: Int,
@SerialName("name") val name: String,
@SerialName("overview") val overview: String,
@SerialName("air_date") val airDate: String,
@SerialName("episode_number") val episodeNumber: Int
)
// use null for nullable types, the gui needs to handle/implement a fallback for null values
val NoneTMDBTVSeason = TMDBTVSeason(0, "", "", null, "", emptyList(), 0)

View File

@ -0,0 +1,5 @@
<vector android:height="24dp" android:tint="#FFFFFF"
android:viewportHeight="24" android:viewportWidth="24"
android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
<path android:fillColor="@android:color/white" android:pathData="M7,10l5,5 5,-5z"/>
</vector>

View File

@ -0,0 +1,5 @@
<vector android:height="24dp" android:tint="#FFFFFF"
android:viewportHeight="24" android:viewportWidth="24"
android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
<path android:fillColor="@android:color/white" android:pathData="M11.99,2C6.47,2 2,6.48 2,12s4.47,10 9.99,10C17.52,22 22,17.52 22,12S17.52,2 11.99,2zM18.92,8h-2.95c-0.32,-1.25 -0.78,-2.45 -1.38,-3.56 1.84,0.63 3.37,1.91 4.33,3.56zM12,4.04c0.83,1.2 1.48,2.53 1.91,3.96h-3.82c0.43,-1.43 1.08,-2.76 1.91,-3.96zM4.26,14C4.1,13.36 4,12.69 4,12s0.1,-1.36 0.26,-2h3.38c-0.08,0.66 -0.14,1.32 -0.14,2 0,0.68 0.06,1.34 0.14,2L4.26,14zM5.08,16h2.95c0.32,1.25 0.78,2.45 1.38,3.56 -1.84,-0.63 -3.37,-1.9 -4.33,-3.56zM8.03,8L5.08,8c0.96,-1.66 2.49,-2.93 4.33,-3.56C8.81,5.55 8.35,6.75 8.03,8zM12,19.96c-0.83,-1.2 -1.48,-2.53 -1.91,-3.96h3.82c-0.43,1.43 -1.08,2.76 -1.91,3.96zM14.34,14L9.66,14c-0.09,-0.66 -0.16,-1.32 -0.16,-2 0,-0.68 0.07,-1.35 0.16,-2h4.68c0.09,0.65 0.16,1.32 0.16,2 0,0.68 -0.07,1.34 -0.16,2zM14.59,19.56c0.6,-1.11 1.06,-2.31 1.38,-3.56h2.95c-0.96,1.65 -2.49,2.93 -4.33,3.56zM16.36,14c0.08,-0.66 0.14,-1.32 0.14,-2 0,-0.68 -0.06,-1.34 -0.14,-2h3.38c0.16,0.64 0.26,1.31 0.26,2s-0.1,1.36 -0.26,2h-3.38z"/>
</vector>

View File

@ -146,6 +146,46 @@
android:textSize="16sp"
android:textStyle="bold" />
<LinearLayout
android:id="@+id/linear_settings_content_language"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:padding="7dp">
<ImageView
android:id="@+id/imageView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="@string/settings_content_language"
android:minWidth="48dp"
android:minHeight="48dp"
android:padding="9dp"
android:scaleType="fitXY"
android:src="@drawable/ic_baseline_language_24"
app:tint="?iconColor" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/text_settings_content_language"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/settings_content_language"
android:textSize="16sp" />
<TextView
android:id="@+id/text_settings_content_language_desc"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/settings_content_language_desc"
android:textColor="?textSecondary" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:id="@+id/linear_settings_secondary"
android:layout_width="match_parent"
@ -158,7 +198,7 @@
android:id="@+id/imageView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="@string/settings_secondary"
android:contentDescription="@string/settings_prefer_subbed"
android:minWidth="48dp"
android:minHeight="48dp"
android:padding="9dp"
@ -185,7 +225,7 @@
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/settings_secondary"
android:text="@string/settings_prefer_subbed"
android:textSize="16sp" />
<TextView
@ -194,7 +234,7 @@
android:layout_height="wrap_content"
android:layout_weight="1"
android:maxLines="2"
android:text="@string/settings_secondary_desc"
android:text="@string/settings_prefer_subbed_desc"
android:textColor="?textSecondary" />
</LinearLayout>

View File

@ -108,35 +108,7 @@
</LinearLayout>
<LinearLayout
android:id="@+id/linear_my_list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="7dp">
<TextView
android:id="@+id/text_my_list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingStart="10dp"
android:paddingTop="15dp"
android:paddingEnd="5dp"
android:paddingBottom="5dp"
android:text="@string/my_list"
android:textSize="16sp"
android:textStyle="bold" />
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recycler_my_list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
tools:listitem="@layout/item_media" />
</LinearLayout>
<LinearLayout
android:id="@+id/linear_new_episodes"
android:id="@+id/linear_up_next"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
@ -150,7 +122,7 @@
android:paddingTop="15dp"
android:paddingEnd="5dp"
android:paddingBottom="5dp"
android:text="@string/new_episodes"
android:text="@string/up_next"
android:textSize="16sp"
android:textStyle="bold" />
@ -164,26 +136,26 @@
</LinearLayout>
<LinearLayout
android:id="@+id/linear_new_simulcasts"
android:id="@+id/linear_watchlist"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="7dp">
<TextView
android:id="@+id/text_new_simulcasts"
android:id="@+id/text_watchlist"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingStart="10dp"
android:paddingTop="15dp"
android:paddingEnd="5dp"
android:paddingBottom="5dp"
android:text="@string/new_simulcasts"
android:text="@string/my_list"
android:textSize="16sp"
android:textStyle="bold" />
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recycler_new_simulcasts"
android:id="@+id/recycler_watchlist"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"

View File

@ -111,6 +111,7 @@
android:layout_marginTop="12dp"
android:layout_marginEnd="7dp"
android:text="@string/text_title_ex"
android:textSize="16sp"
android:textStyle="bold" />
<TextView
@ -128,7 +129,7 @@
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginStart="12dp"
android:layout_marginTop="7dp"
android:layout_marginTop="5dp"
android:layout_marginEnd="12dp"
android:orientation="horizontal">
@ -136,15 +137,19 @@
android:id="@+id/linear_my_list_action"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:foreground="?android:selectableItemBackground"
android:gravity="center_horizontal"
android:orientation="vertical">
<ImageView
android:id="@+id/image_my_list_action"
android:layout_width="36dp"
android:layout_height="36dp"
android:layout_width="48dp"
android:layout_height="48dp"
android:contentDescription="@string/my_list"
android:padding="5dp"
android:paddingStart="11dp"
android:paddingTop="11dp"
android:paddingEnd="11dp"
android:paddingBottom="7dp"
android:src="@drawable/ic_baseline_add_24"
app:tint="?buttonBackground" />
@ -164,7 +169,7 @@
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="7dp"
android:layout_marginTop="12dp"
android:layout_marginTop="7dp"
android:layout_marginEnd="7dp"
android:background="@android:color/transparent"
app:tabGravity="start"

View File

@ -1,10 +1,24 @@
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:id="@+id/button_season_selection"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="7dp"
android:layout_marginTop="6dp"
android:layout_marginEnd="7dp"
android:layout_marginBottom="6dp"
android:singleLine="true"
android:text="@string/text_title_ex"
app:icon="@drawable/ic_baseline_arrow_drop_down_24"
app:iconGravity="end" />
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recycler_episodes"
@ -16,4 +30,4 @@
tools:layout_editor_absoluteY="298dp"
tools:listitem="@layout/item_episode" />
</FrameLayout>
</LinearLayout>

View File

@ -65,6 +65,7 @@
android:layout_margin="7dp"
android:ems="10"
android:hint="@string/password"
android:imeOptions="actionDone"
android:importantForAutofill="no"
android:inputType="textPassword" />

View File

@ -38,7 +38,7 @@
android:id="@+id/text_app_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/app_name"
android:text="@string/on_welcome_heading"
android:textAlignment="center"
android:textSize="26sp"
android:textStyle="bold" />

View File

@ -125,7 +125,7 @@
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginEnd="7dp"
android:text="@string/language"
android:text="@string/subtitles"
android:textAllCaps="false"
app:icon="@drawable/ic_baseline_subtitles_24"
app:layout_constraintBottom_toBottomOf="parent"

View File

@ -35,7 +35,7 @@
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginEnd="44dp"
android:text="@string/language"
android:text="@string/subtitles"
android:textAlignment="center"
android:textColor="@color/exo_white"
android:textSize="16sp"

View File

@ -7,6 +7,7 @@
<!-- home fragment -->
<string name="highlight_media">Highlight</string>
<string name="up_next">Weiterschauen</string>
<string name="my_list">Meine Liste</string>
<string name="new_episodes">Neue Episoden</string>
<string name="new_simulcasts">Neue Simulcasts</string>
@ -38,8 +39,11 @@
<string name="info">Info</string>
<string name="info_about_desc">Version %1$s (%2$s)</string>
<string name="settings">Einstellungen</string>
<string name="settings_secondary">Bevorzuge Japanisch (OmU)</string>
<string name="settings_secondary_desc">Japanisch verwenden, sofern vorhanden</string>
<string name="settings_content_language">Bevorzuge Inhaltssprache</string>
<string name="settings_content_language_desc">Englisch</string>
<string name="settings_content_language_none">Keine</string>
<string name="settings_prefer_subbed">Bevorzuge OmU</string>
<string name="settings_prefer_subbed_desc">Original Sprache verwenden, sofern vorhanden</string>
<string name="settings_autoplay">Autoplay</string>
<string name="settings_autoplay_desc">Nächste Episode automatisch abspielen</string>
<string name="theme">Design</string>
@ -57,7 +61,7 @@
<string name="authors">Autor</string>
<string name="source">Quellcode</string>
<string name="license">Lizenz</string>
<string name="about_info">Eine inoffizielle App für Anime on Demand.</string>
<string name="about_info">Eine inoffizielle App für Crunchyroll.</string>
<string name="third_party_heading">Lizenzen von Drittanbietern</string>
<string name="third_party_component_desc">© %1$s %2$s unter %3$s</string>
<string name="dev_settings_enabled">Du bist jetzt ein Entwickler</string>
@ -71,17 +75,20 @@
<string name="next_episode">Nächste Folge</string>
<string name="skip_opening">Intro überspringen</string>
<string name="language">Sprache</string>
<string name="subtitles">Untertitel</string>
<string name="episodes">Folgen</string>
<string name="episode">Folge</string>
<string name="no_subtitles">Aus</string>
<!-- Onboarding -->
<string name="skip">Überspringen</string>
<string name="next">Weiter</string>
<string name="start">Fertig</string>
<string name="on_welcome">Willkommen!\nTeapod ist eine inoffizielle App für AoD.</string>
<string name="on_welcome_heading">Willkommen</string>
<string name="on_welcome">Teapod ist eine inoffizielle App für Crunchyroll, die unter den Bedingungen der GPL 3 lizenziert ist.\n\nHinweis: Die Benutzung von Teapod kann gegen die Nutzungsbedingungen von Crunchyroll verstoßen.</string>
<string name="on_get_started">Los geht\'s</string>
<string name="on_login_heading">Login</string>
<string name="on_login_desc">Um Teapod verwenden zu können musst du dich mit deinem AoD Account anmelden. Deine Login-Daten werden verschlüsselt auf deinem Gerät gespeichert.</string>
<string name="on_login_desc">Um Teapod verwenden zu können musst du dich mit deinem Crunchyroll Account anmelden. Deine Login-Daten werden verschlüsselt auf deinem Gerät gespeichert.</string>
<string name="on_login_failed">Login nicht erfolgreich! Stelle sicher das deine Login-Daten korrekt sind und versuche es erneut.</string>
<!-- dialogs -->

View File

@ -7,6 +7,7 @@
<!-- home fragment -->
<string name="highlight_media">Highlight</string>
<string name="up_next">Up next</string>
<string name="my_list">My list</string>
<string name="new_episodes">New episodes</string>
<string name="new_simulcasts">New simulcasts</string>
@ -33,24 +34,25 @@
<item quantity="one">%d Minute</item>
<item quantity="other">%d Minutes</item>
</plurals>
<string name="season_number_title" translatable="false">S%1$d - %2$s</string>
<string name="similar_titles">Similar titles</string>
<string name="component_episode_title">Ep. %1$s %2$s</string>
<string name="component_episode_title_sub">Ep. %1$s %2$s (Sub)</string>
<string name="component_poster_desc" translatable="false">episode poster</string>
<string name="component_watched_desc" translatable="false">already watched</string>
<!-- settings fragment -->
<!-- account fragment -->
<string name="account">Account</string>
<string name="account_login_ex" translatable="false">user@example.com</string>
<string name="account_login_desc">Tap to edit</string>
<string name="account_subscription">Subscription %1$s</string>
<string name="account_subscription_desc">Tap to extend</string>
<string name="info">Info</string>
<string name="info_about" translatable="false">Teapod by @Seil0</string>
<string name="info_about_desc">Version %1$s (%2$s)</string>
<string name="settings">Settings</string>
<string name="settings_secondary">Prefer japanese (sub)</string>
<string name="settings_secondary_desc">Use the japanese, if present</string>
<string name="settings_content_language">Preferred content language</string>
<string name="settings_content_language_desc">English</string>
<string name="settings_content_language_none">None</string>
<string name="settings_prefer_subbed">Prefer subbed</string>
<string name="settings_prefer_subbed_desc">Use original language, if present</string>
<string name="settings_autoplay">Autoplay</string>
<string name="settings_autoplay_desc">Play next episode automatically</string>
<string name="theme">Theme</string>
@ -62,7 +64,9 @@
<string name="import_data">import data</string>
<string name="import_data_desc">import "My list" from a file</string>
<string name="import_data_success">imported "My list" successfully</string>
<string name="info">Info</string>
<string name="info_about" translatable="false">Teapod by @Seil0</string>
<string name="info_about_desc">Version %1$s (%2$s)</string>
<!-- about fragment -->
<string name="version">Version</string>
@ -73,7 +77,7 @@
<string name="teapod_repo" translatable="false">git.mosad.xyz/Seil0/teapod</string>
<string name="license">License</string>
<string name="license_desc" translatable="false">GNU General Public License 3</string>
<string name="about_info">An unofficial app for anime on demand.</string>
<string name="about_info">An unofficial app for Crunchyroll.</string>
<string name="tmdb_notice" translatable="false">This product uses the TMDb API but is not endorsed or certified by TMDb.</string>
<string name="third_party_heading">Third Party Licenses</string>
<string name="third_party_component_desc">© %1$s %2$s under %3$s</string>
@ -92,17 +96,20 @@
<string name="time_min_sec" translatable="false">%1$02d:%2$02d</string>
<string name="time_hour_min_sec" translatable="false">%1$d:%2$02d:%3$02d</string>
<string name="language">Language</string>
<string name="subtitles">Subtitles</string>
<string name="episodes">Episodes</string>
<string name="episode">Episode</string>
<string name="no_subtitles">None</string>
<!-- Onboarding -->
<string name="skip">Skip</string>
<string name="next">Next</string>
<string name="start">Start</string>
<string name="on_welcome">Welcome!\nTeapod is an unofficial App for AoD.</string>
<string name="on_welcome_heading">Welcome</string>
<string name="on_welcome">Teapod is an unofficial app for Crunchyroll, licensed under the terms and conditions of GPL 3.\n\nPlease note: Using Teapod may violate the ToS of Crunchyroll.</string>
<string name="on_get_started">Get started</string>
<string name="on_login_heading">Login</string>
<string name="on_login_desc">To use Teapod you need to log in with your AoD account. Your Login-Data will be stored encrypted on your device.</string>
<string name="on_login_desc">To use Teapod you have to log in with your Crunchyroll account. Your login data will be stored encrypted on your device.</string>
<string name="on_login_failed">Could not login! Make sure Username and Password are correct and try again.</string>
<!-- dialogs -->
@ -124,12 +131,16 @@
<string name="preference_file_key" translatable="false">org.mosad.teapod.preferences</string>
<string name="save_key_user_login" translatable="false">org.mosad.teapod.user_login</string>
<string name="save_key_user_password" translatable="false">org.mosad.teapod.user_password</string>
<!-- for legacy reasons the prefer subbed key is called prefer_secondary-->
<string name="save_key_prefer_secondary" translatable="false">org.mosad.teapod.prefer_secondary</string>
<string name="save_key_preferred_local" translatable="false">org.mosad.teapod.preferred_local</string>
<string name="save_key_autoplay" translatable="false">org.mosad.teapod.autoplay</string>
<string name="save_key_dev_settings" translatable="false">org.mosad.teapod.dev.settings</string>
<string name="save_key_theme" translatable="false">org.mosad.teapod.theme</string>
<!-- intents & states -->
<string name="intent_media_id" translatable="false">intent_media_id</string>
<string name="intent_season_id" translatable="false">intent_season_id</string>
<string name="intent_episode_id" translatable="false">intent_episode_id</string>
</resources>

View File

@ -4,6 +4,7 @@
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
<item name="popupMenuStyle">@style/Widget.App.PopupMenu</item>
</style>
<style name="AppTheme.Light" parent="AppTheme">
@ -35,15 +36,30 @@
<item name="shapeTextBackground">@color/textBackgroundDark</item>
<item name="iconColor">@color/iconColorDark</item>
<item name="buttonBackground">@color/buttonBackgroundDark</item>
<item name="md_background_color">@color/themeSecondaryDark</item>
<item name="md_color_content">@color/textSecondaryDark</item>
<!-- without this, the unchecked single choice buttons while be black -->
<item name="md_color_widget_unchecked">@color/textSecondaryDark</item>
<item name="materialAlertDialogTheme">@style/ThemeOverlay.App.MaterialAlertDialog.Dark</item>
<!-- change on click indicator color for manually set components -->
<item name="colorControlHighlight">@color/controlHighlightDark</item>
</style>
<!-- dialog themes -->
<style name="ThemeOverlay.App.MaterialAlertDialog.Dark" parent="ThemeOverlay.MaterialComponents.MaterialAlertDialog">
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorSurface">@color/themeSecondaryDark</item>
<item name="colorOnSurface">@color/textPrimaryDark</item>
<item name="android:colorControlNormal">@color/textSecondaryDark</item> <!-- Radio button unchecked-->
<item name="materialAlertDialogTitleTextStyle">@style/MaterialAlertDialog.App.Title.Text</item>
</style>
<style name="MaterialAlertDialog.App.Title.Text" parent="MaterialAlertDialog.MaterialComponents.Title.Text">
<item name="android:textColor">?textPrimary</item>
</style>
<!-- player theme -->
<style name="PlayerTheme" parent="Theme.MaterialComponents.Light.NoActionBar">
<item name="android:windowNoTitle">true</item>
@ -65,4 +81,9 @@
<item name="cornerSize">5dp</item>
</style>
<!-- popup menus -->
<style name="Widget.App.PopupMenu" parent="Widget.MaterialComponents.PopupMenu">
<item name="android:popupBackground">?themeSecondary</item>
</style>
</resources>

View File

@ -1,6 +1,7 @@
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
ext.kotlin_version = "1.6.10"
ext.ktor_version = "1.6.7"
repositories {
google()
mavenCentral()