diff --git a/fints4javaAndroidApp/src/main/java/net/dankito/banking/fints4java/android/MainActivity.kt b/fints4javaAndroidApp/src/main/java/net/dankito/banking/fints4java/android/MainActivity.kt index 1fdbb052..d36666de 100644 --- a/fints4javaAndroidApp/src/main/java/net/dankito/banking/fints4java/android/MainActivity.kt +++ b/fints4javaAndroidApp/src/main/java/net/dankito/banking/fints4java/android/MainActivity.kt @@ -4,13 +4,13 @@ import android.os.Bundle import android.view.Menu import android.view.MotionEvent import androidx.appcompat.app.ActionBarDrawerToggle -import androidx.appcompat.app.AppCompatActivity import androidx.appcompat.widget.Toolbar import androidx.drawerlayout.widget.DrawerLayout import com.github.clans.fab.FloatingActionMenu import kotlinx.android.synthetic.main.activity_main.* import net.dankito.banking.fints4java.android.di.BankingComponent import net.dankito.banking.fints4java.android.di.BankingModule +import net.dankito.banking.fints4java.android.ui.activities.BaseActivity import net.dankito.banking.fints4java.android.ui.views.DrawerView import net.dankito.banking.fints4java.android.ui.views.MainActivityFloatingActionMenuButton import net.dankito.banking.ui.presenter.BankingPresenter @@ -18,7 +18,7 @@ import org.slf4j.LoggerFactory import javax.inject.Inject -class MainActivity : AppCompatActivity() { +class MainActivity : BaseActivity() { companion object { private val log = LoggerFactory.getLogger(MainActivity::class.java) 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 389fc653..8887db0c 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 @@ -1,10 +1,10 @@ package net.dankito.banking.fints4java.android -import androidx.appcompat.app.AppCompatActivity import net.dankito.banking.fints4java.android.ui.dialogs.AddAccountDialog import net.dankito.banking.fints4java.android.ui.dialogs.EnterAtcDialog import net.dankito.banking.fints4java.android.ui.dialogs.EnterTanDialog import net.dankito.banking.fints4java.android.ui.dialogs.TransferMoneyDialog +import net.dankito.banking.fints4java.android.ui.util.CurrentActivityTracker import net.dankito.banking.ui.IRouter import net.dankito.banking.ui.model.Account import net.dankito.banking.ui.model.BankAccount @@ -18,46 +18,56 @@ import java.util.concurrent.CountDownLatch import java.util.concurrent.atomic.AtomicReference -open class RouterAndroid(protected val activity: AppCompatActivity) : IRouter { +open class RouterAndroid(protected val activityTracker: CurrentActivityTracker) : IRouter { override fun showAddAccountDialog(presenter: BankingPresenter) { - AddAccountDialog().show(activity) + activityTracker.currentActivity?.let { activity -> + AddAccountDialog().show(activity) + } } override fun getTanFromUserFromNonUiThread(account: Account, tanChallenge: TanChallenge, presenter: BankingPresenter): EnterTanResult { - val enteredTan = AtomicReference(null) - val tanEnteredLatch = CountDownLatch(1) + return activityTracker.currentActivity?.let { activity -> + val enteredTan = AtomicReference(null) + val tanEnteredLatch = CountDownLatch(1) - activity.runOnUiThread { - EnterTanDialog().show(account, tanChallenge, activity, false) { - enteredTan.set(it) - tanEnteredLatch.countDown() + activity.runOnUiThread { + EnterTanDialog().show(account, tanChallenge, activity, false) { + enteredTan.set(it) + tanEnteredLatch.countDown() + } } + + try { tanEnteredLatch.await() } catch (ignored: Exception) { } + + enteredTan.get() } - - try { tanEnteredLatch.await() } catch (ignored: Exception) { } - - return enteredTan.get() + ?: EnterTanResult.userDidNotEnterTan() } override fun getAtcFromUserFromNonUiThread(tanMedium: TanGeneratorTanMedium): EnterTanGeneratorAtcResult { - val result = AtomicReference(null) - val tanEnteredLatch = CountDownLatch(1) + return activityTracker.currentActivity?.let { activity -> + val result = AtomicReference(null) + val tanEnteredLatch = CountDownLatch(1) - activity.runOnUiThread { - EnterAtcDialog().show(tanMedium, activity, false) { enteredResult -> - result.set(enteredResult) - tanEnteredLatch.countDown() + activity.runOnUiThread { + EnterAtcDialog().show(tanMedium, activity, false) { enteredResult -> + result.set(enteredResult) + tanEnteredLatch.countDown() + } } + + try { tanEnteredLatch.await() } catch (ignored: Exception) { } + + result.get() } - - try { tanEnteredLatch.await() } catch (ignored: Exception) { } - - return result.get() + ?: EnterTanGeneratorAtcResult.userDidNotEnterTan() } override fun showTransferMoneyDialog(presenter: BankingPresenter, preselectedBankAccount: BankAccount?, preselectedValues: TransferMoneyData?) { - TransferMoneyDialog().show(activity, preselectedBankAccount, preselectedValues) + activityTracker.currentActivity?.let { activity -> + TransferMoneyDialog().show(activity, preselectedBankAccount, preselectedValues) + } } } \ No newline at end of file diff --git a/fints4javaAndroidApp/src/main/java/net/dankito/banking/fints4java/android/di/BankingComponent.kt b/fints4javaAndroidApp/src/main/java/net/dankito/banking/fints4java/android/di/BankingComponent.kt index 0c2545b3..e28a8137 100644 --- a/fints4javaAndroidApp/src/main/java/net/dankito/banking/fints4java/android/di/BankingComponent.kt +++ b/fints4javaAndroidApp/src/main/java/net/dankito/banking/fints4java/android/di/BankingComponent.kt @@ -2,6 +2,7 @@ package net.dankito.banking.fints4java.android.di import dagger.Component import net.dankito.banking.fints4java.android.MainActivity +import net.dankito.banking.fints4java.android.ui.activities.BaseActivity import net.dankito.banking.fints4java.android.ui.dialogs.AddAccountDialog import net.dankito.banking.fints4java.android.ui.dialogs.EnterTanDialog import net.dankito.banking.fints4java.android.ui.dialogs.TransferMoneyDialog @@ -18,6 +19,8 @@ interface BankingComponent { } + fun inject(baseActivity: BaseActivity) + fun inject(mainActivity: MainActivity) fun inject(homeFragment: HomeFragment) diff --git a/fints4javaAndroidApp/src/main/java/net/dankito/banking/fints4java/android/di/BankingModule.kt b/fints4javaAndroidApp/src/main/java/net/dankito/banking/fints4java/android/di/BankingModule.kt index c148d786..1cb68fda 100644 --- a/fints4javaAndroidApp/src/main/java/net/dankito/banking/fints4java/android/di/BankingModule.kt +++ b/fints4javaAndroidApp/src/main/java/net/dankito/banking/fints4java/android/di/BankingModule.kt @@ -5,6 +5,7 @@ import androidx.appcompat.app.AppCompatActivity import dagger.Module import dagger.Provides import net.dankito.banking.fints4java.android.RouterAndroid +import net.dankito.banking.fints4java.android.ui.util.CurrentActivityTracker import net.dankito.banking.fints4java.android.util.Base64ServiceAndroid import net.dankito.banking.fints4javaBankingClientCreator import net.dankito.banking.persistence.IBankingPersistence @@ -114,8 +115,14 @@ class BankingModule(private val applicationContext: Context) { @Provides @Singleton - fun provideRouter() : IRouter { - return RouterAndroid(mainActivity) + fun provideCurrentActivityTracker() : CurrentActivityTracker { + return CurrentActivityTracker() + } + + @Provides + @Singleton + fun provideRouter(currentActivityTracker: CurrentActivityTracker) : IRouter { + return RouterAndroid(currentActivityTracker) } diff --git a/fints4javaAndroidApp/src/main/java/net/dankito/banking/fints4java/android/ui/activities/BaseActivity.kt b/fints4javaAndroidApp/src/main/java/net/dankito/banking/fints4java/android/ui/activities/BaseActivity.kt new file mode 100644 index 00000000..e7752960 --- /dev/null +++ b/fints4javaAndroidApp/src/main/java/net/dankito/banking/fints4java/android/ui/activities/BaseActivity.kt @@ -0,0 +1,67 @@ +package net.dankito.banking.fints4java.android.ui.activities + +import android.os.Bundle +import androidx.appcompat.app.AppCompatActivity +import net.dankito.banking.fints4java.android.di.BankingComponent +import net.dankito.banking.fints4java.android.ui.util.CurrentActivityTracker +import org.slf4j.LoggerFactory +import javax.inject.Inject + + +abstract class BaseActivity : AppCompatActivity() { + + companion object { + private val log = LoggerFactory.getLogger(BaseActivity::class.java) + } + + + @Inject + protected lateinit var currentActivityTracker: CurrentActivityTracker + + + init { + BankingComponent.component.inject(this) + } + + + override fun onCreate(savedInstanceState: Bundle?) { + log.info("Creating Activity $this") + + super.onCreate(savedInstanceState) + } + + override fun onStart() { + super.onStart() + + currentActivityTracker.currentActivity = this + + log.info("Started Activity $this") + } + + override fun onResume() { + super.onResume() + + currentActivityTracker.currentActivity = this + + log.info("Resumed Activity $this") + } + + override fun onPause() { + log.info("Paused Activity $this") + + super.onPause() + } + + override fun onStop() { + log.info("Stopped Activity $this") + + super.onStop() + } + + override fun onDestroy() { + log.info("Destroyed Activity $this") + + super.onDestroy() + } + +} \ No newline at end of file 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 new file mode 100644 index 00000000..36f12ed1 --- /dev/null +++ b/fints4javaAndroidApp/src/main/java/net/dankito/banking/fints4java/android/ui/util/CurrentActivityTracker.kt @@ -0,0 +1,41 @@ +package net.dankito.banking.fints4java.android.ui.util + +import net.dankito.banking.fints4java.android.ui.activities.BaseActivity +import java.util.* +import java.util.concurrent.CopyOnWriteArrayList +import kotlin.concurrent.schedule + + +open class CurrentActivityTracker { + + var currentActivity: BaseActivity? = null + set(value) { + field = value // TODO: check field != value + + if (value != null) { + callAndClearNextActivitySetListeners(value) + } + } + + + protected val nextActivitySetListeners = CopyOnWriteArrayList<(BaseActivity) -> Unit>() + + open fun addNextActivitySetListener(listener: (BaseActivity) -> Unit) { + synchronized(nextActivitySetListeners) { + nextActivitySetListeners.add(listener) + } + } + + protected open fun callAndClearNextActivitySetListeners(activity: BaseActivity) { + synchronized(nextActivitySetListeners) { + val listenersCopy = ArrayList(nextActivitySetListeners) + + nextActivitySetListeners.clear() + + Timer().schedule(500) { // wait some time till activity is initialized + listenersCopy.forEach { it(activity) } + } + } + } + +} \ No newline at end of file