2020-02-17 13:34:52 -08:00
|
|
|
package com.geeksville.mesh.model
|
|
|
|
|
|
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-03-30 10:26:16 -07:00
|
|
|
import android.os.Bundle
|
2020-02-18 12:22:45 -08:00
|
|
|
import android.os.RemoteException
|
2020-02-17 13:34:52 -08:00
|
|
|
import androidx.compose.mutableStateOf
|
2020-02-18 10:40:02 -08:00
|
|
|
import androidx.core.content.edit
|
2020-03-15 18:44:10 -07:00
|
|
|
import com.geeksville.android.BuildUtils.isEmulator
|
2020-02-29 13:21:05 -08:00
|
|
|
import com.geeksville.android.Logging
|
2020-02-17 15:39:49 -08:00
|
|
|
import com.geeksville.mesh.IMeshService
|
2020-02-17 13:34:52 -08:00
|
|
|
import com.geeksville.mesh.MeshProtos
|
2020-02-18 10:40:02 -08:00
|
|
|
import com.geeksville.mesh.ui.getInitials
|
2020-03-15 16:30:12 -07:00
|
|
|
|
2020-02-17 13:34:52 -08:00
|
|
|
/// FIXME - figure out how to merge this staate with the AppStatus Model
|
2020-03-02 07:46:03 -08:00
|
|
|
object UIState : Logging {
|
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
|
|
|
|
|
|
2020-02-17 15:39:49 -08:00
|
|
|
var meshService: IMeshService? = null
|
2020-02-17 13:34:52 -08:00
|
|
|
|
|
|
|
|
/// Are we connected to our radio device
|
|
|
|
|
val isConnected = mutableStateOf(false)
|
|
|
|
|
|
|
|
|
|
/// various radio settings (including the channel)
|
2020-03-02 08:41:16 -08:00
|
|
|
private val radioConfig = mutableStateOf<MeshProtos.RadioConfig?>(null)
|
2020-02-17 13:34:52 -08:00
|
|
|
|
|
|
|
|
/// 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
|
|
|
|
|
var ownerName: String = "MrInIDE Ownername"
|
2020-02-17 13:34:52 -08:00
|
|
|
|
2020-03-17 11:35:19 -07:00
|
|
|
/// If the app was launched because we received a new channel intent, the Url will be here
|
|
|
|
|
var requestedChannelUrl: Uri? = null
|
|
|
|
|
|
2020-03-30 10:26:16 -07:00
|
|
|
var savedInstanceState: Bundle? = null
|
|
|
|
|
|
2020-03-15 18:44:10 -07:00
|
|
|
/**
|
|
|
|
|
* Return the current channel info
|
|
|
|
|
* FIXME, we should sim channels at the MeshService level if we are running on an emulator,
|
|
|
|
|
* for now I just fake it by returning a canned channel.
|
|
|
|
|
*/
|
|
|
|
|
fun getChannel(): Channel? {
|
|
|
|
|
val channel = radioConfig.value?.channelSettings?.let { Channel(it) }
|
|
|
|
|
|
|
|
|
|
return if (channel == null && isEmulator)
|
|
|
|
|
Channel.emulated
|
|
|
|
|
else
|
|
|
|
|
channel
|
|
|
|
|
}
|
2020-03-15 16:30:12 -07:00
|
|
|
|
2020-03-02 08:41:16 -08:00
|
|
|
fun getPreferences(context: Context): SharedPreferences =
|
|
|
|
|
context.getSharedPreferences("ui-prefs", Context.MODE_PRIVATE)
|
2020-02-18 10:40:02 -08:00
|
|
|
|
2020-03-02 08:41:16 -08:00
|
|
|
/// Set the radio config (also updates our saved copy in preferences)
|
|
|
|
|
fun setRadioConfig(context: Context, c: MeshProtos.RadioConfig) {
|
|
|
|
|
radioConfig.value = c
|
2020-03-02 07:46:03 -08:00
|
|
|
|
2020-03-02 08:41:16 -08:00
|
|
|
getPreferences(context).edit(commit = true) {
|
2020-03-17 14:56:06 -07:00
|
|
|
this.putString("channel-url", getChannel()!!.getChannelUrl().toString())
|
2020-03-02 07:46:03 -08:00
|
|
|
}
|
2020-03-02 08:41:16 -08:00
|
|
|
}
|
2020-03-02 07:46:03 -08:00
|
|
|
|
2020-02-18 10:40:02 -08:00
|
|
|
// clean up all this nasty owner state management FIXME
|
|
|
|
|
fun setOwner(context: Context, s: String? = null) {
|
2020-02-18 12:22:45 -08:00
|
|
|
|
2020-02-18 10:40:02 -08:00
|
|
|
if (s != null) {
|
|
|
|
|
ownerName = 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
|
2020-02-18 12:22:45 -08:00
|
|
|
if (ownerName.isNotEmpty())
|
|
|
|
|
try {
|
|
|
|
|
meshService?.setOwner(
|
|
|
|
|
null,
|
|
|
|
|
ownerName,
|
|
|
|
|
getInitials(ownerName)
|
|
|
|
|
) // Note: we use ?. here because we might be running in the emulator
|
|
|
|
|
} catch (ex: RemoteException) {
|
2020-02-29 13:21:05 -08:00
|
|
|
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
|
|
|
}
|