diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/nsclient/NSClientAddUpdateWorker.kt b/app/src/main/java/info/nightscout/androidaps/plugins/general/nsclient/NSClientAddUpdateWorker.kt index 259c62d9f5..8ddd44b31a 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/nsclient/NSClientAddUpdateWorker.kt +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/nsclient/NSClientAddUpdateWorker.kt @@ -59,7 +59,8 @@ class NSClientAddUpdateWorker( for (i in 0 until treatments.length()) { var json = treatments.getJSONObject(i) - // new DB model + aapsLogger.debug(LTag.DATABASE, "Received NS treatment: $json") + val insulin = JsonHelper.safeGetDouble(json, "insulin") val carbs = JsonHelper.safeGetDouble(json, "carbs") var eventType = JsonHelper.safeGetString(json, "eventType") diff --git a/app/src/main/java/info/nightscout/androidaps/queue/CommandQueueImplementation.kt b/app/src/main/java/info/nightscout/androidaps/queue/CommandQueueImplementation.kt index f8f47fd80a..d46b5347ad 100644 --- a/app/src/main/java/info/nightscout/androidaps/queue/CommandQueueImplementation.kt +++ b/app/src/main/java/info/nightscout/androidaps/queue/CommandQueueImplementation.kt @@ -33,6 +33,7 @@ import info.nightscout.androidaps.plugins.general.overview.events.EventNewNotifi import info.nightscout.androidaps.plugins.general.overview.notifications.Notification import info.nightscout.androidaps.queue.commands.* import info.nightscout.androidaps.queue.commands.Command.CommandType +import info.nightscout.androidaps.utils.AndroidPermission import info.nightscout.androidaps.utils.DateUtil import info.nightscout.androidaps.utils.FabricPrivacy import info.nightscout.androidaps.utils.HtmlHelper @@ -64,7 +65,8 @@ class CommandQueueImplementation @Inject constructor( private val dateUtil: DateUtil, private val repository: AppRepository, private val fabricPrivacy: FabricPrivacy, - private val config: Config + private val config: Config, + private val androidPermission: AndroidPermission ) : CommandQueue { private val disposable = CompositeDisposable() @@ -177,7 +179,7 @@ class CommandQueueImplementation @Inject constructor( @Synchronized fun notifyAboutNewCommand() { waitForFinishedThread() if (thread == null || thread!!.state == Thread.State.TERMINATED) { - thread = QueueThread(this, context, aapsLogger, rxBus, activePlugin, rh, sp) + thread = QueueThread(this, context, aapsLogger, rxBus, activePlugin, rh, sp, androidPermission) thread!!.start() aapsLogger.debug(LTag.PUMPQUEUE, "Starting new thread") } else { @@ -199,7 +201,7 @@ class CommandQueueImplementation @Inject constructor( val tempCommandQueue = CommandQueueImplementation( injector, aapsLogger, rxBus, aapsSchedulers, rh, constraintChecker, profileFunction, activePlugin, context, sp, - buildHelper, dateUtil, repository, fabricPrivacy, config + buildHelper, dateUtil, repository, fabricPrivacy, config, androidPermission ) tempCommandQueue.readStatus(reason, callback) tempCommandQueue.disposable.clear() diff --git a/app/src/main/java/info/nightscout/androidaps/queue/QueueThread.kt b/app/src/main/java/info/nightscout/androidaps/queue/QueueThread.kt index f536e70625..22f507f1af 100644 --- a/app/src/main/java/info/nightscout/androidaps/queue/QueueThread.kt +++ b/app/src/main/java/info/nightscout/androidaps/queue/QueueThread.kt @@ -2,6 +2,7 @@ package info.nightscout.androidaps.queue import android.bluetooth.BluetoothManager import android.content.Context +import android.os.Build import android.os.PowerManager import android.os.SystemClock import info.nightscout.androidaps.Constants @@ -12,6 +13,7 @@ import info.nightscout.androidaps.interfaces.CommandQueue import info.nightscout.androidaps.plugins.bus.RxBus import info.nightscout.androidaps.plugins.general.overview.events.EventDismissBolusProgressIfRunning import info.nightscout.androidaps.queue.events.EventQueueChanged +import info.nightscout.androidaps.utils.AndroidPermission import info.nightscout.androidaps.utils.T import info.nightscout.androidaps.utils.resources.ResourceHelper import info.nightscout.shared.logging.AAPSLogger @@ -25,7 +27,8 @@ class QueueThread internal constructor( private val rxBus: RxBus, private val activePlugin: ActivePlugin, private val rh: ResourceHelper, - private val sp: SP + private val sp: SP, + private val androidPermission: AndroidPermission ) : Thread() { private var connectLogged = false @@ -46,6 +49,14 @@ class QueueThread internal constructor( while (true) { val secondsElapsed = (System.currentTimeMillis() - connectionStartTime) / 1000 val pump = activePlugin.activePump + // Manifest.permission.BLUETOOTH_CONNECT + if (Build.VERSION.SDK_INT >= /*Build.VERSION_CODES.S*/31) + if (androidPermission.permissionNotGranted(context, "android.permission.BLUETOOTH_CONNECT")) { + aapsLogger.debug(LTag.PUMPQUEUE, "no permission") + rxBus.send(EventPumpStatusChanged(EventPumpStatusChanged.Status.CONNECTING)) + SystemClock.sleep(1000) + continue + } if (!pump.isConnected() && secondsElapsed > Constants.PUMP_MAX_CONNECTION_TIME_IN_SECONDS) { rxBus.send(EventDismissBolusProgressIfRunning(null, null)) rxBus.send(EventPumpStatusChanged(rh.gs(R.string.connectiontimedout))) diff --git a/app/src/test/java/info/nightscout/androidaps/queue/CommandQueueImplementationTest.kt b/app/src/test/java/info/nightscout/androidaps/queue/CommandQueueImplementationTest.kt index 44cf1b19be..8ddaa5f3d6 100644 --- a/app/src/test/java/info/nightscout/androidaps/queue/CommandQueueImplementationTest.kt +++ b/app/src/test/java/info/nightscout/androidaps/queue/CommandQueueImplementationTest.kt @@ -22,6 +22,7 @@ import info.nightscout.androidaps.plugins.bus.RxBus import info.nightscout.androidaps.plugins.configBuilder.ConstraintChecker import info.nightscout.androidaps.plugins.general.maintenance.PrefFileListProvider import info.nightscout.androidaps.queue.commands.* +import info.nightscout.androidaps.utils.AndroidPermission import info.nightscout.androidaps.utils.DateUtil import info.nightscout.androidaps.utils.FabricPrivacy import info.nightscout.androidaps.utils.buildHelper.BuildHelper @@ -46,6 +47,7 @@ class CommandQueueImplementationTest : TestBaseWithProfile() { @Mock lateinit var powerManager: PowerManager @Mock lateinit var repository: AppRepository @Mock lateinit var fileListProvider: PrefFileListProvider + @Mock lateinit var androidPermission: AndroidPermission class CommandQueueMocked( injector: HasAndroidInjector, @@ -62,9 +64,10 @@ class CommandQueueImplementationTest : TestBaseWithProfile() { dateUtil: DateUtil, repository: AppRepository, fabricPrivacy: FabricPrivacy, - config: Config + config: Config, + androidPermission: AndroidPermission ) : CommandQueueImplementation(injector, aapsLogger, rxBus, aapsSchedulers, rh, constraintChecker, profileFunction, - activePlugin, context, sp, buildHelper, dateUtil, repository, fabricPrivacy, config) { + activePlugin, context, sp, buildHelper, dateUtil, repository, fabricPrivacy, config, androidPermission) { override fun notifyAboutNewCommand() {} @@ -107,7 +110,7 @@ class CommandQueueImplementationTest : TestBaseWithProfile() { constraintChecker, profileFunction, activePlugin, context, sp, BuildHelperImpl(config, fileListProvider), dateUtil, repository, - fabricPrivacy, config) + fabricPrivacy, config, androidPermission) testPumpPlugin = TestPumpPlugin(injector) testPumpPlugin.pumpDescription.basalMinimumRate = 0.1 @@ -142,7 +145,7 @@ class CommandQueueImplementationTest : TestBaseWithProfile() { constraintChecker, profileFunction, activePlugin, context, sp, BuildHelperImpl(config, fileListProvider), dateUtil, repository, - fabricPrivacy, config) + fabricPrivacy, config, androidPermission) // start with empty queue Assert.assertEquals(0, commandQueue.size()) diff --git a/app/src/test/java/info/nightscout/androidaps/queue/QueueThreadTest.kt b/app/src/test/java/info/nightscout/androidaps/queue/QueueThreadTest.kt index 2aafe9cbd1..c3a5de70a5 100644 --- a/app/src/test/java/info/nightscout/androidaps/queue/QueueThreadTest.kt +++ b/app/src/test/java/info/nightscout/androidaps/queue/QueueThreadTest.kt @@ -16,6 +16,7 @@ import info.nightscout.androidaps.plugins.configBuilder.ConstraintChecker import info.nightscout.androidaps.plugins.general.maintenance.PrefFileListProvider import info.nightscout.androidaps.queue.commands.Command import info.nightscout.androidaps.queue.commands.CommandTempBasalAbsolute +import info.nightscout.androidaps.utils.AndroidPermission import info.nightscout.androidaps.utils.buildHelper.BuildHelperImpl import info.nightscout.shared.sharedPreferences.SP import org.junit.Assert @@ -33,6 +34,7 @@ class QueueThreadTest : TestBaseWithProfile() { @Mock lateinit var fileListProvider: PrefFileListProvider @Mock lateinit var powerManager: PowerManager @Mock lateinit var repository: AppRepository + @Mock lateinit var androidPermission: AndroidPermission val injector = HasAndroidInjector { AndroidInjector { @@ -57,7 +59,7 @@ class QueueThreadTest : TestBaseWithProfile() { commandQueue = CommandQueueImplementation( injector, aapsLogger, rxBus, aapsSchedulers, rh, constraintChecker, profileFunction, activePlugin, context, sp, - BuildHelperImpl(config, fileListProvider), dateUtil, repository, fabricPrivacy, config + BuildHelperImpl(config, fileListProvider), dateUtil, repository, fabricPrivacy, config, androidPermission ) val pumpDescription = PumpDescription() @@ -79,7 +81,7 @@ class QueueThreadTest : TestBaseWithProfile() { .thenReturn(percentageConstraint) Mockito.`when`(rh.gs(ArgumentMatchers.eq(R.string.temp_basal_absolute), anyObject(), anyObject())).thenReturn("TEMP BASAL %1\$.2f U/h %2\$d min") - sut = QueueThread(commandQueue, context, aapsLogger, rxBus, activePlugin, rh, sp) + sut = QueueThread(commandQueue, context, aapsLogger, rxBus, activePlugin, rh, sp, androidPermission) } @Test diff --git a/core/src/main/java/info/nightscout/androidaps/plugins/pump/common/ble/BlePreCheck.kt b/core/src/main/java/info/nightscout/androidaps/plugins/pump/common/ble/BlePreCheck.kt index 48e07bb61f..8b931f3e87 100644 --- a/core/src/main/java/info/nightscout/androidaps/plugins/pump/common/ble/BlePreCheck.kt +++ b/core/src/main/java/info/nightscout/androidaps/plugins/pump/common/ble/BlePreCheck.kt @@ -20,8 +20,8 @@ import javax.inject.Singleton @Singleton class BlePreCheck @Inject constructor( - val context: Context, - val rh: ResourceHelper + private val context: Context, + private val rh: ResourceHelper ) { companion object { @@ -43,8 +43,12 @@ class BlePreCheck @Inject constructor( } // change after SDK = 31+ if (Build.VERSION.SDK_INT >= /*Build.VERSION_CODES.S*/31) { + if (ContextCompat.checkSelfPermission(context, "android.permission.BLUETOOTH_CONNECT") != PackageManager.PERMISSION_GRANTED || + ContextCompat.checkSelfPermission(context, "android.permission.BLUETOOTH_SCAN") != PackageManager.PERMISSION_GRANTED + ) //ActivityCompat.requestPermissions(activity, arrayOf(Manifest.permission.BLUETOOTH_SCAN, Manifest.permission.BLUETOOTH_CONNECT), PERMISSION_REQUEST_BLUETOOTH) - ActivityCompat.requestPermissions(activity, arrayOf("android.permission.BLUETOOTH_SCAN", "android.permission.BLUETOOTH_CONNECT"), PERMISSION_REQUEST_BLUETOOTH) + ActivityCompat.requestPermissions(activity, arrayOf("android.permission.BLUETOOTH_SCAN", "android.permission.BLUETOOTH_CONNECT"), PERMISSION_REQUEST_BLUETOOTH) + return false } val bluetoothAdapter = (context.getSystemService(Context.BLUETOOTH_SERVICE) as BluetoothManager?)?.adapter