Implemented displaying an exception if tan image decoding fails

This commit is contained in:
dankl 2020-01-02 00:35:10 +01:00 committed by dankito
parent 29c65354be
commit 110fc59756
4 changed files with 34 additions and 7 deletions

View File

@ -3,6 +3,7 @@ package net.dankito.banking.fints4java.android.ui.dialogs
import android.content.Context import android.content.Context
import android.graphics.BitmapFactory import android.graphics.BitmapFactory
import android.os.Bundle import android.os.Bundle
import android.os.Handler
import android.support.v4.app.DialogFragment import android.support.v4.app.DialogFragment
import android.support.v7.app.AlertDialog import android.support.v7.app.AlertDialog
import android.support.v7.app.AppCompatActivity import android.support.v7.app.AppCompatActivity
@ -28,6 +29,7 @@ import net.dankito.fints.model.TanChallenge
import net.dankito.fints.model.TanProcedureType import net.dankito.fints.model.TanProcedureType
import net.dankito.fints.response.client.FinTsClientResponse import net.dankito.fints.response.client.FinTsClientResponse
import net.dankito.fints.tan.FlickercodeDecoder import net.dankito.fints.tan.FlickercodeDecoder
import net.dankito.fints.tan.TanImage
import net.dankito.fints.tan.TanImageDecoder import net.dankito.fints.tan.TanImageDecoder
@ -144,11 +146,14 @@ open class EnterTanDialog : DialogFragment() {
|| tanChallenge.tanProcedure.type == TanProcedureType.PhotoTan) { || tanChallenge.tanProcedure.type == TanProcedureType.PhotoTan) {
rootView.tanImageView.visibility = View.VISIBLE rootView.tanImageView.visibility = View.VISIBLE
TanImageDecoder().decodeChallenge(tanChallenge.tanChallenge)?.let { tanImage -> val decodedImage = TanImageDecoder().decodeChallenge(tanChallenge.tanChallenge)
val bitmap = BitmapFactory.decodeByteArray(tanImage.imageBytes, 0, tanImage.imageBytes.size) if (decodedImage.decodingSuccessful) {
val bitmap = BitmapFactory.decodeByteArray(decodedImage.imageBytes, 0, decodedImage.imageBytes.size)
rootView.imgTanImageView.setImageBitmap(bitmap) rootView.imgTanImageView.setImageBitmap(bitmap)
} }
// TODO: what to do if decoding fails? At least a message should get shown to user else {
showDecodingTanImageFailedErrorDelayed(decodedImage) // this method gets called right on start up before dialog is shown -> Alert would get displayed before dialog and therefore covered by dialog if we don't delay displaying dialog
}
} }
rootView.edtxtEnteredTan.inputType = InputType.TYPE_CLASS_NUMBER rootView.edtxtEnteredTan.inputType = InputType.TYPE_CLASS_NUMBER
@ -156,6 +161,22 @@ open class EnterTanDialog : DialogFragment() {
} }
protected open fun showDecodingTanImageFailedErrorDelayed(decodedImage: TanImage) {
val handler = Handler()
handler.postDelayed({ showDecodingTanImageFailedError(decodedImage) }, 500)
}
protected open fun showDecodingTanImageFailedError(decodedImage: TanImage) {
activity?.let { context ->
AlertDialog.Builder(context)
.setMessage(context.getString(R.string.dialog_enter_tan_error_could_not_decode_tan_image, decodedImage.error?.localizedMessage))
.setPositiveButton(android.R.string.ok) { dialog, _ -> dialog.dismiss() }
.show()
}
}
protected open fun handleChangeTanMediumResponse(newUsedTanMedium: TanMedium, response: FinTsClientResponse) { protected open fun handleChangeTanMediumResponse(newUsedTanMedium: TanMedium, response: FinTsClientResponse) {
activity?.let { activity -> activity?.let { activity ->
activity.runOnUiThread { activity.runOnUiThread {

View File

@ -61,6 +61,7 @@
<string name="dialog_enter_tan_select_tan_procedure">TAN procedure</string> <string name="dialog_enter_tan_select_tan_procedure">TAN procedure</string>
<string name="dialog_enter_tan_select_tan_medium">TAN medium</string> <string name="dialog_enter_tan_select_tan_medium">TAN medium</string>
<string name="dialog_enter_tan_enter_tan">Enter TAN:</string> <string name="dialog_enter_tan_enter_tan">Enter TAN:</string>
<string name="dialog_enter_tan_error_could_not_decode_tan_image">Could not decode QR code / PhotoTan. Most likely an internal error:\n%s.</string>
<string name="dialog_enter_tan_tan_medium_successfully_changed">TAN medium successfully changed to \'%s\'.</string> <string name="dialog_enter_tan_tan_medium_successfully_changed">TAN medium successfully changed to \'%s\'.</string>
<string name="dialog_enter_tan_error_changing_tan_medium">Could not change TAN medium to \'%s\':\n%s.</string> <string name="dialog_enter_tan_error_changing_tan_medium">Could not change TAN medium to \'%s\':\n%s.</string>

View File

@ -3,9 +3,14 @@ package net.dankito.fints.tan
open class TanImage( open class TanImage(
val mimeType: String, val mimeType: String,
val imageBytes: ByteArray val imageBytes: ByteArray,
val error: Exception? = null
) { ) {
val decodingSuccessful: Boolean
get() = error == null
override fun toString(): String { override fun toString(): String {
return "$mimeType ${imageBytes.size} bytes" return "$mimeType ${imageBytes.size} bytes"
} }

View File

@ -11,7 +11,7 @@ open class TanImageDecoder {
} }
open fun decodeChallenge(challengeHHD_UC: String): TanImage? { open fun decodeChallenge(challengeHHD_UC: String): TanImage {
try { try {
val bytes = challengeHHD_UC.toByteArray(HbciCharset.DefaultCharset) val bytes = challengeHHD_UC.toByteArray(HbciCharset.DefaultCharset)
@ -29,9 +29,9 @@ open class TanImageDecoder {
return TanImage(mimeType, imageBytes) return TanImage(mimeType, imageBytes)
} catch (e: Exception) { } catch (e: Exception) {
log.error("Could not decode challenge HHD_UC to TanImage: $challengeHHD_UC", e) log.error("Could not decode challenge HHD_UC to TanImage: $challengeHHD_UC", e)
}
return null return TanImage("", ByteArray(0), e)
}
} }
protected open fun getLength(higherOrderByte: Byte, lowerOrderByte: Byte): Int { protected open fun getLength(higherOrderByte: Byte, lowerOrderByte: Byte): Int {