2021-03-04 19:23:01 +01:00
|
|
|
package info.nightscout.androidaps
|
|
|
|
|
|
|
|
import android.bluetooth.BluetoothDevice
|
|
|
|
import android.content.Intent
|
|
|
|
import android.content.IntentFilter
|
|
|
|
import android.net.ConnectivityManager
|
|
|
|
import android.net.wifi.WifiManager
|
2021-03-28 13:45:27 +02:00
|
|
|
import android.os.Build
|
2021-03-04 19:23:01 +01:00
|
|
|
import com.j256.ormlite.android.apptools.OpenHelperManager
|
|
|
|
import dagger.android.AndroidInjector
|
|
|
|
import dagger.android.DaggerApplication
|
|
|
|
import info.nightscout.androidaps.database.AppRepository
|
2021-03-28 13:45:27 +02:00
|
|
|
import info.nightscout.androidaps.database.entities.TherapyEvent
|
2021-04-10 11:35:38 +02:00
|
|
|
import info.nightscout.androidaps.database.entities.UserEntry
|
2021-03-29 00:31:40 +02:00
|
|
|
import info.nightscout.androidaps.database.transactions.InsertIfNewByTimestampTherapyEventTransaction
|
2021-03-04 19:23:01 +01:00
|
|
|
import info.nightscout.androidaps.database.transactions.VersionChangeTransaction
|
|
|
|
import info.nightscout.androidaps.db.CompatDBHelper
|
|
|
|
import info.nightscout.androidaps.db.DatabaseHelper
|
|
|
|
import info.nightscout.androidaps.db.StaticInjector
|
|
|
|
import info.nightscout.androidaps.dependencyInjection.DaggerAppComponent
|
2021-04-13 23:27:55 +02:00
|
|
|
import info.nightscout.androidaps.interfaces.ConfigBuilder
|
2021-04-14 18:42:12 +02:00
|
|
|
import info.nightscout.androidaps.interfaces.Config
|
2021-03-04 19:23:01 +01:00
|
|
|
import info.nightscout.androidaps.interfaces.PluginBase
|
|
|
|
import info.nightscout.androidaps.logging.AAPSLogger
|
|
|
|
import info.nightscout.androidaps.logging.LTag
|
2021-04-10 11:35:38 +02:00
|
|
|
import info.nightscout.androidaps.logging.UserEntryLogger
|
2021-03-04 19:23:01 +01:00
|
|
|
import info.nightscout.androidaps.plugins.configBuilder.PluginStore
|
|
|
|
import info.nightscout.androidaps.plugins.constraints.versionChecker.VersionCheckerUtils
|
|
|
|
import info.nightscout.androidaps.receivers.BTReceiver
|
|
|
|
import info.nightscout.androidaps.receivers.ChargingStateReceiver
|
|
|
|
import info.nightscout.androidaps.receivers.KeepAliveReceiver.KeepAliveManager
|
|
|
|
import info.nightscout.androidaps.receivers.NetworkChangeReceiver
|
|
|
|
import info.nightscout.androidaps.receivers.TimeDateOrTZChangeReceiver
|
|
|
|
import info.nightscout.androidaps.utils.ActivityMonitor
|
2021-03-28 13:45:27 +02:00
|
|
|
import info.nightscout.androidaps.utils.DateUtil
|
2021-03-04 19:23:01 +01:00
|
|
|
import info.nightscout.androidaps.utils.locale.LocaleHelper.update
|
|
|
|
import info.nightscout.androidaps.utils.sharedPreferences.SP
|
|
|
|
import io.reactivex.disposables.CompositeDisposable
|
2021-03-28 13:45:27 +02:00
|
|
|
import io.reactivex.rxkotlin.plusAssign
|
2021-03-04 19:23:01 +01:00
|
|
|
import net.danlew.android.joda.JodaTimeAndroid
|
|
|
|
import javax.inject.Inject
|
|
|
|
|
|
|
|
class MainApp : DaggerApplication() {
|
|
|
|
|
|
|
|
private val disposable = CompositeDisposable()
|
|
|
|
|
|
|
|
@Inject lateinit var pluginStore: PluginStore
|
|
|
|
@Inject lateinit var aapsLogger: AAPSLogger
|
|
|
|
@Inject lateinit var activityMonitor: ActivityMonitor
|
|
|
|
@Inject lateinit var versionCheckersUtils: VersionCheckerUtils
|
|
|
|
@Inject lateinit var sp: SP
|
|
|
|
@Inject lateinit var config: Config
|
2021-04-13 23:27:55 +02:00
|
|
|
@Inject lateinit var configBuilder: ConfigBuilder
|
2021-03-04 19:23:01 +01:00
|
|
|
@Inject lateinit var keepAliveManager: KeepAliveManager
|
|
|
|
@Inject lateinit var plugins: List<@JvmSuppressWildcards PluginBase>
|
|
|
|
@Inject lateinit var compatDBHelper: CompatDBHelper
|
|
|
|
@Inject lateinit var repository: AppRepository
|
2021-03-28 13:45:27 +02:00
|
|
|
@Inject lateinit var dateUtil: DateUtil
|
2021-03-04 19:23:01 +01:00
|
|
|
@Inject lateinit var staticInjector: StaticInjector// TODO avoid , here fake only to initialize
|
2021-04-10 11:35:38 +02:00
|
|
|
@Inject lateinit var uel: UserEntryLogger
|
2021-03-04 19:23:01 +01:00
|
|
|
|
|
|
|
override fun onCreate() {
|
|
|
|
super.onCreate()
|
|
|
|
aapsLogger.debug("onCreate")
|
|
|
|
update(this)
|
|
|
|
dbHelper = OpenHelperManager.getHelper(this, DatabaseHelper::class.java)
|
|
|
|
|
|
|
|
var gitRemote: String? = BuildConfig.REMOTE
|
|
|
|
var commitHash: String? = BuildConfig.HEAD
|
|
|
|
if (gitRemote?.contains("NoGitSystemAvailable") == true) {
|
|
|
|
gitRemote = null
|
|
|
|
commitHash = null
|
|
|
|
}
|
2021-03-28 13:45:27 +02:00
|
|
|
disposable += repository.runTransaction(VersionChangeTransaction(BuildConfig.VERSION_NAME, BuildConfig.VERSION_CODE, gitRemote, commitHash)).subscribe()
|
2021-04-11 17:58:50 +02:00
|
|
|
disposable += repository.runTransaction(InsertIfNewByTimestampTherapyEventTransaction(timestamp = dateUtil.now(), type = TherapyEvent.Type.NOTE, note = getString(info.nightscout.androidaps.core.R.string.androidaps_start).toString() + " - " + Build.MANUFACTURER + " " + Build.MODEL, glucoseUnit = TherapyEvent.GlucoseUnit.MGDL)).subscribe()
|
2021-03-28 13:45:27 +02:00
|
|
|
disposable += compatDBHelper.dbChangeDisposable()
|
2021-03-04 19:23:01 +01:00
|
|
|
registerActivityLifecycleCallbacks(activityMonitor)
|
|
|
|
JodaTimeAndroid.init(this)
|
|
|
|
aapsLogger.debug("Version: " + BuildConfig.VERSION_NAME)
|
|
|
|
aapsLogger.debug("BuildVersion: " + BuildConfig.BUILDVERSION)
|
|
|
|
aapsLogger.debug("Remote: " + BuildConfig.REMOTE)
|
|
|
|
registerLocalBroadcastReceiver()
|
|
|
|
|
|
|
|
//trigger here to see the new version on app start after an update
|
|
|
|
versionCheckersUtils.triggerCheckVersion()
|
|
|
|
|
|
|
|
// Register all tabs in app here
|
|
|
|
pluginStore.plugins = plugins
|
2021-04-13 23:27:55 +02:00
|
|
|
configBuilder.initialize()
|
2021-03-28 13:45:27 +02:00
|
|
|
keepAliveManager.setAlarm(this)
|
2021-03-04 19:23:01 +01:00
|
|
|
doMigrations()
|
2021-04-10 11:35:38 +02:00
|
|
|
uel.log(UserEntry.Action.START_AAPS, UserEntry.Sources.Aaps)
|
2021-03-04 19:23:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private fun doMigrations() {
|
|
|
|
// set values for different builds
|
|
|
|
if (!sp.contains(R.string.key_ns_alarms)) sp.putBoolean(R.string.key_ns_alarms, config.NSCLIENT)
|
|
|
|
if (!sp.contains(R.string.key_ns_announcements)) sp.putBoolean(R.string.key_ns_announcements, config.NSCLIENT)
|
|
|
|
if (!sp.contains(R.string.key_language)) sp.putString(R.string.key_language, "default")
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun applicationInjector(): AndroidInjector<out DaggerApplication> {
|
|
|
|
return DaggerAppComponent
|
|
|
|
.builder()
|
|
|
|
.application(this)
|
|
|
|
.build()
|
|
|
|
}
|
|
|
|
|
|
|
|
private fun registerLocalBroadcastReceiver() {
|
|
|
|
var filter = IntentFilter()
|
|
|
|
filter.addAction(Intent.ACTION_TIME_CHANGED)
|
|
|
|
filter.addAction(Intent.ACTION_TIMEZONE_CHANGED)
|
|
|
|
registerReceiver(TimeDateOrTZChangeReceiver(), filter)
|
|
|
|
filter = IntentFilter()
|
|
|
|
@Suppress("DEPRECATION")
|
|
|
|
filter.addAction(ConnectivityManager.CONNECTIVITY_ACTION)
|
|
|
|
filter.addAction(WifiManager.NETWORK_STATE_CHANGED_ACTION)
|
|
|
|
filter.addAction(WifiManager.WIFI_STATE_CHANGED_ACTION)
|
|
|
|
registerReceiver(NetworkChangeReceiver(), filter)
|
|
|
|
filter = IntentFilter()
|
|
|
|
filter.addAction(Intent.ACTION_POWER_CONNECTED)
|
|
|
|
filter.addAction(Intent.ACTION_POWER_DISCONNECTED)
|
|
|
|
filter.addAction(Intent.ACTION_BATTERY_CHANGED)
|
|
|
|
registerReceiver(ChargingStateReceiver(), filter)
|
|
|
|
filter = IntentFilter()
|
|
|
|
filter.addAction(BluetoothDevice.ACTION_ACL_CONNECTED)
|
|
|
|
filter.addAction(BluetoothDevice.ACTION_ACL_DISCONNECTED)
|
|
|
|
registerReceiver(BTReceiver(), filter)
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun onTerminate() {
|
|
|
|
aapsLogger.debug(LTag.CORE, "onTerminate")
|
|
|
|
unregisterActivityLifecycleCallbacks(activityMonitor)
|
|
|
|
keepAliveManager.cancelAlarm(this)
|
|
|
|
super.onTerminate()
|
|
|
|
}
|
|
|
|
|
|
|
|
companion object {
|
|
|
|
|
|
|
|
lateinit var dbHelper: DatabaseHelper
|
|
|
|
}
|
|
|
|
}
|