mirror of
https://github.com/recloudstream/cloudstream.git
synced 2024-08-15 01:53:11 +00:00
Used CountDownTimer instead of while
This commit is contained in:
parent
e25603d63a
commit
044274038f
2 changed files with 56 additions and 55 deletions
|
@ -2,6 +2,7 @@ package com.lagradost.cloudstream3.ui.settings
|
|||
|
||||
import android.graphics.Bitmap
|
||||
import android.os.Bundle
|
||||
import android.os.CountDownTimer
|
||||
import android.view.View
|
||||
import android.view.View.FOCUS_DOWN
|
||||
import android.view.inputmethod.EditorInfo
|
||||
|
@ -63,7 +64,6 @@ import com.lagradost.cloudstream3.utils.UIHelper.dismissSafe
|
|||
import com.lagradost.cloudstream3.utils.UIHelper.hideKeyboard
|
||||
import com.lagradost.cloudstream3.utils.UIHelper.setImage
|
||||
import com.lagradost.cloudstream3.utils.UIHelper.toPx
|
||||
import kotlinx.coroutines.delay
|
||||
import qrcode.QRCode
|
||||
import java.io.ByteArrayOutputStream
|
||||
|
||||
|
@ -146,9 +146,13 @@ class SettingsAccount : PreferenceFragmentCompat(), BiometricAuthenticator.Biome
|
|||
try {
|
||||
when (api) {
|
||||
is OAuth2API -> {
|
||||
if (isLayout(TV or EMULATOR) && api.supportDeviceAuth && activity != null) {
|
||||
if (isLayout(PHONE) || !api.supportDeviceAuth) {
|
||||
api.authenticate(activity)
|
||||
} else if (api.supportDeviceAuth && activity != null) {
|
||||
|
||||
val binding: DeviceAuthBinding =
|
||||
DeviceAuthBinding.inflate(activity.layoutInflater, null, false)
|
||||
|
||||
val builder =
|
||||
AlertDialog.Builder(activity)
|
||||
.setView(binding.root)
|
||||
|
@ -166,27 +170,24 @@ class SettingsAccount : PreferenceFragmentCompat(), BiometricAuthenticator.Biome
|
|||
try {
|
||||
val pinCodeData = api.getDevicePin()
|
||||
if (pinCodeData == null) {
|
||||
activity.runOnUiThread {
|
||||
showToast(
|
||||
activity.getString(R.string.device_pin_error_message)
|
||||
)
|
||||
}
|
||||
showToast(R.string.device_pin_error_message)
|
||||
api.authenticate(activity)
|
||||
return@ioSafe
|
||||
}
|
||||
|
||||
val logoBytes = ContextCompat.getDrawable(
|
||||
/*val logoBytes = ContextCompat.getDrawable(
|
||||
activity,
|
||||
R.drawable.cloud_2_solid
|
||||
)?.toBitmapOrNull()?.let { bitmap ->
|
||||
val csLogo = ByteArrayOutputStream()
|
||||
bitmap.compress(Bitmap.CompressFormat.PNG, 100, csLogo)
|
||||
csLogo.toByteArray()
|
||||
}
|
||||
}*/
|
||||
|
||||
val qrCodeImage = QRCode.ofRoundedSquares()
|
||||
.withColor(activity.colorFromAttribute(R.attr.colorPrimary))
|
||||
.withLogo(logoBytes, 200.toPx, 200.toPx)
|
||||
.withColor(activity.colorFromAttribute(R.attr.textColor))
|
||||
.withBackgroundColor(activity.colorFromAttribute(R.attr.primaryBlackBackground))
|
||||
//.withLogo(logoBytes, 200.toPx, 200.toPx) //For later if logo needed anytime
|
||||
.build(pinCodeData.verificationUrl)
|
||||
.render().nativeImage() as Bitmap
|
||||
|
||||
|
@ -194,51 +195,59 @@ class SettingsAccount : PreferenceFragmentCompat(), BiometricAuthenticator.Biome
|
|||
dialog.show()
|
||||
binding.apply {
|
||||
devicePinCode.setText(txt(pinCodeData.userCode))
|
||||
deviceAuthMessage.setText(txt(R.string.device_pin_url_message, pinCodeData.verificationUrl))
|
||||
deviceAuthMessage.setText(
|
||||
txt(
|
||||
R.string.device_pin_url_message,
|
||||
pinCodeData.verificationUrl
|
||||
)
|
||||
)
|
||||
deviceAuthQrcode.setImage(
|
||||
img(qrCodeImage)
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
var expirationCounter = pinCodeData.expiresIn
|
||||
val expirationMillis =
|
||||
pinCodeData.expiresIn.times(1000).toLong()
|
||||
|
||||
while (expirationCounter > 0) {
|
||||
activity.runOnUiThread {
|
||||
binding.deviceAuthValidationCounter.setText(
|
||||
txt(
|
||||
R.string.device_pin_counter_text, expirationCounter.div(60) , expirationCounter.rem(60)
|
||||
object : CountDownTimer(expirationMillis, 1000) {
|
||||
|
||||
override fun onTick(millisUntilFinished: Long) {
|
||||
val secondsUntilFinished =
|
||||
millisUntilFinished.div(1000).toInt()
|
||||
|
||||
binding.deviceAuthValidationCounter.setText(
|
||||
txt(
|
||||
R.string.device_pin_counter_text,
|
||||
secondsUntilFinished.div(60),
|
||||
secondsUntilFinished.rem(60)
|
||||
)
|
||||
)
|
||||
)
|
||||
}
|
||||
|
||||
if (expirationCounter.rem(pinCodeData.interval) == 0 && api.handleDeviceAuth(pinCodeData)) {
|
||||
activity.runOnUiThread {
|
||||
showToast(
|
||||
activity.getString(R.string.authenticated_user)
|
||||
.format(
|
||||
ioSafe {
|
||||
if (secondsUntilFinished.rem(pinCodeData.interval) == 0 && api.handleDeviceAuth(pinCodeData)) {
|
||||
showToast(
|
||||
txt(
|
||||
R.string.authenticated_user,
|
||||
api.name
|
||||
)
|
||||
)
|
||||
)
|
||||
dialog.dismissSafe(activity)
|
||||
cancel()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
override fun onFinish() {
|
||||
showToast(R.string.device_pin_expired_message)
|
||||
dialog.dismissSafe(activity)
|
||||
}
|
||||
return@ioSafe
|
||||
}
|
||||
delay(1000)
|
||||
expirationCounter--
|
||||
}
|
||||
activity.runOnUiThread {
|
||||
showToast(
|
||||
activity.getString(R.string.device_pin_expired_message)
|
||||
)
|
||||
dialog.dismissSafe(activity)
|
||||
|
||||
}.start()
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
logError(e)
|
||||
}
|
||||
}
|
||||
} else {
|
||||
api.authenticate(activity)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -332,23 +341,15 @@ class SettingsAccount : PreferenceFragmentCompat(), BiometricAuthenticator.Biome
|
|||
server = if (api.requiresServer) binding.loginServerInput.text?.toString() else null,
|
||||
)
|
||||
ioSafe {
|
||||
val isSuccessful = try {
|
||||
api.login(loginData)
|
||||
try {
|
||||
showToast(
|
||||
txt(
|
||||
if (api.login(loginData)) R.string.authenticated_user else R.string.authenticated_user_fail,
|
||||
api.name
|
||||
)
|
||||
)
|
||||
} catch (e: Exception) {
|
||||
logError(e)
|
||||
false
|
||||
}
|
||||
activity.runOnUiThread {
|
||||
try {
|
||||
showToast(
|
||||
activity.getString(if (isSuccessful) R.string.authenticated_user else R.string.authenticated_user_fail)
|
||||
.format(
|
||||
api.name
|
||||
)
|
||||
)
|
||||
} catch (e: Exception) {
|
||||
logError(e) // format might fail
|
||||
}
|
||||
}
|
||||
}
|
||||
dialog.dismissSafe(activity)
|
||||
|
|
|
@ -3,6 +3,6 @@
|
|||
android:viewportHeight="283" xmlns:android="http://schemas.android.com/apk/res/android">
|
||||
<path android:name="path"
|
||||
android:pathData="M 245.05 148.63 C 242.249 148.627 239.463 149.052 236.79 149.89 C 235.151 141.364 230.698 133.63 224.147 127.931 C 217.597 122.233 209.321 118.893 200.65 118.45 C 195.913 105.431 186.788 94.458 174.851 87.427 C 162.914 80.396 148.893 77.735 135.21 79.905 C 121.527 82.074 109.017 88.941 99.84 99.32 C 89.871 95.945 79.051 96.024 69.133 99.545 C 59.215 103.065 50.765 109.826 45.155 118.73 C 39.545 127.634 37.094 138.174 38.2 148.64 L 37.94 148.64 C 30.615 148.64 23.582 151.553 18.403 156.733 C 13.223 161.912 10.31 168.945 10.31 176.27 C 10.31 183.595 13.223 190.628 18.403 195.807 C 23.582 200.987 30.615 203.9 37.94 203.9 L 245.05 203.9 C 252.375 203.9 259.408 200.987 264.587 195.807 C 269.767 190.628 272.68 183.595 272.68 176.27 C 272.68 168.945 269.767 161.912 264.587 156.733 C 259.408 151.553 252.375 148.64 245.05 148.64 Z"
|
||||
android:fillColor="?attr/colorPrimary" android:strokeWidth="1"
|
||||
android:fillColor="?attr/textColor" android:strokeWidth="1"
|
||||
tools:ignore="VectorPath"/>
|
||||
</vector>
|
Loading…
Add table
Add a link
Reference in a new issue