add showtoast, use strings and migrate to account select

This commit is contained in:
IndusAryan 2023-12-20 20:20:00 +05:30
parent 0e562fd342
commit 15d75e5194
4 changed files with 70 additions and 64 deletions

View file

@ -135,6 +135,7 @@ import com.lagradost.cloudstream3.utils.Coroutines.main
import com.lagradost.cloudstream3.utils.DataStore.getKey import com.lagradost.cloudstream3.utils.DataStore.getKey
import com.lagradost.cloudstream3.utils.DataStore.setKey import com.lagradost.cloudstream3.utils.DataStore.setKey
import com.lagradost.cloudstream3.utils.DataStoreHelper import com.lagradost.cloudstream3.utils.DataStoreHelper
import com.lagradost.cloudstream3.utils.DataStoreHelper.accounts
import com.lagradost.cloudstream3.utils.DataStoreHelper.migrateResumeWatching import com.lagradost.cloudstream3.utils.DataStoreHelper.migrateResumeWatching
import com.lagradost.cloudstream3.utils.Event import com.lagradost.cloudstream3.utils.Event
import com.lagradost.cloudstream3.utils.InAppUpdater.Companion.runAutoUpdate import com.lagradost.cloudstream3.utils.InAppUpdater.Companion.runAutoUpdate
@ -1155,13 +1156,13 @@ class MainActivity : AppCompatActivity(), ColorPickerDialogListener {
changeStatusBarState(isEmulatorSettings()) changeStatusBarState(isEmulatorSettings())
/** Biometric Stuff **/ /** Biometric stuff for users without accounts **/
val authEnabled = settingsManager.getBoolean( val authEnabled = settingsManager.getBoolean(getString(R.string.biometric_enabled_key), false)
getString(R.string.biometric_enabled_key), false) val noAccounts = settingsManager.getBoolean(getString(R.string.skip_startup_account_select_key), false) || accounts.count() <= 1
if (isTruePhone() && authEnabled) { if (isTruePhone() && authEnabled && noAccounts ) {
BiometricAuthenticator.initializeBiometrics(this@MainActivity) BiometricAuthenticator.initializeBiometrics(this@MainActivity)
BiometricAuthenticator.checkBiometricAvailability(this@MainActivity) BiometricAuthenticator.checkBiometricAvailability()
BiometricAuthenticator.biometricPrompt.authenticate(promptInfo) BiometricAuthenticator.biometricPrompt.authenticate(promptInfo)
} }

View file

@ -18,6 +18,7 @@ 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_EDIT_ACCOUNT
import com.lagradost.cloudstream3.ui.account.AccountAdapter.Companion.VIEW_TYPE_SELECT_ACCOUNT import com.lagradost.cloudstream3.ui.account.AccountAdapter.Companion.VIEW_TYPE_SELECT_ACCOUNT
import com.lagradost.cloudstream3.ui.settings.SettingsFragment.Companion.isTvSettings import com.lagradost.cloudstream3.ui.settings.SettingsFragment.Companion.isTvSettings
import com.lagradost.cloudstream3.utils.BiometricAuthenticator
import com.lagradost.cloudstream3.utils.DataStoreHelper.accounts import com.lagradost.cloudstream3.utils.DataStoreHelper.accounts
import com.lagradost.cloudstream3.utils.DataStoreHelper.selectedKeyIndex import com.lagradost.cloudstream3.utils.DataStoreHelper.selectedKeyIndex
import com.lagradost.cloudstream3.utils.DataStoreHelper.setAccount import com.lagradost.cloudstream3.utils.DataStoreHelper.setAccount
@ -41,6 +42,8 @@ class AccountSelectActivity : AppCompatActivity() {
) )
val settingsManager = PreferenceManager.getDefaultSharedPreferences(this) val settingsManager = PreferenceManager.getDefaultSharedPreferences(this)
val authEnabled = settingsManager.getBoolean(getString(R.string.biometric_enabled_key), false)
val skipStartup = settingsManager.getBoolean( val skipStartup = settingsManager.getBoolean(
getString(R.string.skip_startup_account_select_key), getString(R.string.skip_startup_account_select_key),
false false
@ -48,6 +51,15 @@ class AccountSelectActivity : AppCompatActivity() {
viewModel = ViewModelProvider(this)[AccountViewModel::class.java] viewModel = ViewModelProvider(this)[AccountViewModel::class.java]
fun askBiometricAuth() {
if (BiometricAuthenticator.isTruePhone() && authEnabled) {
BiometricAuthenticator.initializeBiometrics(this)
BiometricAuthenticator.checkBiometricAvailability()
BiometricAuthenticator.biometricPrompt.authenticate(BiometricAuthenticator.promptInfo)
}
}
// Don't show account selection if there is only // Don't show account selection if there is only
// one account that exists // one account that exists
if (!isEditingFromMainActivity && skipStartup) { if (!isEditingFromMainActivity && skipStartup) {
@ -158,6 +170,8 @@ class AccountSelectActivity : AppCompatActivity() {
} else 6 } else 6
} }
} }
askBiometricAuth()
} }
private fun navigateToMainActivity() { private fun navigateToMainActivity() {

View file

@ -1,9 +1,10 @@
package com.lagradost.cloudstream3.utils package com.lagradost.cloudstream3.utils
import android.content.Context import android.app.Activity
import android.os.Build import android.os.Build
import android.util.Log import android.util.Log
import android.widget.Toast import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.biometric.BiometricManager import androidx.biometric.BiometricManager
import androidx.biometric.BiometricManager.Authenticators.BIOMETRIC_STRONG import androidx.biometric.BiometricManager.Authenticators.BIOMETRIC_STRONG
import androidx.biometric.BiometricManager.Authenticators.BIOMETRIC_WEAK import androidx.biometric.BiometricManager.Authenticators.BIOMETRIC_WEAK
@ -11,7 +12,8 @@ import androidx.biometric.BiometricManager.Authenticators.DEVICE_CREDENTIAL
import androidx.biometric.BiometricPrompt import androidx.biometric.BiometricPrompt
import androidx.core.content.ContextCompat import androidx.core.content.ContextCompat
import com.lagradost.cloudstream3.AcraApplication.Companion.context import com.lagradost.cloudstream3.AcraApplication.Companion.context
import com.lagradost.cloudstream3.MainActivity import com.lagradost.cloudstream3.CommonActivity.showToast
import com.lagradost.cloudstream3.R
import com.lagradost.cloudstream3.ui.settings.SettingsFragment.Companion.isEmulatorSettings import com.lagradost.cloudstream3.ui.settings.SettingsFragment.Companion.isEmulatorSettings
import com.lagradost.cloudstream3.ui.settings.SettingsFragment.Companion.isTrueTvSettings import com.lagradost.cloudstream3.ui.settings.SettingsFragment.Companion.isTrueTvSettings
import com.lagradost.cloudstream3.ui.settings.SettingsFragment.Companion.isTvSettings import com.lagradost.cloudstream3.ui.settings.SettingsFragment.Companion.isTvSettings
@ -21,80 +23,67 @@ object BiometricAuthenticator {
private lateinit var biometricManager: BiometricManager private lateinit var biometricManager: BiometricManager
lateinit var biometricPrompt: BiometricPrompt lateinit var biometricPrompt: BiometricPrompt
lateinit var promptInfo: BiometricPrompt.PromptInfo lateinit var promptInfo: BiometricPrompt.PromptInfo
const val TAG = "cs3Auth"
fun initializeBiometrics(activity: MainActivity) { fun initializeBiometrics(activity: Activity) {
val executor = ContextCompat.getMainExecutor(activity) val executor = ContextCompat.getMainExecutor(activity)
biometricManager = BiometricManager.from(activity) biometricManager = BiometricManager.from(activity)
biometricPrompt = BiometricPrompt(activity, executor, object : BiometricPrompt.AuthenticationCallback() { biometricPrompt = BiometricPrompt(activity as AppCompatActivity, executor, object : BiometricPrompt.AuthenticationCallback() {
override fun onAuthenticationError(errorCode: Int, errString: CharSequence) { override fun onAuthenticationError(errorCode: Int, errString: CharSequence) {
super.onAuthenticationError(errorCode, errString) super.onAuthenticationError(errorCode, errString)
Toast.makeText(context?.applicationContext, "Authentication error: $errString", Toast.LENGTH_SHORT).show() Log.d(TAG, "Authentication error: $errString")
activity.finish() activity.finish()
} }
override fun onAuthenticationSucceeded(result: BiometricPrompt.AuthenticationResult) { override fun onAuthenticationSucceeded(result: BiometricPrompt.AuthenticationResult) {
super.onAuthenticationSucceeded(result) super.onAuthenticationSucceeded(result)
Log.d("Cs3Auth", "Biometric succeeded.") Log.d(TAG, "Biometric succeeded.")
} }
override fun onAuthenticationFailed() { override fun onAuthenticationFailed() {
super.onAuthenticationFailed() super.onAuthenticationFailed()
Toast.makeText(context?.applicationContext, "Authentication failed", Toast.LENGTH_SHORT).show() Log.d(TAG, "Authentication error")
showToast(R.string.biometric_failed, Toast.LENGTH_SHORT)
activity.finish() activity.finish()
} }
}) })
promptInfo = BiometricPrompt.PromptInfo.Builder() promptInfo = BiometricPrompt.PromptInfo.Builder()
.setTitle("CloudStream") .setTitle("Unlock CloudStream")
.setSubtitle("Log in using your biometric credential") //.setSubtitle("Log in using your biometric credential")
//.setNegativeButtonText("Use account password") //.setNegativeButtonText("Use account password")
.setAllowedAuthenticators(BIOMETRIC_WEAK or BIOMETRIC_STRONG or DEVICE_CREDENTIAL) .setAllowedAuthenticators(BIOMETRIC_WEAK or BIOMETRIC_STRONG or DEVICE_CREDENTIAL)
.build() .build()
} }
fun checkBiometricAvailability(context: Context) { fun checkBiometricAvailability() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
// Strong and credential bundle cannot be checked at same time in API < 11 // Strong and credential bundle cannot be checked at same time in API < A11 (R)
when (biometricManager.canAuthenticate(BIOMETRIC_WEAK or BIOMETRIC_STRONG or DEVICE_CREDENTIAL)) { when (biometricManager.canAuthenticate(BIOMETRIC_WEAK or BIOMETRIC_STRONG or DEVICE_CREDENTIAL)) {
BiometricManager.BIOMETRIC_SUCCESS -> BiometricManager.BIOMETRIC_SUCCESS ->
Log.d("Cs3Auth", "App can authenticate.") Log.d(TAG, "App can authenticate.")
BiometricManager.BIOMETRIC_ERROR_NO_HARDWARE -> BiometricManager.BIOMETRIC_ERROR_NO_HARDWARE ->
Log.d("Cs3Auth", "No biometric sensor found.") Log.d(TAG, "No biometric sensor found.")
BiometricManager.BIOMETRIC_ERROR_HW_UNAVAILABLE -> BiometricManager.BIOMETRIC_ERROR_HW_UNAVAILABLE ->
Log.d("Cs3Auth", "Biometric authentication is currently unavailable.") Log.d(TAG, "Biometric authentication is currently unavailable.")
BiometricManager.BIOMETRIC_ERROR_NONE_ENROLLED -> BiometricManager.BIOMETRIC_ERROR_NONE_ENROLLED ->
Toast.makeText( showToast(R.string.biometric_not_enrolled, Toast.LENGTH_SHORT)
context,
"No biometric credentials are enrolled",
Toast.LENGTH_SHORT
).show()
BiometricManager.BIOMETRIC_ERROR_SECURITY_UPDATE_REQUIRED -> { BiometricManager.BIOMETRIC_ERROR_SECURITY_UPDATE_REQUIRED ->
Toast.makeText( showToast(R.string.biometric_update_required, Toast.LENGTH_SHORT)
context,
"Please update your software and security patches.",
Toast.LENGTH_SHORT
).show()
}
BiometricManager.BIOMETRIC_ERROR_UNSUPPORTED -> { BiometricManager.BIOMETRIC_ERROR_UNSUPPORTED ->
Toast.makeText( showToast(R.string.biometric_unsupported, Toast.LENGTH_SHORT)
context,
"Please update your software and security patches.",
Toast.LENGTH_SHORT
).show()
}
BiometricManager.BIOMETRIC_STATUS_UNKNOWN -> { BiometricManager.BIOMETRIC_STATUS_UNKNOWN ->
Log.d("Cs3Auth", "Unknown error encountered(Biometric data failed).") Log.e(TAG, "Unknown error encountered while authenticating fingerprint.")
}
} }
} }
@ -103,33 +92,26 @@ object BiometricAuthenticator {
when (biometricManager.canAuthenticate(BIOMETRIC_WEAK or BIOMETRIC_STRONG)) { when (biometricManager.canAuthenticate(BIOMETRIC_WEAK or BIOMETRIC_STRONG)) {
BiometricManager.BIOMETRIC_SUCCESS -> BiometricManager.BIOMETRIC_SUCCESS ->
Log.d("Cs3Auth", "App can authenticate using biometrics.") Log.d(TAG, "App can authenticate using biometrics.")
BiometricManager.BIOMETRIC_ERROR_NO_HARDWARE -> BiometricManager.BIOMETRIC_ERROR_NO_HARDWARE ->
Log.e("Cs3Auth", "No biometric features available on this device.") Log.d(TAG, "No biometric features available on this device.")
BiometricManager.BIOMETRIC_ERROR_HW_UNAVAILABLE -> BiometricManager.BIOMETRIC_ERROR_HW_UNAVAILABLE ->
Log.e("Cs3Auth", "Biometric features are currently unavailable.") Log.e(TAG, "Biometric features are currently unavailable.")
BiometricManager.BIOMETRIC_ERROR_NONE_ENROLLED -> BiometricManager.BIOMETRIC_ERROR_NONE_ENROLLED ->
Log.e("Cs3Auth", "Biometric features are currently unavailable.") Log.e(TAG, "Biometric features are currently unavailable.")
BiometricManager.BIOMETRIC_ERROR_SECURITY_UPDATE_REQUIRED -> { BiometricManager.BIOMETRIC_ERROR_SECURITY_UPDATE_REQUIRED ->
Toast.makeText( showToast(R.string.biometric_update_required, Toast.LENGTH_SHORT)
context,
"Please update your software and security patches.",
Toast.LENGTH_SHORT
).show()
}
BiometricManager.BIOMETRIC_ERROR_UNSUPPORTED -> { BiometricManager.BIOMETRIC_ERROR_UNSUPPORTED ->
Toast.makeText( showToast(R.string.biometric_unsupported, Toast.LENGTH_SHORT)
context,
"Please update your software and security patches.", BiometricManager.BIOMETRIC_STATUS_UNKNOWN ->
Toast.LENGTH_SHORT Log.d(TAG, "Unknown error encountered while authenticating fingerprint.")
).show()
}
BiometricManager.BIOMETRIC_STATUS_UNKNOWN -> {
Log.d("Cs3Auth", "Unknown error encountered(Biometric data failed).")
}
} }
} }
} }

View file

@ -67,7 +67,7 @@
<string name="enable_nsfw_on_providers_key" translatable="false">enable_nsfw_on_providers_key</string> <string name="enable_nsfw_on_providers_key" translatable="false">enable_nsfw_on_providers_key</string>
<string name="skip_startup_account_select_key" translatable="false">skip_startup_account_select_key</string> <string name="skip_startup_account_select_key" translatable="false">skip_startup_account_select_key</string>
<string name="enable_skip_op_from_database" translatable="false">enable_skip_op_from_database</string> <string name="enable_skip_op_from_database" translatable="false">enable_skip_op_from_database</string>
<string name="biometric_enabled_key" translatable="false">biometric_key</string>
<!-- FORMAT MIGHT TRANSLATE, WILL CAUSE CRASH IF APPLIED WRONG --> <!-- FORMAT MIGHT TRANSLATE, WILL CAUSE CRASH IF APPLIED WRONG -->
<string name="extra_info_format" formatted="true" translatable="false">%d %s | %s</string> <string name="extra_info_format" formatted="true" translatable="false">%d %s | %s</string>
<string name="storage_size_format" formatted="true" translatable="false">%s • %s</string> <string name="storage_size_format" formatted="true" translatable="false">%s • %s</string>
@ -741,5 +741,14 @@
<string name="auto_rotate_video_key">auto_rotate_video_key</string> <string name="auto_rotate_video_key">auto_rotate_video_key</string>
<string name="auto_rotate_video_desc">Enable automatic switching of screen orientation based on video orientation</string> <string name="auto_rotate_video_desc">Enable automatic switching of screen orientation based on video orientation</string>
<string name="auto_rotate_video">Auto rotate</string> <string name="auto_rotate_video">Auto rotate</string>
<string name="biometric_setting">Use Fingerprint authentication</string>
<string name="biometric_setting">Use Fingerprint sensor authentication</string>
<string name="biometric_success">Fingerprint authentication succeeded</string>
<string name="biometric_failed">Fingerprint authentication failed</string>
<string name="biometric_prompt_title">Unlock CloudStream</string>
<string name="biometric_not_enrolled">No biometric credentials are enrolled</string>
<string name="biometric_update_required">Please update software and security patches.</string>
<string name="biometric_unsupported">Biometric authentication is not supported on this device</string>
<string name="biometric_enabled_key" translatable="false">biometric_key</string>
</resources> </resources>