channel qrs are now sharable and real

This commit is contained in:
Kevin Hester 2020-03-02 08:41:16 -08:00
parent 90cee2f202
commit 44ebac1758
4 changed files with 56 additions and 28 deletions

View file

@ -1,6 +1,7 @@
package com.geeksville.mesh.model
import android.content.Context
import android.content.SharedPreferences
import android.graphics.Bitmap
import android.os.RemoteException
import android.util.Base64
@ -26,7 +27,7 @@ object UIState : Logging {
val isConnected = mutableStateOf(false)
/// various radio settings (including the channel)
val radioConfig = mutableStateOf(MeshProtos.RadioConfig.getDefaultInstance())
private val radioConfig = mutableStateOf<MeshProtos.RadioConfig?>(null)
/// our name in hte radio
/// Note, we generate owner initials automatically for now
@ -34,22 +35,44 @@ object UIState : Logging {
var ownerName: String = "MrInIDE Ownername"
/// Return an URL that represents the current channel values
val channelUrl
get(): String {
val channelBytes = radioConfig.value.channelSettings.toByteArray()
fun getChannelUrl(context: Context): String {
// If we have a valid radio config use it, othterwise use whatever we have saved in the prefs
val radio = radioConfig.value
if (radio != null) {
val settings = radio.channelSettings
val channelBytes = settings.toByteArray()
val enc = Base64.encodeToString(channelBytes, Base64.URL_SAFE + Base64.NO_WRAP)
return "https://www.meshtastic.org/c/$enc"
} else {
return getPreferences(context).getString(
"owner",
"https://www.meshtastic.org/c/unset"
)!!
}
}
val channelQR
get(): Bitmap {
val multiFormatWriter = MultiFormatWriter()
fun getChannelQR(context: Context): Bitmap
{
val multiFormatWriter = MultiFormatWriter()
val bitMatrix = multiFormatWriter.encode(channelUrl, BarcodeFormat.QR_CODE, 192, 192);
val barcodeEncoder = BarcodeEncoder()
return barcodeEncoder.createBitmap(bitMatrix)
val bitMatrix =
multiFormatWriter.encode(getChannelUrl(context), BarcodeFormat.QR_CODE, 192, 192);
val barcodeEncoder = BarcodeEncoder()
return barcodeEncoder.createBitmap(bitMatrix)
}
fun getPreferences(context: Context): SharedPreferences =
context.getSharedPreferences("ui-prefs", Context.MODE_PRIVATE)
/// Set the radio config (also updates our saved copy in preferences)
fun setRadioConfig(context: Context, c: MeshProtos.RadioConfig) {
radioConfig.value = c
getPreferences(context).edit(commit = true) {
this.putString("channel-url", getChannelUrl(context))
}
}
// clean up all this nasty owner state management FIXME
fun setOwner(context: Context, s: String? = null) {
@ -58,8 +81,7 @@ object UIState : Logging {
ownerName = s
// note: we allow an empty userstring to be written to prefs
val prefs = context.getSharedPreferences("ui-prefs", Context.MODE_PRIVATE)
prefs.edit(commit = true) {
getPreferences(context).edit(commit = true) {
putString("owner", s)
}
}