kopia lustrzana https://github.com/meshtastic/Meshtastic-Android
refactor: implement repository pattern for `ConnectionState`
rodzic
179c176baf
commit
13c76f49ee
|
@ -45,7 +45,6 @@ import com.geeksville.mesh.ui.map.MapFragment
|
||||||
import com.geeksville.mesh.util.Exceptions
|
import com.geeksville.mesh.util.Exceptions
|
||||||
import com.geeksville.mesh.util.getParcelableExtraCompat
|
import com.geeksville.mesh.util.getParcelableExtraCompat
|
||||||
import com.geeksville.mesh.util.LanguageUtils
|
import com.geeksville.mesh.util.LanguageUtils
|
||||||
import com.geeksville.mesh.util.exceptionReporter
|
|
||||||
import com.geeksville.mesh.util.getPackageInfoCompat
|
import com.geeksville.mesh.util.getPackageInfoCompat
|
||||||
import com.google.android.material.dialog.MaterialAlertDialogBuilder
|
import com.google.android.material.dialog.MaterialAlertDialogBuilder
|
||||||
import com.google.android.material.snackbar.Snackbar
|
import com.google.android.material.snackbar.Snackbar
|
||||||
|
@ -301,28 +300,10 @@ class MainActivity : AppCompatActivity(), Logging {
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onDestroy() {
|
override fun onDestroy() {
|
||||||
unregisterMeshReceiver()
|
|
||||||
mainScope.cancel("Activity going away")
|
mainScope.cancel("Activity going away")
|
||||||
super.onDestroy()
|
super.onDestroy()
|
||||||
}
|
}
|
||||||
|
|
||||||
private var receiverRegistered = false
|
|
||||||
|
|
||||||
private fun registerMeshReceiver() {
|
|
||||||
unregisterMeshReceiver()
|
|
||||||
val filter = IntentFilter()
|
|
||||||
filter.addAction(MeshService.ACTION_MESH_CONNECTED)
|
|
||||||
registerReceiver(meshServiceReceiver, filter)
|
|
||||||
receiverRegistered = true
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun unregisterMeshReceiver() {
|
|
||||||
if (receiverRegistered) {
|
|
||||||
receiverRegistered = false
|
|
||||||
unregisterReceiver(meshServiceReceiver)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/** Show an alert that may contain HTML */
|
/** Show an alert that may contain HTML */
|
||||||
private fun showAlert(titleText: Int, messageText: Int) {
|
private fun showAlert(titleText: Int, messageText: Int) {
|
||||||
|
|
||||||
|
@ -349,15 +330,8 @@ class MainActivity : AppCompatActivity(), Logging {
|
||||||
|
|
||||||
/// Called when we gain/lose a connection to our mesh radio
|
/// Called when we gain/lose a connection to our mesh radio
|
||||||
private fun onMeshConnectionChanged(newConnection: MeshService.ConnectionState) {
|
private fun onMeshConnectionChanged(newConnection: MeshService.ConnectionState) {
|
||||||
val oldConnection = model.connectionState.value!!
|
|
||||||
debug("connchange $oldConnection -> $newConnection")
|
|
||||||
|
|
||||||
if (newConnection == MeshService.ConnectionState.CONNECTED) {
|
if (newConnection == MeshService.ConnectionState.CONNECTED) {
|
||||||
serviceRepository.meshService?.let { service ->
|
serviceRepository.meshService?.let { service ->
|
||||||
|
|
||||||
model.setConnectionState(newConnection)
|
|
||||||
|
|
||||||
debug("Getting latest DeviceConfig from service")
|
|
||||||
try {
|
try {
|
||||||
val info: MyNodeInfo? = service.myNodeInfo // this can be null
|
val info: MyNodeInfo? = service.myNodeInfo // this can be null
|
||||||
|
|
||||||
|
@ -388,7 +362,6 @@ class MainActivity : AppCompatActivity(), Logging {
|
||||||
}
|
}
|
||||||
} catch (ex: RemoteException) {
|
} catch (ex: RemoteException) {
|
||||||
warn("Abandoning connect $ex, because we probably just lost device connection")
|
warn("Abandoning connect $ex, because we probably just lost device connection")
|
||||||
model.setConnectionState(oldConnection)
|
|
||||||
}
|
}
|
||||||
// if provideLocation enabled: Start providing location (from phone GPS) to mesh
|
// if provideLocation enabled: Start providing location (from phone GPS) to mesh
|
||||||
if (model.provideLocation.value == true)
|
if (model.provideLocation.value == true)
|
||||||
|
@ -405,9 +378,6 @@ class MainActivity : AppCompatActivity(), Logging {
|
||||||
notificationPermissionsLauncher.launch(notificationPermissions)
|
notificationPermissionsLauncher.launch(notificationPermissions)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
// For other connection states, just slam them in
|
|
||||||
model.setConnectionState(newConnection)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -479,24 +449,6 @@ class MainActivity : AppCompatActivity(), Logging {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private val meshServiceReceiver = object : BroadcastReceiver() {
|
|
||||||
|
|
||||||
override fun onReceive(context: Context, intent: Intent) =
|
|
||||||
exceptionReporter {
|
|
||||||
debug("Received from mesh service $intent")
|
|
||||||
|
|
||||||
when (intent.action) {
|
|
||||||
MeshService.ACTION_MESH_CONNECTED -> {
|
|
||||||
val extra = intent.getStringExtra(EXTRA_CONNECTED)
|
|
||||||
if (extra != null) {
|
|
||||||
onMeshConnectionChanged(MeshService.ConnectionState.valueOf(extra))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else -> TODO()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private var connectionJob: Job? = null
|
private var connectionJob: Job? = null
|
||||||
|
|
||||||
private val mesh = object :
|
private val mesh = object :
|
||||||
|
@ -515,9 +467,6 @@ class MainActivity : AppCompatActivity(), Logging {
|
||||||
null // Only switch once - thereafter it should be stored in settings
|
null // Only switch once - thereafter it should be stored in settings
|
||||||
}
|
}
|
||||||
|
|
||||||
// We don't start listening for packets until after we are connected to the service
|
|
||||||
registerMeshReceiver()
|
|
||||||
|
|
||||||
val connectionState =
|
val connectionState =
|
||||||
MeshService.ConnectionState.valueOf(service.connectionState())
|
MeshService.ConnectionState.valueOf(service.connectionState())
|
||||||
|
|
||||||
|
@ -529,9 +478,7 @@ class MainActivity : AppCompatActivity(), Logging {
|
||||||
// We won't receive a notify for the initial state of connection, so we force an update here
|
// We won't receive a notify for the initial state of connection, so we force an update here
|
||||||
onMeshConnectionChanged(connectionState)
|
onMeshConnectionChanged(connectionState)
|
||||||
} catch (ex: RemoteException) {
|
} catch (ex: RemoteException) {
|
||||||
// If we get an exception while reading our service config, the device might have gone away, double check to see if we are really connected
|
|
||||||
errormsg("Device error during init ${ex.message}")
|
errormsg("Device error during init ${ex.message}")
|
||||||
model.setConnectionState(MeshService.ConnectionState.valueOf(service.connectionState()))
|
|
||||||
} finally {
|
} finally {
|
||||||
connectionJob = null
|
connectionJob = null
|
||||||
}
|
}
|
||||||
|
@ -541,7 +488,6 @@ class MainActivity : AppCompatActivity(), Logging {
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onDisconnected() {
|
override fun onDisconnected() {
|
||||||
unregisterMeshReceiver()
|
|
||||||
serviceRepository.setMeshService(null)
|
serviceRepository.setMeshService(null)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -586,7 +532,6 @@ class MainActivity : AppCompatActivity(), Logging {
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onStop() {
|
override fun onStop() {
|
||||||
unregisterMeshReceiver() // No point in receiving updates while the GUI is gone, we'll get them when the user launches the activity
|
|
||||||
unbindMeshService()
|
unbindMeshService()
|
||||||
|
|
||||||
model.connectionState.removeObservers(this)
|
model.connectionState.removeObservers(this)
|
||||||
|
@ -600,8 +545,9 @@ class MainActivity : AppCompatActivity(), Logging {
|
||||||
override fun onStart() {
|
override fun onStart() {
|
||||||
super.onStart()
|
super.onStart()
|
||||||
|
|
||||||
model.connectionState.observe(this) { connected ->
|
model.connectionState.observe(this) { state ->
|
||||||
updateConnectionStatusImage(connected)
|
onMeshConnectionChanged(state)
|
||||||
|
updateConnectionStatusImage(state)
|
||||||
}
|
}
|
||||||
|
|
||||||
bluetoothViewModel.enabled.observe(this) { enabled ->
|
bluetoothViewModel.enabled.observe(this) { enabled ->
|
||||||
|
|
|
@ -305,15 +305,9 @@ class UIViewModel @Inject constructor(
|
||||||
context.getSharedPreferences("ui-prefs", Context.MODE_PRIVATE)
|
context.getSharedPreferences("ui-prefs", Context.MODE_PRIVATE)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Connection state to our radio device
|
// Connection state to our radio device
|
||||||
private val _connectionState = MutableLiveData(MeshService.ConnectionState.DISCONNECTED)
|
val connectionState get() = radioConfigRepository.connectionState.asLiveData()
|
||||||
val connectionState: LiveData<MeshService.ConnectionState> get() = _connectionState
|
fun isConnected() = connectionState.value != MeshService.ConnectionState.DISCONNECTED
|
||||||
|
|
||||||
fun isConnected() = _connectionState.value != MeshService.ConnectionState.DISCONNECTED
|
|
||||||
|
|
||||||
fun setConnectionState(connectionState: MeshService.ConnectionState) {
|
|
||||||
_connectionState.value = connectionState
|
|
||||||
}
|
|
||||||
|
|
||||||
private val _requestChannelUrl = MutableLiveData<Uri?>(null)
|
private val _requestChannelUrl = MutableLiveData<Uri?>(null)
|
||||||
val requestChannelUrl: LiveData<Uri?> get() = _requestChannelUrl
|
val requestChannelUrl: LiveData<Uri?> get() = _requestChannelUrl
|
||||||
|
|
|
@ -1037,7 +1037,7 @@ class MeshService : Service(), Logging {
|
||||||
|
|
||||||
/// Called when we gain/lose connection to our radio
|
/// Called when we gain/lose connection to our radio
|
||||||
private fun onConnectionChanged(c: ConnectionState) {
|
private fun onConnectionChanged(c: ConnectionState) {
|
||||||
debug("onConnectionChanged=$c")
|
debug("onConnectionChanged: $connectionState -> $c")
|
||||||
|
|
||||||
/// Perform all the steps needed once we start waiting for device sleep to complete
|
/// Perform all the steps needed once we start waiting for device sleep to complete
|
||||||
fun startDeviceSleep() {
|
fun startDeviceSleep() {
|
||||||
|
|
Ładowanie…
Reference in New Issue