From 9e42634402e27d5b08f5a760f5aed21ea1cb70a7 Mon Sep 17 00:00:00 2001 From: Kevin Hester Date: Mon, 29 Mar 2021 20:45:11 +0800 Subject: [PATCH] cleanup mockinterface detection --- .../geeksville/mesh/service/BluetoothInterface.kt | 2 -- .../com/geeksville/mesh/service/MockInterface.kt | 6 ++++++ .../mesh/service/RadioInterfaceService.kt | 8 +------- .../com/geeksville/mesh/ui/SettingsFragment.kt | 15 ++++++--------- 4 files changed, 13 insertions(+), 18 deletions(-) diff --git a/app/src/main/java/com/geeksville/mesh/service/BluetoothInterface.kt b/app/src/main/java/com/geeksville/mesh/service/BluetoothInterface.kt index 3a5dd8a9..85e4fc09 100644 --- a/app/src/main/java/com/geeksville/mesh/service/BluetoothInterface.kt +++ b/app/src/main/java/com/geeksville/mesh/service/BluetoothInterface.kt @@ -105,8 +105,6 @@ class BluetoothInterface(val service: RadioInterfaceService, val address: String return bluetoothManager.adapter } - fun toInterfaceName(deviceName: String) = "x$deviceName" - /** Return true if this address is still acceptable. For BLE that means, still bonded */ override fun addressValid(context: Context, rest: String): Boolean { val allPaired = diff --git a/app/src/main/java/com/geeksville/mesh/service/MockInterface.kt b/app/src/main/java/com/geeksville/mesh/service/MockInterface.kt index af4f58fa..f7bdef9d 100644 --- a/app/src/main/java/com/geeksville/mesh/service/MockInterface.kt +++ b/app/src/main/java/com/geeksville/mesh/service/MockInterface.kt @@ -1,5 +1,8 @@ package com.geeksville.mesh.service +import android.content.Context +import com.geeksville.android.BuildUtils +import com.geeksville.android.GeeksvilleApplication import com.geeksville.android.Logging import com.geeksville.mesh.* import com.geeksville.mesh.model.getInitials @@ -14,6 +17,9 @@ class MockInterface(private val service: RadioInterfaceService) : Logging, IRadi rest: String ): IRadioInterface = MockInterface(service) + override fun addressValid(context: Context, rest: String): Boolean = + BuildUtils.isEmulator || ((context.applicationContext as GeeksvilleApplication).isInTestLab) + init { registerFactory() } diff --git a/app/src/main/java/com/geeksville/mesh/service/RadioInterfaceService.kt b/app/src/main/java/com/geeksville/mesh/service/RadioInterfaceService.kt index e539e019..d3edeb5b 100644 --- a/app/src/main/java/com/geeksville/mesh/service/RadioInterfaceService.kt +++ b/app/src/main/java/com/geeksville/mesh/service/RadioInterfaceService.kt @@ -76,18 +76,12 @@ class RadioInterfaceService : Service(), Logging { var address = prefs.getString(DEVADDR_KEY, null) // If we are running on the emulator we default to the mock interface, so we can have some data to show to the user - if (address == null && isMockInterfaceAvailable(context)) + if (address == null && MockInterface.addressValid(context, "")) address = MockInterface.prefix.toString() return address } - /** return true if we should show the mock interface on this device - * (ie are we in an emulator or in testlab - */ - fun isMockInterfaceAvailable(context: Context) = - isEmulator || ((context.applicationContext as GeeksvilleApplication).isInTestLab) - /** Like getDeviceAddress, but filtered to return only devices we are currently bonded with * * at diff --git a/app/src/main/java/com/geeksville/mesh/ui/SettingsFragment.kt b/app/src/main/java/com/geeksville/mesh/ui/SettingsFragment.kt index 03db5bb8..a5dbcb95 100644 --- a/app/src/main/java/com/geeksville/mesh/ui/SettingsFragment.kt +++ b/app/src/main/java/com/geeksville/mesh/ui/SettingsFragment.kt @@ -38,10 +38,7 @@ import com.geeksville.mesh.android.bluetoothManager import com.geeksville.mesh.android.usbManager import com.geeksville.mesh.databinding.SettingsFragmentBinding import com.geeksville.mesh.model.UIViewModel -import com.geeksville.mesh.service.BluetoothInterface -import com.geeksville.mesh.service.MeshService -import com.geeksville.mesh.service.RadioInterfaceService -import com.geeksville.mesh.service.SerialInterface +import com.geeksville.mesh.service.* import com.geeksville.mesh.service.SoftwareUpdateService.Companion.ACTION_UPDATE_PROGRESS import com.geeksville.mesh.service.SoftwareUpdateService.Companion.ProgressNotStarted import com.geeksville.mesh.service.SoftwareUpdateService.Companion.ProgressSuccess @@ -275,10 +272,7 @@ class BTScanModel(app: Application) : AndroidViewModel(app), Logging { debug("BTScan component active") selectedAddress = RadioInterfaceService.getDeviceAddress(context) - return if (bluetoothAdapter == null || RadioInterfaceService.isMockInterfaceAvailable( - context - ) - ) { + return if (bluetoothAdapter == null || MockInterface.addressValid(context, "")) { warn("No bluetooth adapter. Running under emulation?") val testnodes = listOf( @@ -790,7 +784,10 @@ class SettingsFragment : ScreenFragment("Settings"), Logging { // get rid of the warning text once at least one device is paired. // If we are running on an emulator, always leave this message showing so we can test the worst case layout binding.warningNotPaired.visibility = - if (hasBonded && !RadioInterfaceService.isMockInterfaceAvailable(requireContext())) View.GONE else View.VISIBLE + if (hasBonded && !MockInterface.addressValid(requireContext(), "")) + View.GONE + else + View.VISIBLE } /// Setup the GUI to do a classic (pre SDK 26 BLE scan)