Meshtastic-Android/app/src/main/java/com/geeksville/mesh/model/UIState.kt

233 lines
8.3 KiB
Kotlin
Raw Normal View History

2020-02-17 13:34:52 -08:00
package com.geeksville.mesh.model
import android.app.Application
2020-02-18 10:40:02 -08:00
import android.content.Context
2020-03-02 08:41:16 -08:00
import android.content.SharedPreferences
2020-03-17 11:35:19 -07:00
import android.net.Uri
2020-02-18 12:22:45 -08:00
import android.os.RemoteException
2020-07-17 17:06:29 -04:00
import android.view.Menu
2020-02-18 10:40:02 -08:00
import androidx.core.content.edit
import androidx.lifecycle.AndroidViewModel
2020-09-23 22:47:45 -04:00
import androidx.lifecycle.LiveData
2020-04-07 17:42:31 -07:00
import androidx.lifecycle.MutableLiveData
2020-09-23 22:47:45 -04:00
import androidx.lifecycle.viewModelScope
import com.geeksville.android.Logging
2021-02-27 11:44:05 +08:00
import com.geeksville.mesh.AppOnlyProtos
import com.geeksville.mesh.IMeshService
2020-02-17 13:34:52 -08:00
import com.geeksville.mesh.MeshProtos
2020-05-13 17:00:23 -07:00
import com.geeksville.mesh.MyNodeInfo
2020-09-23 22:47:45 -04:00
import com.geeksville.mesh.database.MeshtasticDatabase
import com.geeksville.mesh.database.PacketRepository
import com.geeksville.mesh.database.entity.Packet
import com.geeksville.mesh.service.MeshService
2020-09-23 22:47:45 -04:00
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
2021-02-06 22:08:49 -08:00
import kotlin.math.max
/// Given a human name, strip out the first letter of the first three words and return that as the initials for
/// that user. If the original name is only one word, strip vowels from the original name and if the result is
/// 3 or more characters, use the first three characters. If not, just take the first 3 characters of the
/// original name.
fun getInitials(nameIn: String): String {
val nchars = 3
val minchars = 2
val name = nameIn.trim()
val words = name.split(Regex("\\s+")).filter { it.isNotEmpty() }
val initials = when (words.size) {
in 0..minchars - 1 -> {
2020-09-27 14:05:10 -07:00
val nm = if (name.length >= 1)
name.first() + name.drop(1).filterNot { c -> c.toLowerCase() in "aeiou" }
else
""
if (nm.length >= nchars) nm else name
}
else -> words.map { it.first() }.joinToString("")
}
return initials.take(nchars)
}
2020-03-15 16:30:12 -07:00
2021-02-21 12:07:53 +08:00
class UIViewModel(private val app: Application) : AndroidViewModel(app), Logging {
2020-09-23 22:47:45 -04:00
private val repository: PacketRepository
val allPackets: LiveData<List<Packet>>
2020-04-07 17:42:31 -07:00
init {
2020-09-23 22:47:45 -04:00
val packetsDao = MeshtasticDatabase.getDatabase(app).packetDao()
repository = PacketRepository(packetsDao)
allPackets = repository.allPackets
2020-04-07 17:42:31 -07:00
debug("ViewModel created")
}
2020-09-23 22:47:45 -04:00
fun insertPacket(packet: Packet) = viewModelScope.launch(Dispatchers.IO) {
repository.insert(packet)
}
fun deleteAllPacket() = viewModelScope.launch(Dispatchers.IO) {
repository.deleteAll()
}
2020-04-07 17:42:31 -07:00
companion object {
fun getPreferences(context: Context): SharedPreferences =
context.getSharedPreferences("ui-prefs", Context.MODE_PRIVATE)
}
2021-02-21 12:07:53 +08:00
private val context: Context get() = app.applicationContext
2020-07-17 17:06:29 -04:00
var actionBarMenu: Menu? = null
2020-04-07 17:42:31 -07:00
var meshService: IMeshService? = null
val nodeDB = NodeDB(this)
val messagesState = MessagesState(this)
2020-04-07 17:42:31 -07:00
/// Are we connected to our radio device
2020-04-08 08:16:06 -07:00
val isConnected =
object :
MutableLiveData<MeshService.ConnectionState>(MeshService.ConnectionState.DISCONNECTED) {
2020-04-08 08:16:06 -07:00
}
2020-04-07 17:42:31 -07:00
/// various radio settings (including the channel)
2020-04-08 08:16:06 -07:00
val radioConfig = object : MutableLiveData<MeshProtos.RadioConfig?>(null) {
}
2020-04-07 17:42:31 -07:00
2021-02-27 11:44:05 +08:00
val channels = object : MutableLiveData<AppOnlyProtos.ChannelSet?>(null) {
}
2021-02-05 21:29:28 -08:00
var positionBroadcastSecs: Int?
get() {
radioConfig.value?.preferences?.let {
if (it.locationShare == MeshProtos.LocationSharing.LocDisabled) return 0
if (it.positionBroadcastSecs > 0) return it.positionBroadcastSecs
// These default values are borrowed from the device code.
if (it.isRouter) return 60 * 60
return 15 * 60
}
return null
}
set(value) {
val config = radioConfig.value
if (value != null && config != null) {
val builder = config.toBuilder()
if (value > 0) {
builder.preferencesBuilder.positionBroadcastSecs = value
2021-02-06 22:08:49 -08:00
builder.preferencesBuilder.gpsUpdateInterval = value
builder.preferencesBuilder.sendOwnerInterval = max(1, 3600 / value).toInt()
2021-02-05 21:29:28 -08:00
builder.preferencesBuilder.locationShare =
MeshProtos.LocationSharing.LocEnabled
2021-02-06 22:08:49 -08:00
} else {
builder.preferencesBuilder.positionBroadcastSecs = Int.MAX_VALUE
2021-02-05 21:29:28 -08:00
builder.preferencesBuilder.locationShare =
MeshProtos.LocationSharing.LocDisabled
2021-02-06 22:08:49 -08:00
}
2021-02-05 21:29:28 -08:00
setRadioConfig(builder.build())
}
}
var lsSleepSecs: Int?
get() {
radioConfig.value?.preferences?.let {
return it.lsSecs
}
return null
}
set(value) {
val config = radioConfig.value
if (value != null && config != null) {
val builder = config.toBuilder()
builder.preferencesBuilder.lsSecs = value
setRadioConfig(builder.build())
}
}
2020-05-13 17:00:23 -07:00
/// hardware info about our local device
val myNodeInfo = object : MutableLiveData<MyNodeInfo>(null) {}
override fun onCleared() {
super.onCleared()
debug("ViewModel cleared")
}
2021-02-27 11:44:05 +08:00
/**
* Return the primary channel info
*/
val primaryChannel: ChannelSet? get() {
return channels.value?.let { it ->
Channel(it.getSettings(0))
}
}
2020-04-07 17:42:31 -07:00
/// Set the radio config (also updates our saved copy in preferences)
2021-02-27 11:44:05 +08:00
private fun setRadioConfig(c: MeshProtos.RadioConfig) {
2020-04-07 17:42:31 -07:00
debug("Setting new radio config!")
meshService?.radioConfig = c.toByteArray()
radioConfig.value =
c // Must be done after calling the service, so we will will properly throw if the service failed (and therefore not cache invalid new settings)
2021-02-27 11:44:05 +08:00
}
/// Set the radio config (also updates our saved copy in preferences)
private fun setChannels(c: AppOnlyProtos.ChannelSet) {
debug("Setting new channels!")
meshService?.channels = c.toByteArray()
channels.value =
c // Must be done after calling the service, so we will will properly throw if the service failed (and therefore not cache invalid new settings)
2020-04-07 17:42:31 -07:00
getPreferences(context).edit(commit = true) {
2021-02-27 11:44:05 +08:00
this.putString("channel-url", primaryChannel!!.getChannelUrl().toString())
2020-04-07 17:42:31 -07:00
}
}
2020-02-17 13:34:52 -08:00
/** Update just the channel settings portion of our config (both in the device and in saved preferences) */
fun setChannel(c: MeshProtos.ChannelSettings) {
// When running on the emulator, radio config might not really be available, in that case, just ignore attempts to change the config
radioConfig.value?.toBuilder()?.let { config ->
config.channelSettings = c
setRadioConfig(config.build())
}
}
2020-02-17 13:34:52 -08:00
/// Kinda ugly - created in the activity but used from Compose - figure out if there is a cleaner way GIXME
// lateinit var googleSignInClient: GoogleSignInClient
/// our name in hte radio
/// Note, we generate owner initials automatically for now
2020-02-18 12:22:45 -08:00
/// our activity will read this from prefs or set it to the empty string
val ownerName = object : MutableLiveData<String>("MrIDE Test") {
2020-03-15 18:44:10 -07:00
}
2020-03-15 16:30:12 -07:00
2020-02-18 10:40:02 -08:00
val bluetoothEnabled = object : MutableLiveData<Boolean>(false) {
}
/// If the app was launched because we received a new channel intent, the Url will be here
var requestedChannelUrl: Uri? = null
2020-02-18 10:40:02 -08:00
// clean up all this nasty owner state management FIXME
fun setOwner(s: String? = null) {
2020-02-18 12:22:45 -08:00
2020-02-18 10:40:02 -08:00
if (s != null) {
ownerName.value = s
2020-02-18 12:22:45 -08:00
// note: we allow an empty userstring to be written to prefs
2020-03-02 08:41:16 -08:00
getPreferences(context).edit(commit = true) {
2020-02-18 10:40:02 -08:00
putString("owner", s)
}
}
// Note: we are careful to not set a new unique ID
if (ownerName.value!!.isNotEmpty())
2020-02-18 12:22:45 -08:00
try {
meshService?.setOwner(
null,
ownerName.value,
getInitials(ownerName.value!!)
2020-02-18 12:22:45 -08:00
) // Note: we use ?. here because we might be running in the emulator
} catch (ex: RemoteException) {
errormsg("Can't set username on device, is device offline? ${ex.message}")
2020-02-18 12:22:45 -08:00
}
2020-02-18 10:40:02 -08:00
}
2020-02-17 13:34:52 -08:00
}