2023-10-30 23:32:01 +00:00
|
|
|
package com.lagradost.cloudstream3.ui.account
|
|
|
|
|
2023-11-11 22:47:23 +00:00
|
|
|
import android.annotation.SuppressLint
|
2023-10-30 23:32:01 +00:00
|
|
|
import android.content.Intent
|
|
|
|
import android.os.Bundle
|
2024-03-08 00:45:20 +00:00
|
|
|
import android.util.Log
|
|
|
|
import android.widget.Toast
|
2023-10-30 23:32:01 +00:00
|
|
|
import androidx.appcompat.app.AppCompatActivity
|
2023-11-11 22:47:23 +00:00
|
|
|
import androidx.lifecycle.ViewModelProvider
|
|
|
|
import androidx.preference.PreferenceManager
|
2023-10-30 23:32:01 +00:00
|
|
|
import androidx.recyclerview.widget.GridLayoutManager
|
|
|
|
import com.lagradost.cloudstream3.CommonActivity
|
|
|
|
import com.lagradost.cloudstream3.CommonActivity.loadThemes
|
2023-11-11 22:47:23 +00:00
|
|
|
import com.lagradost.cloudstream3.CommonActivity.showToast
|
2023-10-30 23:32:01 +00:00
|
|
|
import com.lagradost.cloudstream3.MainActivity
|
|
|
|
import com.lagradost.cloudstream3.R
|
|
|
|
import com.lagradost.cloudstream3.databinding.ActivityAccountSelectBinding
|
2023-11-11 22:47:23 +00:00
|
|
|
import com.lagradost.cloudstream3.mvvm.observe
|
|
|
|
import com.lagradost.cloudstream3.ui.AutofitRecyclerView
|
|
|
|
import com.lagradost.cloudstream3.ui.account.AccountAdapter.Companion.VIEW_TYPE_EDIT_ACCOUNT
|
|
|
|
import com.lagradost.cloudstream3.ui.account.AccountAdapter.Companion.VIEW_TYPE_SELECT_ACCOUNT
|
2024-03-08 00:45:20 +00:00
|
|
|
import com.lagradost.cloudstream3.ui.settings.SettingsFragment.Companion.isTruePhone
|
2023-10-30 23:32:01 +00:00
|
|
|
import com.lagradost.cloudstream3.ui.settings.SettingsFragment.Companion.isTvSettings
|
2024-03-08 00:45:20 +00:00
|
|
|
import com.lagradost.cloudstream3.utils.BiometricAuthenticator
|
|
|
|
import com.lagradost.cloudstream3.utils.BiometricAuthenticator.deviceHasPasswordPinLock
|
|
|
|
import com.lagradost.cloudstream3.utils.BiometricAuthenticator.startBiometricAuthentication
|
2023-11-11 22:47:23 +00:00
|
|
|
import com.lagradost.cloudstream3.utils.DataStoreHelper.accounts
|
|
|
|
import com.lagradost.cloudstream3.utils.DataStoreHelper.selectedKeyIndex
|
|
|
|
import com.lagradost.cloudstream3.utils.DataStoreHelper.setAccount
|
2023-10-30 23:32:01 +00:00
|
|
|
import com.lagradost.cloudstream3.utils.UIHelper.colorFromAttribute
|
|
|
|
|
2024-03-08 00:45:20 +00:00
|
|
|
class AccountSelectActivity : AppCompatActivity(), BiometricAuthenticator.BiometricAuthCallback {
|
2023-10-30 23:32:01 +00:00
|
|
|
|
2023-11-11 22:47:23 +00:00
|
|
|
lateinit var viewModel: AccountViewModel
|
|
|
|
|
|
|
|
@SuppressLint("NotifyDataSetChanged")
|
2023-10-30 23:32:01 +00:00
|
|
|
override fun onCreate(savedInstanceState: Bundle?) {
|
|
|
|
super.onCreate(savedInstanceState)
|
2023-11-11 22:47:23 +00:00
|
|
|
loadThemes(this)
|
|
|
|
|
|
|
|
window.navigationBarColor = colorFromAttribute(R.attr.primaryBlackBackground)
|
|
|
|
|
|
|
|
// Are we editing and coming from MainActivity?
|
|
|
|
val isEditingFromMainActivity = intent.getBooleanExtra(
|
|
|
|
"isEditingFromMainActivity",
|
|
|
|
false
|
|
|
|
)
|
|
|
|
|
|
|
|
val settingsManager = PreferenceManager.getDefaultSharedPreferences(this)
|
2024-03-08 00:45:20 +00:00
|
|
|
val authEnabled = settingsManager.getBoolean(getString(R.string.biometric_key), false)
|
|
|
|
val skipStartup = settingsManager.getBoolean(getString(R.string.skip_startup_account_select_key), false
|
2023-11-11 22:47:23 +00:00
|
|
|
) || accounts.count() <= 1
|
|
|
|
|
|
|
|
viewModel = ViewModelProvider(this)[AccountViewModel::class.java]
|
|
|
|
|
2024-03-08 00:45:20 +00:00
|
|
|
fun askBiometricAuth() {
|
|
|
|
|
|
|
|
if (isTruePhone() && authEnabled) {
|
|
|
|
if (deviceHasPasswordPinLock(this)) {
|
|
|
|
startBiometricAuthentication(this, R.string.biometric_authentication_title, false)
|
|
|
|
|
|
|
|
BiometricAuthenticator.promptInfo?.let {
|
|
|
|
BiometricAuthenticator.biometricPrompt?.authenticate(it)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
showToast(R.string.phone_not_secured, Toast.LENGTH_LONG)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-30 23:34:01 +00:00
|
|
|
// Don't show account selection if there is only
|
|
|
|
// one account that exists
|
2023-11-11 22:47:23 +00:00
|
|
|
if (!isEditingFromMainActivity && skipStartup) {
|
|
|
|
val currentAccount = accounts.firstOrNull { it.keyIndex == selectedKeyIndex }
|
|
|
|
if (currentAccount?.lockPin != null) {
|
|
|
|
CommonActivity.init(this)
|
|
|
|
viewModel.handleAccountSelect(currentAccount, this, true)
|
|
|
|
observe(viewModel.isAllowedLogin) { isAllowedLogin ->
|
|
|
|
if (isAllowedLogin) {
|
|
|
|
// We are allowed to continue to MainActivity
|
|
|
|
navigateToMainActivity()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (accounts.count() > 1) {
|
|
|
|
showToast(this, getString(
|
|
|
|
R.string.logged_account,
|
|
|
|
currentAccount?.name
|
|
|
|
))
|
|
|
|
}
|
|
|
|
|
|
|
|
navigateToMainActivity()
|
|
|
|
}
|
|
|
|
|
2023-10-30 23:34:01 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-10-30 23:32:01 +00:00
|
|
|
CommonActivity.init(this)
|
|
|
|
|
2023-11-02 20:14:16 +00:00
|
|
|
val binding = ActivityAccountSelectBinding.inflate(layoutInflater)
|
2023-10-30 23:32:01 +00:00
|
|
|
setContentView(binding.root)
|
|
|
|
|
2023-11-11 22:47:23 +00:00
|
|
|
val recyclerView: AutofitRecyclerView = binding.accountRecyclerView
|
|
|
|
|
|
|
|
observe(viewModel.accounts) { liveAccounts ->
|
|
|
|
val adapter = AccountAdapter(
|
|
|
|
liveAccounts,
|
|
|
|
// Handle the selected account
|
|
|
|
accountSelectCallback = {
|
|
|
|
viewModel.handleAccountSelect(it, this)
|
|
|
|
observe(viewModel.isAllowedLogin) { isAllowedLogin ->
|
|
|
|
if (isAllowedLogin) {
|
|
|
|
// We are allowed to continue to MainActivity
|
|
|
|
navigateToMainActivity()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
accountCreateCallback = { viewModel.handleAccountUpdate(it, this) },
|
|
|
|
accountEditCallback = {
|
|
|
|
viewModel.handleAccountUpdate(it, this)
|
|
|
|
|
|
|
|
// We came from MainActivity, return there
|
|
|
|
// and switch to the edited account
|
|
|
|
if (isEditingFromMainActivity) {
|
|
|
|
setAccount(it)
|
|
|
|
navigateToMainActivity()
|
|
|
|
}
|
|
|
|
},
|
|
|
|
accountDeleteCallback = { viewModel.handleAccountDelete(it,this) }
|
|
|
|
)
|
|
|
|
|
|
|
|
recyclerView.adapter = adapter
|
|
|
|
|
|
|
|
if (isTvSettings()) {
|
|
|
|
binding.editAccountButton.setBackgroundResource(
|
|
|
|
R.drawable.player_button_tv_attr_no_bg
|
|
|
|
)
|
|
|
|
}
|
2023-10-30 23:32:01 +00:00
|
|
|
|
2023-11-11 22:47:23 +00:00
|
|
|
observe(viewModel.selectedKeyIndex) { selectedKeyIndex ->
|
|
|
|
// Scroll to current account (which is focused by default)
|
|
|
|
val layoutManager = recyclerView.layoutManager as GridLayoutManager
|
|
|
|
layoutManager.scrollToPositionWithOffset(selectedKeyIndex, 0)
|
|
|
|
}
|
2023-10-30 23:32:01 +00:00
|
|
|
|
2023-11-11 22:47:23 +00:00
|
|
|
observe(viewModel.isEditing) { isEditing ->
|
|
|
|
if (isEditing) {
|
|
|
|
binding.editAccountButton.setImageResource(R.drawable.ic_baseline_close_24)
|
|
|
|
binding.title.setText(R.string.manage_accounts)
|
|
|
|
adapter.viewType = VIEW_TYPE_EDIT_ACCOUNT
|
|
|
|
} else {
|
|
|
|
binding.editAccountButton.setImageResource(R.drawable.ic_baseline_edit_24)
|
|
|
|
binding.title.setText(R.string.select_an_account)
|
|
|
|
adapter.viewType = VIEW_TYPE_SELECT_ACCOUNT
|
|
|
|
}
|
|
|
|
|
|
|
|
adapter.notifyDataSetChanged()
|
|
|
|
}
|
2023-10-30 23:32:01 +00:00
|
|
|
|
2023-11-11 22:47:23 +00:00
|
|
|
if (isEditingFromMainActivity) {
|
|
|
|
viewModel.setIsEditing(true)
|
|
|
|
}
|
2023-11-02 20:14:16 +00:00
|
|
|
|
2023-11-11 22:47:23 +00:00
|
|
|
binding.editAccountButton.setOnClickListener {
|
|
|
|
// We came from MainActivity, return there
|
|
|
|
// and resume its state
|
|
|
|
if (isEditingFromMainActivity) {
|
|
|
|
navigateToMainActivity()
|
|
|
|
return@setOnClickListener
|
|
|
|
}
|
2023-10-30 23:32:01 +00:00
|
|
|
|
2023-11-11 22:47:23 +00:00
|
|
|
viewModel.toggleIsEditing()
|
2023-10-30 23:32:01 +00:00
|
|
|
}
|
|
|
|
|
2023-11-11 22:47:23 +00:00
|
|
|
if (isTvSettings()) {
|
|
|
|
recyclerView.spanCount = if (liveAccounts.count() + 1 <= 6) {
|
|
|
|
liveAccounts.count() + 1
|
|
|
|
} else 6
|
|
|
|
}
|
2023-10-30 23:32:01 +00:00
|
|
|
}
|
2024-03-08 00:45:20 +00:00
|
|
|
|
|
|
|
askBiometricAuth()
|
2023-10-30 23:32:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private fun navigateToMainActivity() {
|
|
|
|
val mainIntent = Intent(this, MainActivity::class.java)
|
|
|
|
startActivity(mainIntent)
|
|
|
|
finish() // Finish the account selection activity
|
|
|
|
}
|
2024-03-08 00:45:20 +00:00
|
|
|
|
|
|
|
override fun onAuthenticationSuccess() {
|
|
|
|
Log.i(BiometricAuthenticator.TAG,"Authentication successful in AccountSelectActivity")
|
|
|
|
}
|
2023-10-30 23:32:01 +00:00
|
|
|
}
|