Added settings

This commit is contained in:
Vadim Furman 2021-02-05 21:29:28 -08:00
parent 0ea40967ac
commit 2d81acbee5
4 changed files with 130 additions and 37 deletions

View file

@ -121,7 +121,7 @@
android:name="com.geeksville.mesh.MainActivity"
android:label="@string/app_name"
android:screenOrientation="portrait"
android:windowSoftInputMode="stateAlwaysHidden"
android:windowSoftInputMode="stateAlwaysHidden|adjustPan"
android:theme="@style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

View file

@ -102,6 +102,49 @@ class UIViewModel(app: Application) : AndroidViewModel(app), Logging {
val radioConfig = object : MutableLiveData<MeshProtos.RadioConfig?>(null) {
}
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
builder.preferencesBuilder.locationShare =
MeshProtos.LocationSharing.LocEnabled
} else
builder.preferencesBuilder.locationShare =
MeshProtos.LocationSharing.LocDisabled
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())
}
}
/// hardware info about our local device
val myNodeInfo = object : MutableLiveData<MyNodeInfo>(null) {}

View file

@ -574,15 +574,30 @@ class SettingsFragment : ScreenFragment("Settings"), Logging {
/// Setup the ui widgets unrelated to BLE scanning
private fun initCommonUI() {
model.ownerName.observe(viewLifecycleOwner, Observer { name ->
model.ownerName.observe(viewLifecycleOwner, { name ->
binding.usernameEditText.setText(name)
})
model.radioConfig.observe(viewLifecycleOwner, { _ ->
var inMinutes = model.positionBroadcastSecs
if (inMinutes != null) {
inMinutes /= 60
}
binding.positionBroadcastPeriodEditText.setText(inMinutes.toString())
binding.lsSleepEditText.setText(model.lsSleepSecs.toString())
})
// Only let user edit their name or set software update while connected to a radio
model.isConnected.observe(viewLifecycleOwner, Observer { connected ->
binding.usernameView.isEnabled = connected == MeshService.ConnectionState.CONNECTED
if (connected == MeshService.ConnectionState.DISCONNECTED)
model.isConnected.observe(viewLifecycleOwner, Observer { connectionState ->
val connected = connectionState == MeshService.ConnectionState.CONNECTED
binding.usernameView.isEnabled = connected
binding.positionBroadcastPeriodView.isEnabled = connected
binding.lsSleepView.isEnabled = connected
if (connectionState == MeshService.ConnectionState.DISCONNECTED)
model.ownerName.value = ""
initNodeInfo()
})
@ -600,30 +615,32 @@ class SettingsFragment : ScreenFragment("Settings"), Logging {
val n = binding.usernameEditText.text.toString().trim()
if (n.isNotEmpty())
model.setOwner(n)
requireActivity().hideKeyboard()
}
binding.positionBroadcastPeriodEditText.on(EditorInfo.IME_ACTION_DONE) {
val n = binding.positionBroadcastPeriodEditText.text.toString().toIntOrNull()
debug("did IME action, text = ${binding.positionBroadcastPeriodEditText.text.toString()}, int=$n")
val meshService = model.meshService
if (n != null && meshService != null) {
try {
var config: MeshProtos.RadioConfig =
MeshProtos.RadioConfig.parseFrom(meshService.getRadioConfig())
val builder : MeshProtos.RadioConfig.Builder = config.toBuilder()
builder.preferencesBuilder.setPositionBroadcastSecs(n * 60)
builder.preferencesBuilder.setLsSecs(n * 60)
config = builder.build()
debug("config=${config.toString()}")
meshService.setRadioConfig(config.toByteArray())
} catch (ex: RemoteException) {
errormsg("Can't change parameter, is device offline? ${ex.message}")
}
val str = binding.positionBroadcastPeriodEditText.text.toString()
var n = str.toIntOrNull()
if (n != null && n < 100000 && n >= 0) {
n *= 60
model.positionBroadcastSecs = n
} else {
binding.scanStatusText.text = "Bad value: $str"
}
requireActivity().hideKeyboard()
}
binding.lsSleepEditText.on(EditorInfo.IME_ACTION_DONE) {
val str = binding.lsSleepEditText.text.toString()
var n = str.toIntOrNull()
if (n != null && n < 100000 && n >= 0) {
model.lsSleepSecs = n
} else {
binding.scanStatusText.text = "Bad value: $str"
}
requireActivity().hideKeyboard()
}
val app = (requireContext().applicationContext as GeeksvilleApplication)
// Set analytics checkbox

View file

@ -1,6 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
@ -19,18 +20,6 @@
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/deviceRadioGroup" />
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/positionBroadcastPeriodEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginEnd="16dp"
android:imeOptions="actionDone"
android:inputType="number"
android:text="9000"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/warningNotPaired" />
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/usernameView"
@ -50,10 +39,9 @@
android:layout_height="wrap_content"
android:imeOptions="actionDone"
android:singleLine="true" />
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.textview.MaterialTextView
android:id="@+id/scanStatusText"
android:layout_width="0dp"
@ -153,4 +141,49 @@
app:layout_constraintTop_toBottomOf="@+id/updateFirmwareButton" />
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/positionBroadcastPeriodView"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="16dp"
android:hint="Broadcast position period (minutes), 0 - disable"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/warningNotPaired">
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/positionBroadcastPeriodEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:imeOptions="actionDone"
android:inputType="number"
android:singleLine="true" />
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/lsSleepView"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="16dp"
android:hint="Sleep period, secs"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/positionBroadcastPeriodView">
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/lsSleepEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:imeOptions="actionDone"
android:inputType="number"
android:singleLine="true" />
</com.google.android.material.textfield.TextInputLayout>
</androidx.constraintlayout.widget.ConstraintLayout>