diff --git a/fints4javaAndroidApp/src/main/java/net/dankito/banking/fints4java/android/ui/dialogs/EnterTanDialog.kt b/fints4javaAndroidApp/src/main/java/net/dankito/banking/fints4java/android/ui/dialogs/EnterTanDialog.kt index 54c15bd1..ec32394b 100644 --- a/fints4javaAndroidApp/src/main/java/net/dankito/banking/fints4java/android/ui/dialogs/EnterTanDialog.kt +++ b/fints4javaAndroidApp/src/main/java/net/dankito/banking/fints4java/android/ui/dialogs/EnterTanDialog.kt @@ -3,6 +3,7 @@ package net.dankito.banking.fints4java.android.ui.dialogs import android.content.Context import android.graphics.BitmapFactory import android.os.Bundle +import android.os.Handler import android.support.v4.app.DialogFragment import android.support.v7.app.AlertDialog 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.response.client.FinTsClientResponse import net.dankito.fints.tan.FlickercodeDecoder +import net.dankito.fints.tan.TanImage import net.dankito.fints.tan.TanImageDecoder @@ -144,11 +146,14 @@ open class EnterTanDialog : DialogFragment() { || tanChallenge.tanProcedure.type == TanProcedureType.PhotoTan) { rootView.tanImageView.visibility = View.VISIBLE - TanImageDecoder().decodeChallenge(tanChallenge.tanChallenge)?.let { tanImage -> - val bitmap = BitmapFactory.decodeByteArray(tanImage.imageBytes, 0, tanImage.imageBytes.size) + val decodedImage = TanImageDecoder().decodeChallenge(tanChallenge.tanChallenge) + if (decodedImage.decodingSuccessful) { + val bitmap = BitmapFactory.decodeByteArray(decodedImage.imageBytes, 0, decodedImage.imageBytes.size) 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 @@ -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) { activity?.let { activity -> activity.runOnUiThread { diff --git a/fints4javaAndroidApp/src/main/res/values/strings.xml b/fints4javaAndroidApp/src/main/res/values/strings.xml index 798bd4de..09c93272 100644 --- a/fints4javaAndroidApp/src/main/res/values/strings.xml +++ b/fints4javaAndroidApp/src/main/res/values/strings.xml @@ -61,6 +61,7 @@ TAN procedure TAN medium Enter TAN: + Could not decode QR code / PhotoTan. Most likely an internal error:\n%s. TAN medium successfully changed to \'%s\'. Could not change TAN medium to \'%s\':\n%s. diff --git a/fints4javaLib/src/main/kotlin/net/dankito/fints/tan/TanImage.kt b/fints4javaLib/src/main/kotlin/net/dankito/fints/tan/TanImage.kt index 66f31424..83a5f827 100644 --- a/fints4javaLib/src/main/kotlin/net/dankito/fints/tan/TanImage.kt +++ b/fints4javaLib/src/main/kotlin/net/dankito/fints/tan/TanImage.kt @@ -3,9 +3,14 @@ package net.dankito.fints.tan open class TanImage( val mimeType: String, - val imageBytes: ByteArray + val imageBytes: ByteArray, + val error: Exception? = null ) { + val decodingSuccessful: Boolean + get() = error == null + + override fun toString(): String { return "$mimeType ${imageBytes.size} bytes" } diff --git a/fints4javaLib/src/main/kotlin/net/dankito/fints/tan/TanImageDecoder.kt b/fints4javaLib/src/main/kotlin/net/dankito/fints/tan/TanImageDecoder.kt index b0a039c8..95ed120c 100644 --- a/fints4javaLib/src/main/kotlin/net/dankito/fints/tan/TanImageDecoder.kt +++ b/fints4javaLib/src/main/kotlin/net/dankito/fints/tan/TanImageDecoder.kt @@ -11,7 +11,7 @@ open class TanImageDecoder { } - open fun decodeChallenge(challengeHHD_UC: String): TanImage? { + open fun decodeChallenge(challengeHHD_UC: String): TanImage { try { val bytes = challengeHHD_UC.toByteArray(HbciCharset.DefaultCharset) @@ -29,9 +29,9 @@ open class TanImageDecoder { return TanImage(mimeType, imageBytes) } catch (e: Exception) { 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 {