Unprivated function to allow nginx plugin to use it

This commit is contained in:
Blatzar 2022-08-17 18:14:36 +02:00
parent 4546370dc4
commit 8b321bbd5e
5 changed files with 184 additions and 230 deletions

View file

@ -1,7 +1,9 @@
package com.lagradost.cloudstream3 package com.lagradost.cloudstream3
import android.app.Activity
import android.app.Application import android.app.Application
import android.content.Context import android.content.Context
import android.content.ContextWrapper
import android.widget.Toast import android.widget.Toast
import androidx.fragment.app.Fragment import androidx.fragment.app.Fragment
import com.google.auto.service.AutoService import com.google.auto.service.AutoService
@ -89,6 +91,10 @@ class AcraApplication : Application() {
} }
companion object { companion object {
/** Use to get activity from Context */
tailrec fun Context.getActivity(): Activity? = this as? Activity
?: (this as? ContextWrapper)?.baseContext?.getActivity()
private var _context: WeakReference<Context>? = null private var _context: WeakReference<Context>? = null
var context var context
get() = _context?.get() get() = _context?.get()

View file

@ -12,7 +12,6 @@ abstract class AccountManager(private val defIndex: Int) : AuthAPI {
val aniListApi = AniListApi(0) val aniListApi = AniListApi(0)
val openSubtitlesApi = OpenSubtitlesApi(0) val openSubtitlesApi = OpenSubtitlesApi(0)
val indexSubtitlesApi = IndexSubtitleApi() val indexSubtitlesApi = IndexSubtitleApi()
//val nginxApi = NginxApi(0) // TODO: fix
// used to login via app intent // used to login via app intent
val OAuth2Apis val OAuth2Apis

View file

@ -1,60 +0,0 @@
// package com.lagradost.cloudstream3.syncproviders.providers
// import com.lagradost.cloudstream3.AcraApplication.Companion.getKey
// import com.lagradost.cloudstream3.AcraApplication.Companion.setKey
// import com.lagradost.cloudstream3.R
// import com.lagradost.cloudstream3.movieproviders.NginxProvider
// import com.lagradost.cloudstream3.syncproviders.AuthAPI
// import com.lagradost.cloudstream3.syncproviders.InAppAuthAPI
// import com.lagradost.cloudstream3.syncproviders.InAppAuthAPIManager
// class NginxApi(index: Int) : InAppAuthAPIManager(index) {
// override val name = "Nginx"
// override val idPrefix = "nginx"
// override val icon = R.drawable.nginx
// override val requiresUsername = true
// override val requiresPassword = true
// override val requiresServer = true
// override val createAccountUrl = "https://www.sarlays.com/use-nginx-with-cloudstream/"
// companion object {
// const val NGINX_USER_KEY: String = "nginx_user"
// }
// override fun getLatestLoginData(): InAppAuthAPI.LoginData? {
// return getKey(accountId, NGINX_USER_KEY)
// }
// override fun loginInfo(): AuthAPI.LoginInfo? {
// val data = getLatestLoginData() ?: return null
// return AuthAPI.LoginInfo(name = data.username ?: data.server, accountIndex = accountIndex)
// }
// override suspend fun login(data: InAppAuthAPI.LoginData): Boolean {
// if (data.server.isNullOrBlank()) return false // we require a server
// switchToNewAccount()
// setKey(accountId, NGINX_USER_KEY, data)
// registerAccount()
// initialize()
// return true
// }
// override fun logOut() {
// removeAccountKeys()
// initializeData()
// }
// private fun initializeData() {
// val data = getLatestLoginData() ?: run {
// NginxProvider.overrideUrl = null
// NginxProvider.loginCredentials = null
// return
// }
// NginxProvider.overrideUrl = data.server?.removeSuffix("/")
// NginxProvider.loginCredentials = "${data.username ?: ""}:${data.password ?: ""}"
// }
// override suspend fun initialize() {
// initializeData()
// }
// }

View file

@ -1,6 +1,6 @@
package com.lagradost.cloudstream3.ui.settings package com.lagradost.cloudstream3.ui.settings
import android.content.Context import android.app.Activity
import android.content.Intent import android.content.Intent
import android.net.Uri import android.net.Uri
import android.os.Bundle import android.os.Bundle
@ -36,14 +36,11 @@ import kotlinx.android.synthetic.main.account_switch.*
import kotlinx.android.synthetic.main.add_account_input.* import kotlinx.android.synthetic.main.add_account_input.*
class SettingsAccount : PreferenceFragmentCompat() { class SettingsAccount : PreferenceFragmentCompat() {
override fun onViewCreated(view: View, savedInstanceState: Bundle?) { companion object {
super.onViewCreated(view, savedInstanceState) /** Used by nginx plugin too */
setUpToolbar(R.string.category_account) fun showLoginInfo(activity: Activity?, api: AccountManager, info: AuthAPI.LoginInfo) {
}
private fun showLoginInfo(api: AccountManager, info: AuthAPI.LoginInfo) {
val builder = val builder =
AlertDialog.Builder(context ?: return, R.style.AlertDialogCustom) AlertDialog.Builder(activity ?: return, R.style.AlertDialogCustom)
.setView(R.layout.account_managment) .setView(R.layout.account_managment)
val dialog = builder.show() val dialog = builder.show()
@ -55,19 +52,52 @@ class SettingsAccount : PreferenceFragmentCompat() {
dialog.dismissSafe(activity) dialog.dismissSafe(activity)
} }
(info.name ?: context?.getString(R.string.no_data))?.let { (info.name ?: activity.getString(R.string.no_data)).let {
dialog.findViewById<TextView>(R.id.account_name)?.text = it dialog.findViewById<TextView>(R.id.account_name)?.text = it
} }
dialog.account_site?.text = api.name dialog.account_site?.text = api.name
dialog.account_switch_account?.setOnClickListener { dialog.account_switch_account?.setOnClickListener {
dialog.dismissSafe(activity) dialog.dismissSafe(activity)
showAccountSwitch(it.context, api) showAccountSwitch(activity, api)
} }
} }
private fun showAccountSwitch(activity: Activity, api: AccountManager) {
val accounts = api.getAccounts() ?: return
val builder =
AlertDialog.Builder(activity, R.style.AlertDialogCustom)
.setView(R.layout.account_switch)
val dialog = builder.show()
dialog.account_add?.setOnClickListener {
addAccount(activity, api)
dialog?.dismissSafe(activity)
}
val ogIndex = api.accountIndex
val items = ArrayList<AuthAPI.LoginInfo>()
for (index in accounts) {
api.accountIndex = index
val accountInfo = api.loginInfo()
if (accountInfo != null) {
items.add(accountInfo)
}
}
api.accountIndex = ogIndex
val adapter = AccountAdapter(items, R.layout.account_single) {
dialog?.dismissSafe(activity)
api.changeAccount(it.card.accountIndex)
}
val list = dialog.findViewById<RecyclerView>(R.id.account_list)
list?.adapter = adapter
}
@UiThread @UiThread
private fun addAccount(api: AccountManager) { private fun addAccount(activity: Activity?, api: AccountManager) {
try { try {
when (api) { when (api) {
is OAuth2API -> { is OAuth2API -> {
@ -75,7 +105,7 @@ class SettingsAccount : PreferenceFragmentCompat() {
} }
is InAppAuthAPI -> { is InAppAuthAPI -> {
val builder = val builder =
AlertDialog.Builder(context ?: return, R.style.AlertDialogCustom) AlertDialog.Builder(activity ?: return, R.style.AlertDialogCustom)
.setView(R.layout.add_account_input) .setView(R.layout.add_account_input)
val dialog = builder.show() val dialog = builder.show()
@ -86,7 +116,7 @@ class SettingsAccount : PreferenceFragmentCompat() {
dialog.login_username_input to api.requiresUsername dialog.login_username_input to api.requiresUsername
) )
if (context?.isTvSettings() == true) { if (activity.isTvSettings()) {
visibilityMap.forEach { (input, isVisible) -> visibilityMap.forEach { (input, isVisible) ->
input.isVisible = isVisible input.isVisible = isVisible
@ -94,7 +124,9 @@ class SettingsAccount : PreferenceFragmentCompat() {
input.setOnEditorActionListener { textView, actionId, _ -> input.setOnEditorActionListener { textView, actionId, _ ->
if (actionId == EditorInfo.IME_ACTION_NEXT) { if (actionId == EditorInfo.IME_ACTION_NEXT) {
val view = textView.focusSearch(FOCUS_DOWN) val view = textView.focusSearch(FOCUS_DOWN)
return@setOnEditorActionListener view?.requestFocus(FOCUS_DOWN) == true return@setOnEditorActionListener view?.requestFocus(
FOCUS_DOWN
) == true
} }
return@setOnEditorActionListener true return@setOnEditorActionListener true
} }
@ -114,7 +146,7 @@ class SettingsAccount : PreferenceFragmentCompat() {
val i = Intent(Intent.ACTION_VIEW) val i = Intent(Intent.ACTION_VIEW)
i.data = Uri.parse(api.createAccountUrl) i.data = Uri.parse(api.createAccountUrl)
try { try {
startActivity(i) activity.startActivity(i)
} catch (e: Exception) { } catch (e: Exception) {
logError(e) logError(e)
} }
@ -144,11 +176,11 @@ class SettingsAccount : PreferenceFragmentCompat() {
logError(e) logError(e)
false false
} }
activity?.runOnUiThread { activity.runOnUiThread {
try { try {
showToast( showToast(
activity, activity,
getString(if (isSuccessful) R.string.authenticated_user else R.string.authenticated_user_fail).format( activity.getString(if (isSuccessful) R.string.authenticated_user else R.string.authenticated_user_fail).format(
api.name api.name
) )
) )
@ -171,50 +203,22 @@ class SettingsAccount : PreferenceFragmentCompat() {
logError(e) logError(e)
} }
} }
private fun showAccountSwitch(context: Context, api: AccountManager) {
val accounts = api.getAccounts() ?: return
val builder =
AlertDialog.Builder(context, R.style.AlertDialogCustom).setView(R.layout.account_switch)
val dialog = builder.show()
dialog.account_add?.setOnClickListener {
addAccount(api)
dialog?.dismissSafe(activity)
} }
val ogIndex = api.accountIndex override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
val items = ArrayList<AuthAPI.LoginInfo>() setUpToolbar(R.string.category_account)
for (index in accounts) {
api.accountIndex = index
val accountInfo = api.loginInfo()
if (accountInfo != null) {
items.add(accountInfo)
}
}
api.accountIndex = ogIndex
val adapter = AccountAdapter(items, R.layout.account_single) {
dialog?.dismissSafe(activity)
api.changeAccount(it.card.accountIndex)
}
val list = dialog.findViewById<RecyclerView>(R.id.account_list)
list?.adapter = adapter
} }
override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) { override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
hideKeyboard() hideKeyboard()
setPreferencesFromResource(R.xml.settings_account, rootKey) setPreferencesFromResource(R.xml.settings_account, rootKey)
// TODO: fix
val syncApis = val syncApis =
listOf( listOf(
R.string.mal_key to malApi, R.string.mal_key to malApi,
R.string.anilist_key to aniListApi, R.string.anilist_key to aniListApi,
R.string.opensubtitles_key to openSubtitlesApi, R.string.opensubtitles_key to openSubtitlesApi,
//R.string.nginx_key to nginxApi,
) )
for ((key, api) in syncApis) { for ((key, api) in syncApis) {
@ -224,9 +228,9 @@ class SettingsAccount : PreferenceFragmentCompat() {
setOnPreferenceClickListener { setOnPreferenceClickListener {
val info = api.loginInfo() val info = api.loginInfo()
if (info != null) { if (info != null) {
showLoginInfo(api, info) showLoginInfo(activity, api, info)
} else { } else {
addAccount(api) addAccount(activity, api)
} }
return@setOnPreferenceClickListener true return@setOnPreferenceClickListener true
} }

View file

@ -101,7 +101,12 @@ class PluginAdapter(
try { try {
plugin.openSettings!!.invoke(itemView.context) plugin.openSettings!!.invoke(itemView.context)
} catch (e: Throwable) { } catch (e: Throwable) {
Log.e("PluginAdapter", "Failed to open ${metadata.name} settings: ${Log.getStackTraceString(e)}") Log.e(
"PluginAdapter",
"Failed to open ${metadata.name} settings: ${
Log.getStackTraceString(e)
}"
)
} }
} }