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-10-10 18:05:55 +02:00
|
|
|
import com.uber.rxdogtag.RxDogTag
|
2021-03-04 19:23:01 +01:00
|
|
|
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.dependencyInjection.DaggerAppComponent
|
2021-10-20 22:54:32 +02:00
|
|
|
import info.nightscout.androidaps.di.StaticInjector
|
2021-04-14 18:42:12 +02:00
|
|
|
import info.nightscout.androidaps.interfaces.Config
|
2021-05-29 23:38:19 +02:00
|
|
|
import info.nightscout.androidaps.interfaces.ConfigBuilder
|
2021-03-04 19:23:01 +01:00
|
|
|
import info.nightscout.androidaps.interfaces.PluginBase
|
2021-12-10 15:19:19 +01:00
|
|
|
import info.nightscout.shared.logging.AAPSLogger
|
|
|
|
import info.nightscout.shared.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
|
2021-11-28 18:20:52 +01:00
|
|
|
import info.nightscout.androidaps.plugins.general.overview.notifications.Notification
|
|
|
|
import info.nightscout.androidaps.plugins.general.overview.notifications.NotificationStore
|
2021-03-04 19:23:01 +01:00
|
|
|
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
|
2021-10-26 13:41:48 +02:00
|
|
|
import info.nightscout.androidaps.services.AlarmSoundServiceHelper
|
2021-03-04 19:23:01 +01:00
|
|
|
import info.nightscout.androidaps.utils.ActivityMonitor
|
2021-03-28 13:45:27 +02:00
|
|
|
import info.nightscout.androidaps.utils.DateUtil
|
2021-11-28 18:20:52 +01:00
|
|
|
import info.nightscout.androidaps.utils.buildHelper.BuildHelper
|
2021-10-23 21:08:31 +02:00
|
|
|
import info.nightscout.androidaps.utils.locale.LocaleHelper
|
2021-09-20 14:13:56 +02:00
|
|
|
import info.nightscout.androidaps.utils.protection.PasswordCheck
|
2021-12-10 12:17:03 +01:00
|
|
|
import info.nightscout.shared.sharedPreferences.SP
|
2021-03-04 19:23:01 +01:00
|
|
|
import io.reactivex.disposables.CompositeDisposable
|
2021-10-20 22:54:32 +02:00
|
|
|
import io.reactivex.exceptions.UndeliverableException
|
|
|
|
import io.reactivex.plugins.RxJavaPlugins
|
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
|
2021-10-20 22:54:32 +02:00
|
|
|
import java.io.IOException
|
|
|
|
import java.net.SocketException
|
2021-03-04 19:23:01 +01:00
|
|
|
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-11-28 18:20:52 +01:00
|
|
|
@Inject lateinit var buildHelper: BuildHelper
|
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-09-20 14:13:56 +02:00
|
|
|
@Inject lateinit var passwordCheck: PasswordCheck
|
2021-10-26 13:41:48 +02:00
|
|
|
@Inject lateinit var alarmSoundServiceHelper: AlarmSoundServiceHelper
|
2021-11-28 18:20:52 +01:00
|
|
|
@Inject lateinit var notificationStore: NotificationStore
|
2021-03-04 19:23:01 +01:00
|
|
|
|
|
|
|
override fun onCreate() {
|
|
|
|
super.onCreate()
|
|
|
|
aapsLogger.debug("onCreate")
|
2021-10-10 18:05:55 +02:00
|
|
|
RxDogTag.install()
|
2021-10-20 22:54:32 +02:00
|
|
|
setRxErrorHandler()
|
2021-10-23 21:08:31 +02:00
|
|
|
LocaleHelper.update(this)
|
2021-03-04 19:23:01 +01:00
|
|
|
|
|
|
|
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-10-23 21:08:31 +02:00
|
|
|
if (sp.getBoolean(R.string.key_ns_logappstartedevent, config.APS))
|
|
|
|
disposable += repository
|
|
|
|
.runTransaction(
|
|
|
|
InsertIfNewByTimestampTherapyEventTransaction(
|
|
|
|
timestamp = dateUtil.now(),
|
|
|
|
type = TherapyEvent.Type.NOTE,
|
|
|
|
note = getString(info.nightscout.androidaps.core.R.string.androidaps_start) + " - " + 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()
|
|
|
|
|
2021-11-28 18:20:52 +01:00
|
|
|
// trigger here to see the new version on app start after an update
|
2021-03-04 19:23:01 +01:00
|
|
|
versionCheckersUtils.triggerCheckVersion()
|
2021-11-28 18:20:52 +01:00
|
|
|
// check if identification is set
|
|
|
|
if (buildHelper.isDev() && sp.getStringOrNull(R.string.key_email_for_crash_report, null).isNullOrBlank())
|
|
|
|
notificationStore.add(Notification(Notification.IDENTIFICATION_NOT_SET, getString(R.string.identification_not_set), Notification.INFO))
|
2021-03-04 19:23:01 +01:00
|
|
|
|
|
|
|
// 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-09-20 14:13:56 +02:00
|
|
|
passwordCheck.passwordResetCheck(this)
|
2021-03-04 19:23:01 +01:00
|
|
|
}
|
|
|
|
|
2021-10-20 22:54:32 +02:00
|
|
|
private fun setRxErrorHandler() {
|
|
|
|
RxJavaPlugins.setErrorHandler { t: Throwable ->
|
|
|
|
var e = t
|
|
|
|
if (e is UndeliverableException) {
|
|
|
|
e = e.cause!!
|
|
|
|
}
|
|
|
|
if (e is IOException || e is SocketException) {
|
|
|
|
// fine, irrelevant network problem or API that throws on cancellation
|
|
|
|
return@setErrorHandler
|
|
|
|
}
|
|
|
|
if (e is InterruptedException) {
|
|
|
|
// fine, some blocking code was interrupted by a dispose call
|
|
|
|
return@setErrorHandler
|
|
|
|
}
|
|
|
|
if (e is NullPointerException || e is IllegalArgumentException) {
|
|
|
|
// that's likely a bug in the application
|
|
|
|
Thread.currentThread().uncaughtExceptionHandler?.uncaughtException(Thread.currentThread(), e)
|
|
|
|
return@setErrorHandler
|
|
|
|
}
|
|
|
|
if (e is IllegalStateException) {
|
|
|
|
// that's a bug in RxJava or in a custom operator
|
|
|
|
Thread.currentThread().uncaughtExceptionHandler?.uncaughtException(Thread.currentThread(), e)
|
|
|
|
return@setErrorHandler
|
|
|
|
}
|
|
|
|
aapsLogger.warn(LTag.CORE, "Undeliverable exception received, not sure what to do", e)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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)
|
2021-10-26 13:41:48 +02:00
|
|
|
alarmSoundServiceHelper.stopService(this)
|
2021-03-04 19:23:01 +01:00
|
|
|
super.onTerminate()
|
|
|
|
}
|
2021-10-10 18:05:55 +02:00
|
|
|
}
|