diff --git a/fints4javaAndroidApp/src/main/java/net/dankito/banking/fints4java/android/RouterAndroid.kt b/fints4javaAndroidApp/src/main/java/net/dankito/banking/fints4java/android/RouterAndroid.kt index 8887db0c..7c7def6b 100644 --- a/fints4javaAndroidApp/src/main/java/net/dankito/banking/fints4java/android/RouterAndroid.kt +++ b/fints4javaAndroidApp/src/main/java/net/dankito/banking/fints4java/android/RouterAndroid.kt @@ -21,51 +21,49 @@ import java.util.concurrent.atomic.AtomicReference open class RouterAndroid(protected val activityTracker: CurrentActivityTracker) : IRouter { override fun showAddAccountDialog(presenter: BankingPresenter) { - activityTracker.currentActivity?.let { activity -> + activityTracker.currentOrNextActivity { activity -> AddAccountDialog().show(activity) } } override fun getTanFromUserFromNonUiThread(account: Account, tanChallenge: TanChallenge, presenter: BankingPresenter): EnterTanResult { - return activityTracker.currentActivity?.let { activity -> - val enteredTan = AtomicReference(null) - val tanEnteredLatch = CountDownLatch(1) + val enteredTan = AtomicReference(null) + val tanEnteredLatch = CountDownLatch(1) + activityTracker.currentOrNextActivity { activity -> activity.runOnUiThread { EnterTanDialog().show(account, tanChallenge, activity, false) { enteredTan.set(it) tanEnteredLatch.countDown() } } - - try { tanEnteredLatch.await() } catch (ignored: Exception) { } - - enteredTan.get() } - ?: EnterTanResult.userDidNotEnterTan() + + try { tanEnteredLatch.await() } catch (ignored: Exception) { } + + return enteredTan.get() } override fun getAtcFromUserFromNonUiThread(tanMedium: TanGeneratorTanMedium): EnterTanGeneratorAtcResult { - return activityTracker.currentActivity?.let { activity -> - val result = AtomicReference(null) - val tanEnteredLatch = CountDownLatch(1) + val result = AtomicReference(null) + val tanEnteredLatch = CountDownLatch(1) + activityTracker.currentOrNextActivity { activity -> activity.runOnUiThread { EnterAtcDialog().show(tanMedium, activity, false) { enteredResult -> result.set(enteredResult) tanEnteredLatch.countDown() } } - - try { tanEnteredLatch.await() } catch (ignored: Exception) { } - - result.get() } - ?: EnterTanGeneratorAtcResult.userDidNotEnterTan() + + try { tanEnteredLatch.await() } catch (ignored: Exception) { } + + return result.get() } override fun showTransferMoneyDialog(presenter: BankingPresenter, preselectedBankAccount: BankAccount?, preselectedValues: TransferMoneyData?) { - activityTracker.currentActivity?.let { activity -> + activityTracker.currentOrNextActivity { activity -> TransferMoneyDialog().show(activity, preselectedBankAccount, preselectedValues) } } diff --git a/fints4javaAndroidApp/src/main/java/net/dankito/banking/fints4java/android/ui/util/CurrentActivityTracker.kt b/fints4javaAndroidApp/src/main/java/net/dankito/banking/fints4java/android/ui/util/CurrentActivityTracker.kt index 36f12ed1..ed278b27 100644 --- a/fints4javaAndroidApp/src/main/java/net/dankito/banking/fints4java/android/ui/util/CurrentActivityTracker.kt +++ b/fints4javaAndroidApp/src/main/java/net/dankito/banking/fints4java/android/ui/util/CurrentActivityTracker.kt @@ -8,6 +8,9 @@ import kotlin.concurrent.schedule open class CurrentActivityTracker { + protected val nextActivitySetListeners = CopyOnWriteArrayList<(BaseActivity) -> Unit>() + + var currentActivity: BaseActivity? = null set(value) { field = value // TODO: check field != value @@ -18,7 +21,14 @@ open class CurrentActivityTracker { } - protected val nextActivitySetListeners = CopyOnWriteArrayList<(BaseActivity) -> Unit>() + open fun currentOrNextActivity(activity: (BaseActivity) -> Unit) { + currentActivity?.let { + activity(it) + } + ?: addNextActivitySetListener { + activity(it) + } + } open fun addNextActivitySetListener(listener: (BaseActivity) -> Unit) { synchronized(nextActivitySetListeners) {