-
Notifications
You must be signed in to change notification settings - Fork 39
/
RuuviScannerApplication.kt
113 lines (95 loc) · 4.19 KB
/
RuuviScannerApplication.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package com.ruuvi.station.app
import android.app.Application
import android.bluetooth.BluetoothAdapter
import android.content.Context
import android.content.IntentFilter
import android.os.PowerManager
import com.facebook.stetho.Stetho
import com.raizlabs.android.dbflow.config.FlowManager
import com.ruuvi.station.BuildConfig
import com.ruuvi.station.app.di.AppInjectionModules
import com.ruuvi.station.app.preferences.PreferencesRepository
import com.ruuvi.station.bluetooth.DefaultOnTagFoundListener
import com.ruuvi.station.bluetooth.domain.BluetoothStateReceiver
import com.ruuvi.station.feature.data.FeatureFlag
import com.ruuvi.station.feature.domain.RuntimeBehavior
import com.ruuvi.station.feature.provider.RuntimeFeatureFlagProvider
import com.ruuvi.station.network.domain.NetworkDataSyncInteractor
import com.ruuvi.station.network.domain.RuuviNetworkInteractor
import com.ruuvi.station.util.Foreground
import com.ruuvi.station.util.ForegroundListener
import com.ruuvi.station.util.ReleaseTree
import com.ruuvi.station.util.extensions.diffGreaterThan
import com.ruuvi.station.util.test.FakeScanResultsSender
import kotlinx.coroutines.ExperimentalCoroutinesApi
import org.kodein.di.Kodein
import org.kodein.di.KodeinAware
import org.kodein.di.conf.ConfigurableKodein
import org.kodein.di.generic.bind
import org.kodein.di.generic.instance
import org.kodein.di.generic.singleton
import timber.log.Timber
import java.util.*
@ExperimentalCoroutinesApi
class RuuviScannerApplication : Application(), KodeinAware {
override val kodein = ConfigurableKodein()
val defaultOnTagFoundListener: DefaultOnTagFoundListener by instance()
private val fakesSender: FakeScanResultsSender by instance()
private val bluetoothReceiver: BluetoothStateReceiver by instance()
private val foreground: Foreground by instance()
private val networkInteractor: RuuviNetworkInteractor by instance()
private val networkDataSyncInteractor: NetworkDataSyncInteractor by instance()
private val preferencesRepository: PreferencesRepository by instance()
private val runtimeFeatureFlagProvider: RuntimeFeatureFlagProvider by instance()
private val runtimeBehavior: RuntimeBehavior by instance()
private var isInForeground: Boolean = false
private val listener: ForegroundListener = object : ForegroundListener {
override fun onBecameForeground() {
isInForeground = true
defaultOnTagFoundListener.isForeground = true
networkDataSyncInteractor.startAutoRefresh()
runtimeBehavior.refreshFeatureFlags()
}
override fun onBecameBackground() {
isInForeground = false
defaultOnTagFoundListener.isForeground = false
networkDataSyncInteractor.stopAutoRefresh()
}
}
override fun onCreate() {
super.onCreate()
if (BuildConfig.DEBUG) {
Timber.plant(Timber.DebugTree())
} else if (BuildConfig.FILE_LOGS_ENABLED) {
Timber.plant(ReleaseTree())
}
setupDependencyInjection()
//Lingver.init(this, Locale(preferencesRepository.getLocale()))
FlowManager.init(this)
if (BuildConfig.DEBUG) {
Stetho.initializeWithDefaults(this)
//turn on for debug if you don't have real ruuvi tag
//fakesSender.startSendFakes()
}
registerReceiver(bluetoothReceiver, IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED))
defaultOnTagFoundListener.isForeground = isInForeground
foreground.addListener(listener)
setupExperimentalFeatures()
}
private fun setupDependencyInjection() {
kodein.apply {
addImport(AppInjectionModules.module)
addImport(Kodein.Module(javaClass.name) {
bind<Application>() with singleton { this@RuuviScannerApplication }
bind<PowerManager>() with singleton {
this@RuuviScannerApplication.getSystemService(Context.POWER_SERVICE) as PowerManager
}
})
}
}
private fun setupExperimentalFeatures() {
if (networkInteractor.signedIn) {
runtimeFeatureFlagProvider.setFeatureEnabled(FeatureFlag.RUUVI_NETWORK, true)
}
}
}