fix crashes

This commit is contained in:
Milos Kozak 2022-09-22 15:45:42 +02:00
parent 28c617aea2
commit 7543f3f118
9 changed files with 81 additions and 35 deletions

View file

@ -2,7 +2,6 @@ package info.nightscout.androidaps.plugins.general.dataBroadcaster
import android.content.Context import android.content.Context
import android.content.Intent import android.content.Intent
import android.content.pm.PackageManager
import android.content.pm.ResolveInfo import android.content.pm.ResolveInfo
import android.os.Bundle import android.os.Bundle
import dagger.android.HasAndroidInjector import dagger.android.HasAndroidInjector
@ -10,6 +9,7 @@ import info.nightscout.androidaps.R
import info.nightscout.androidaps.events.Event import info.nightscout.androidaps.events.Event
import info.nightscout.androidaps.events.EventAutosensCalculationFinished import info.nightscout.androidaps.events.EventAutosensCalculationFinished
import info.nightscout.androidaps.extensions.durationInMinutes import info.nightscout.androidaps.extensions.durationInMinutes
import info.nightscout.androidaps.extensions.safeQueryBroadcastReceivers
import info.nightscout.androidaps.extensions.toStringFull import info.nightscout.androidaps.extensions.toStringFull
import info.nightscout.androidaps.interfaces.* import info.nightscout.androidaps.interfaces.*
import info.nightscout.androidaps.plugins.aps.events.EventOpenAPSUpdateGui import info.nightscout.androidaps.plugins.aps.events.EventOpenAPSUpdateGui
@ -18,16 +18,16 @@ import info.nightscout.androidaps.plugins.general.nsclient.data.DeviceStatusData
import info.nightscout.androidaps.plugins.general.nsclient.data.NSDeviceStatus import info.nightscout.androidaps.plugins.general.nsclient.data.NSDeviceStatus
import info.nightscout.androidaps.plugins.general.overview.events.EventOverviewBolusProgress import info.nightscout.androidaps.plugins.general.overview.events.EventOverviewBolusProgress
import info.nightscout.androidaps.plugins.iob.iobCobCalculator.GlucoseStatusProvider import info.nightscout.androidaps.plugins.iob.iobCobCalculator.GlucoseStatusProvider
import info.nightscout.androidaps.receivers.ReceiverStatusStore
import info.nightscout.androidaps.receivers.Intents import info.nightscout.androidaps.receivers.Intents
import info.nightscout.androidaps.receivers.ReceiverStatusStore
import info.nightscout.androidaps.utils.DateUtil import info.nightscout.androidaps.utils.DateUtil
import info.nightscout.androidaps.utils.DefaultValueHelper import info.nightscout.androidaps.utils.DefaultValueHelper
import info.nightscout.androidaps.utils.FabricPrivacy import info.nightscout.androidaps.utils.FabricPrivacy
import info.nightscout.androidaps.interfaces.ResourceHelper
import info.nightscout.androidaps.utils.rx.AapsSchedulers import info.nightscout.androidaps.utils.rx.AapsSchedulers
import info.nightscout.shared.logging.AAPSLogger import info.nightscout.shared.logging.AAPSLogger
import info.nightscout.shared.logging.LTag import info.nightscout.shared.logging.LTag
import io.reactivex.rxjava3.disposables.CompositeDisposable import io.reactivex.rxjava3.disposables.CompositeDisposable
import io.reactivex.rxjava3.kotlin.plusAssign
import javax.inject.Inject import javax.inject.Inject
import javax.inject.Singleton import javax.inject.Singleton
@ -65,21 +65,18 @@ class DataBroadcastPlugin @Inject constructor(
private val disposable = CompositeDisposable() private val disposable = CompositeDisposable()
override fun onStart() { override fun onStart() {
super.onStart() super.onStart()
disposable.add(rxBus disposable += rxBus
.toObservable(EventOpenAPSUpdateGui::class.java) .toObservable(EventOpenAPSUpdateGui::class.java)
.observeOn(aapsSchedulers.io) .observeOn(aapsSchedulers.io)
.subscribe({ sendData(it) }, fabricPrivacy::logException) .subscribe({ sendData(it) }, fabricPrivacy::logException)
) disposable += rxBus
disposable.add(rxBus
.toObservable(EventAutosensCalculationFinished::class.java) .toObservable(EventAutosensCalculationFinished::class.java)
.observeOn(aapsSchedulers.io) .observeOn(aapsSchedulers.io)
.subscribe({ sendData(it) }, fabricPrivacy::logException) .subscribe({ sendData(it) }, fabricPrivacy::logException)
) disposable += rxBus
disposable.add(rxBus
.toObservable(EventOverviewBolusProgress::class.java) .toObservable(EventOverviewBolusProgress::class.java)
.observeOn(aapsSchedulers.io) .observeOn(aapsSchedulers.io)
.subscribe({ sendData(it) }, fabricPrivacy::logException) .subscribe({ sendData(it) }, fabricPrivacy::logException)
)
} }
override fun onStop() { override fun onStop() {
@ -187,7 +184,7 @@ class DataBroadcastPlugin @Inject constructor(
} }
private fun sendBroadcast(intent: Intent) { private fun sendBroadcast(intent: Intent) {
val receivers: List<ResolveInfo> = context.packageManager.queryBroadcastReceivers(intent, PackageManager.ResolveInfoFlags.of(0)) val receivers: List<ResolveInfo> = context.packageManager.safeQueryBroadcastReceivers(intent, 0)
for (resolveInfo in receivers) for (resolveInfo in receivers)
resolveInfo.activityInfo.packageName?.let { resolveInfo.activityInfo.packageName?.let {
intent.setPackage(it) intent.setPackage(it)

View file

@ -19,13 +19,19 @@ import info.nightscout.androidaps.database.entities.ValueWithUnit
import info.nightscout.androidaps.database.transactions.CgmSourceTransaction import info.nightscout.androidaps.database.transactions.CgmSourceTransaction
import info.nightscout.androidaps.database.transactions.InvalidateGlucoseValueTransaction import info.nightscout.androidaps.database.transactions.InvalidateGlucoseValueTransaction
import info.nightscout.androidaps.extensions.fromConstant import info.nightscout.androidaps.extensions.fromConstant
import info.nightscout.androidaps.interfaces.* import info.nightscout.androidaps.extensions.safeGetInstalledPackages
import info.nightscout.androidaps.interfaces.BgSource
import info.nightscout.androidaps.interfaces.Config
import info.nightscout.androidaps.interfaces.PluginBase
import info.nightscout.androidaps.interfaces.PluginDescription
import info.nightscout.androidaps.interfaces.PluginType
import info.nightscout.androidaps.interfaces.Profile
import info.nightscout.androidaps.interfaces.ResourceHelper
import info.nightscout.androidaps.logging.UserEntryLogger import info.nightscout.androidaps.logging.UserEntryLogger
import info.nightscout.androidaps.receivers.DataWorker import info.nightscout.androidaps.receivers.DataWorker
import info.nightscout.androidaps.utils.DateUtil import info.nightscout.androidaps.utils.DateUtil
import info.nightscout.androidaps.utils.T import info.nightscout.androidaps.utils.T
import info.nightscout.androidaps.utils.XDripBroadcast import info.nightscout.androidaps.utils.XDripBroadcast
import info.nightscout.androidaps.interfaces.ResourceHelper
import info.nightscout.shared.logging.AAPSLogger import info.nightscout.shared.logging.AAPSLogger
import info.nightscout.shared.logging.LTag import info.nightscout.shared.logging.LTag
import info.nightscout.shared.sharedPreferences.SP import info.nightscout.shared.sharedPreferences.SP
@ -244,7 +250,7 @@ class DexcomPlugin @Inject constructor(
fun findDexcomPackageName(): String? { fun findDexcomPackageName(): String? {
val packageManager = context.packageManager val packageManager = context.packageManager
for (packageInfo in packageManager.getInstalledPackages(PackageManager.PackageInfoFlags.of(0))) { for (packageInfo in packageManager.safeGetInstalledPackages(0)) {
if (PACKAGE_NAMES.contains(packageInfo.packageName)) return packageInfo.packageName if (PACKAGE_NAMES.contains(packageInfo.packageName)) return packageInfo.packageName
} }
return null return null

View file

@ -15,6 +15,7 @@ import info.nightscout.androidaps.database.entities.TherapyEvent
import info.nightscout.androidaps.database.entities.UserEntry import info.nightscout.androidaps.database.entities.UserEntry
import info.nightscout.androidaps.database.entities.ValueWithUnit import info.nightscout.androidaps.database.entities.ValueWithUnit
import info.nightscout.androidaps.database.transactions.CgmSourceTransaction import info.nightscout.androidaps.database.transactions.CgmSourceTransaction
import info.nightscout.androidaps.extensions.safeGetInstalledPackages
import info.nightscout.androidaps.interfaces.BgSource import info.nightscout.androidaps.interfaces.BgSource
import info.nightscout.androidaps.interfaces.PluginBase import info.nightscout.androidaps.interfaces.PluginBase
import info.nightscout.androidaps.interfaces.PluginDescription import info.nightscout.androidaps.interfaces.PluginDescription
@ -91,7 +92,7 @@ class IntelligoPlugin @Inject constructor(
private fun handleNewData() { private fun handleNewData() {
if (!isEnabled()) return if (!isEnabled()) return
for (pack in context.packageManager.getInstalledPackages(PackageManager.PackageInfoFlags.of(PackageManager.GET_PROVIDERS.toLong()))) { for (pack in context.packageManager.safeGetInstalledPackages(PackageManager.GET_PROVIDERS)) {
val providers = pack.providers val providers = pack.providers
if (providers != null) { if (providers != null) {
for (provider in providers) { for (provider in providers) {
@ -181,8 +182,10 @@ class IntelligoPlugin @Inject constructor(
glucoseValue.sourceSensor == GlucoseValue.SourceSensor.INTELLIGO_NATIVE && sp.getBoolean(R.string.key_dexcomg5_nsupload, false) glucoseValue.sourceSensor == GlucoseValue.SourceSensor.INTELLIGO_NATIVE && sp.getBoolean(R.string.key_dexcomg5_nsupload, false)
companion object { companion object {
@Suppress("SpellCheckingInspection") @Suppress("SpellCheckingInspection")
const val AUTHORITY = "alexpr.co.uk.infinivocgm.intelligo.cgm_db.CgmExternalProvider" const val AUTHORITY = "alexpr.co.uk.infinivocgm.intelligo.cgm_db.CgmExternalProvider"
//const val AUTHORITY = "alexpr.co.uk.infinivocgm.cgm_db.CgmExternalProvider/" //const val AUTHORITY = "alexpr.co.uk.infinivocgm.cgm_db.CgmExternalProvider/"
const val TABLE_NAME = "CgmReading" const val TABLE_NAME = "CgmReading"
const val INTERVAL = 180000L // 3 min const val INTERVAL = 180000L // 3 min

View file

@ -2,11 +2,11 @@ package info.nightscout.androidaps.utils
import android.content.Context import android.content.Context
import android.content.Intent import android.content.Intent
import android.content.pm.PackageManager
import android.os.Bundle import android.os.Bundle
import info.nightscout.androidaps.R import info.nightscout.androidaps.R
import info.nightscout.androidaps.annotations.OpenForTesting import info.nightscout.androidaps.annotations.OpenForTesting
import info.nightscout.androidaps.database.entities.GlucoseValue import info.nightscout.androidaps.database.entities.GlucoseValue
import info.nightscout.androidaps.extensions.safeQueryBroadcastReceivers
import info.nightscout.androidaps.interfaces.GlucoseUnit import info.nightscout.androidaps.interfaces.GlucoseUnit
import info.nightscout.androidaps.interfaces.ProfileFunction import info.nightscout.androidaps.interfaces.ProfileFunction
import info.nightscout.androidaps.interfaces.ResourceHelper import info.nightscout.androidaps.interfaces.ResourceHelper
@ -42,8 +42,8 @@ class XDripBroadcast @Inject constructor(
intent.putExtras(bundle) intent.putExtras(bundle)
intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES) intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES)
context.sendBroadcast(intent) context.sendBroadcast(intent)
val q = context.packageManager.queryBroadcastReceivers(intent, PackageManager.ResolveInfoFlags.of(0)) val q = context.packageManager.safeQueryBroadcastReceivers(intent, 0)
return if (q.size < 1) { return if (q.isEmpty()) {
ToastUtils.errorToast(context, R.string.xdripnotinstalled) ToastUtils.errorToast(context, R.string.xdripnotinstalled)
aapsLogger.debug(rh.gs(R.string.xdripnotinstalled)) aapsLogger.debug(rh.gs(R.string.xdripnotinstalled))
false false
@ -75,8 +75,8 @@ class XDripBroadcast @Inject constructor(
val intent = Intent(Intents.XDRIP_PLUS_NS_EMULATOR) val intent = Intent(Intents.XDRIP_PLUS_NS_EMULATOR)
intent.putExtras(bundle).addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES) intent.putExtras(bundle).addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES)
context.sendBroadcast(intent) context.sendBroadcast(intent)
val receivers = context.packageManager.queryBroadcastReceivers(intent, PackageManager.ResolveInfoFlags.of(0)) val receivers = context.packageManager.safeQueryBroadcastReceivers(intent, 0)
if (receivers.size < 1) { if (receivers.isEmpty()) {
//NSUpload.log.debug("No xDrip receivers found. ") //NSUpload.log.debug("No xDrip receivers found. ")
aapsLogger.debug(LTag.BGSOURCE, "No xDrip receivers found.") aapsLogger.debug(LTag.BGSOURCE, "No xDrip receivers found.")
} else { } else {
@ -151,7 +151,7 @@ class XDripBroadcast @Inject constructor(
} }
private fun broadcast(intent: Intent) { private fun broadcast(intent: Intent) {
context.packageManager.queryBroadcastReceivers(intent, PackageManager.ResolveInfoFlags.of(0)).forEach { resolveInfo -> context.packageManager.safeQueryBroadcastReceivers(intent, 0).forEach { resolveInfo ->
resolveInfo.activityInfo.packageName?.let { resolveInfo.activityInfo.packageName?.let {
intent.setPackage(it) intent.setPackage(it)
context.sendBroadcast(intent) context.sendBroadcast(intent)

View file

@ -1,6 +1,7 @@
package info.nightscout.androidaps.extensions package info.nightscout.androidaps.extensions
import android.content.Intent import android.content.Intent
import java.io.Serializable
/** /**
* Safe version of getParcelableExtra depending on Android version running * Safe version of getParcelableExtra depending on Android version running
@ -8,3 +9,11 @@ import android.content.Intent
fun <T> Intent.safeGetParcelableExtra(name: String?, clazz: Class<T>): T? = fun <T> Intent.safeGetParcelableExtra(name: String?, clazz: Class<T>): T? =
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.TIRAMISU) getParcelableExtra(name, clazz) if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.TIRAMISU) getParcelableExtra(name, clazz)
else @Suppress("DEPRECATION") getParcelableExtra(name) else @Suppress("DEPRECATION") getParcelableExtra(name)
/**
* Safe version of getSerializableExtra depending on Android version running
*/
@Suppress("UNCHECKED_CAST")
fun <T : Serializable?> Intent.safeGetSerializableExtra(name: String?, clazz: Class<T>): T? =
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.TIRAMISU) getSerializableExtra(name, clazz)
else @Suppress("DEPRECATION") (getSerializableExtra(name) as T?)

View file

@ -41,6 +41,7 @@ import info.nightscout.androidaps.diaconn.pumplog.LOG_TB_START_V3
import info.nightscout.androidaps.diaconn.pumplog.LOG_TB_STOP_V3 import info.nightscout.androidaps.diaconn.pumplog.LOG_TB_STOP_V3
import info.nightscout.androidaps.diaconn.pumplog.PumplogUtil import info.nightscout.androidaps.diaconn.pumplog.PumplogUtil
import info.nightscout.androidaps.events.EventPumpStatusChanged import info.nightscout.androidaps.events.EventPumpStatusChanged
import info.nightscout.androidaps.extensions.safeGetPackageInfo
import info.nightscout.androidaps.interfaces.ActivePlugin import info.nightscout.androidaps.interfaces.ActivePlugin
import info.nightscout.androidaps.interfaces.PumpDescription import info.nightscout.androidaps.interfaces.PumpDescription
import info.nightscout.androidaps.interfaces.PumpSync import info.nightscout.androidaps.interfaces.PumpSync
@ -911,7 +912,8 @@ class BigLogInquireResponsePacket(
val api = retrofit?.create(DiaconnApiService::class.java) val api = retrofit?.create(DiaconnApiService::class.java)
val pumpLogDto = PumpLogDto( val pumpLogDto = PumpLogDto(
app_uid = appUid, app_uid = appUid,
app_version = context.packageManager.getPackageInfo(context.packageName, PackageManager.PackageInfoFlags.of(0)).versionName, app_version = context.packageManager.safeGetPackageInfo(context.packageName, 0)
.versionName,
pump_uid = diaconnG8Pump.pumpUid, pump_uid = diaconnG8Pump.pumpUid,
pump_version = diaconnG8Pump.pumpVersion, pump_version = diaconnG8Pump.pumpVersion,
incarnation_num = diaconnG8Pump.pumpIncarnationNum, incarnation_num = diaconnG8Pump.pumpIncarnationNum,

View file

@ -2,6 +2,7 @@ package info.nightscout.androidaps.plugins.pump.omnipod.common.ui.wizard.activat
import android.os.Bundle import android.os.Bundle
import androidx.annotation.IdRes import androidx.annotation.IdRes
import info.nightscout.androidaps.extensions.safeGetSerializableExtra
import info.nightscout.androidaps.plugins.pump.omnipod.common.R import info.nightscout.androidaps.plugins.pump.omnipod.common.R
import info.nightscout.androidaps.plugins.pump.omnipod.common.ui.wizard.common.activity.OmnipodWizardActivityBase import info.nightscout.androidaps.plugins.pump.omnipod.common.ui.wizard.common.activity.OmnipodWizardActivityBase
@ -26,7 +27,7 @@ abstract class PodActivationWizardActivity : OmnipodWizardActivityBase() {
setContentView(R.layout.omnipod_common_pod_activation_wizard_activity) setContentView(R.layout.omnipod_common_pod_activation_wizard_activity)
startDestination = savedInstanceState?.getInt(KEY_START_DESTINATION, R.id.startPodActivationFragment) startDestination = savedInstanceState?.getInt(KEY_START_DESTINATION, R.id.startPodActivationFragment)
?: if (intent.getSerializableExtra(KEY_TYPE, Type::class.java) == Type.LONG) { ?: if (intent.safeGetSerializableExtra(KEY_TYPE, Type::class.java) == Type.LONG) {
R.id.startPodActivationFragment R.id.startPodActivationFragment
} else { } else {
R.id.attachPodFragment R.id.attachPodFragment

View file

@ -0,0 +1,28 @@
package info.nightscout.androidaps.extensions
import android.content.Intent
import android.content.pm.PackageInfo
import android.content.pm.PackageManager
import android.content.pm.ResolveInfo
/**
* Safe version of getInstalledPackages depending on Android version running
*/
fun PackageManager.safeGetInstalledPackages(flags: Int): List<PackageInfo> =
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.TIRAMISU) getInstalledPackages(PackageManager.PackageInfoFlags.of(flags.toLong()))
else @Suppress("DEPRECATION") getInstalledPackages(flags)
/**
* Safe version of queryBroadcastReceivers depending on Android version running
*/
fun PackageManager.safeQueryBroadcastReceivers(intent: Intent, flags: Int): List<ResolveInfo> =
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.TIRAMISU) queryBroadcastReceivers(intent, PackageManager.ResolveInfoFlags.of(flags.toLong()))
else @Suppress("DEPRECATION") queryBroadcastReceivers(intent, flags)
/**
* Safe version of getPackageInfo depending on Android version running
*/
@Throws(PackageManager.NameNotFoundException::class)
fun PackageManager.safeGetPackageInfo(packageName: String, flags: Int): PackageInfo =
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.TIRAMISU) getPackageInfo(packageName, PackageManager.PackageInfoFlags.of(flags.toLong()))
else @Suppress("DEPRECATION") getPackageInfo(packageName, flags)

View file

@ -2,13 +2,13 @@ package info.nightscout.androidaps.interaction.actions
import android.content.Intent import android.content.Intent
import android.content.pm.PackageManager import android.content.pm.PackageManager
import android.content.pm.PackageManager.PackageInfoFlags
import android.os.Bundle import android.os.Bundle
import android.util.Log import android.util.Log
import android.widget.Toast import android.widget.Toast
import dagger.android.DaggerActivity import dagger.android.DaggerActivity
import info.nightscout.androidaps.R import info.nightscout.androidaps.R
import info.nightscout.androidaps.events.EventWearToMobile import info.nightscout.androidaps.events.EventWearToMobile
import info.nightscout.androidaps.extensions.safeGetPackageInfo
import info.nightscout.androidaps.plugins.bus.RxBus import info.nightscout.androidaps.plugins.bus.RxBus
import info.nightscout.shared.weardata.EventData import info.nightscout.shared.weardata.EventData
import javax.inject.Inject import javax.inject.Inject
@ -32,7 +32,7 @@ class QuickSnoozeActivity : DaggerActivity() {
val i = Intent() val i = Intent()
i.setClassName(xDripPackageName, "$xDripPackageName.QuickSnooze") i.setClassName(xDripPackageName, "$xDripPackageName.QuickSnooze")
startActivity(i) startActivity(i)
} catch (e : Exception) { } catch (e: Exception) {
Log.e("WEAR", "failed to snooze xDrip: ", e) Log.e("WEAR", "failed to snooze xDrip: ", e)
} }
} else { } else {
@ -45,7 +45,7 @@ class QuickSnoozeActivity : DaggerActivity() {
@Suppress("SameParameterValue") @Suppress("SameParameterValue")
private fun isPackageExisted(targetPackage: String): Boolean { private fun isPackageExisted(targetPackage: String): Boolean {
try { try {
packageManager.getPackageInfo(targetPackage, PackageInfoFlags.of(0)) packageManager.safeGetPackageInfo(targetPackage, 0)
} catch (e: PackageManager.NameNotFoundException) { } catch (e: PackageManager.NameNotFoundException) {
return false return false
} }