2020-02-17 20:00:11 -08:00
|
|
|
package com.geeksville.mesh.ui
|
|
|
|
|
|
2020-04-08 10:55:28 -07:00
|
|
|
import android.content.Intent
|
2020-04-09 16:33:42 -07:00
|
|
|
import android.graphics.ColorMatrix
|
|
|
|
|
import android.graphics.ColorMatrixColorFilter
|
2020-04-07 16:04:58 -07:00
|
|
|
import android.os.Bundle
|
2020-04-15 07:49:39 -07:00
|
|
|
import android.os.RemoteException
|
2020-04-07 16:04:58 -07:00
|
|
|
import android.view.LayoutInflater
|
|
|
|
|
import android.view.View
|
|
|
|
|
import android.view.ViewGroup
|
2020-04-09 16:33:42 -07:00
|
|
|
import android.view.inputmethod.EditorInfo
|
2020-04-07 17:42:31 -07:00
|
|
|
import android.widget.ArrayAdapter
|
2020-04-09 16:33:42 -07:00
|
|
|
import android.widget.ImageView
|
2020-04-07 17:42:31 -07:00
|
|
|
import androidx.fragment.app.activityViewModels
|
2020-04-08 10:55:28 -07:00
|
|
|
import com.geeksville.analytics.DataPair
|
|
|
|
|
import com.geeksville.android.GeeksvilleApplication
|
2020-02-18 08:56:37 -08:00
|
|
|
import com.geeksville.android.Logging
|
2020-04-09 16:33:42 -07:00
|
|
|
import com.geeksville.android.hideKeyboard
|
2020-06-14 00:11:08 -04:00
|
|
|
import com.geeksville.mesh.MeshProtos
|
2020-02-17 20:00:11 -08:00
|
|
|
import com.geeksville.mesh.R
|
2020-06-12 20:26:10 -07:00
|
|
|
import com.geeksville.mesh.model.Channel
|
2020-06-14 00:11:08 -04:00
|
|
|
import com.geeksville.mesh.model.ChannelOption
|
2020-04-07 17:42:31 -07:00
|
|
|
import com.geeksville.mesh.model.UIViewModel
|
2020-04-13 16:00:48 -07:00
|
|
|
import com.geeksville.mesh.service.MeshService
|
2020-04-09 16:33:42 -07:00
|
|
|
import com.google.android.material.dialog.MaterialAlertDialogBuilder
|
2020-04-15 07:49:39 -07:00
|
|
|
import com.google.android.material.snackbar.Snackbar
|
2020-06-12 20:26:10 -07:00
|
|
|
import com.google.protobuf.ByteString
|
2020-04-08 10:55:28 -07:00
|
|
|
import kotlinx.android.synthetic.main.channel_fragment.*
|
2020-06-12 20:26:10 -07:00
|
|
|
import java.security.SecureRandom
|
2020-04-08 10:55:28 -07:00
|
|
|
|
2020-02-17 20:00:11 -08:00
|
|
|
|
2020-04-09 16:33:42 -07:00
|
|
|
// Make an image view dim
|
|
|
|
|
fun ImageView.setDim() {
|
|
|
|
|
val matrix = ColorMatrix()
|
|
|
|
|
matrix.setSaturation(0f) //0 means grayscale
|
|
|
|
|
val cf = ColorMatrixColorFilter(matrix)
|
|
|
|
|
colorFilter = cf
|
|
|
|
|
imageAlpha = 64 // 128 = 0.5
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Return image view to normal
|
|
|
|
|
fun ImageView.setOpaque() {
|
|
|
|
|
colorFilter = null
|
|
|
|
|
imageAlpha = 255
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-07 16:04:58 -07:00
|
|
|
class ChannelFragment : ScreenFragment("Channel"), Logging {
|
|
|
|
|
|
2020-04-07 17:42:31 -07:00
|
|
|
private val model: UIViewModel by activityViewModels()
|
|
|
|
|
|
2020-04-07 16:04:58 -07:00
|
|
|
override fun onCreateView(
|
|
|
|
|
inflater: LayoutInflater, container: ViewGroup?,
|
|
|
|
|
savedInstanceState: Bundle?
|
2020-04-07 17:42:31 -07:00
|
|
|
): View? {
|
|
|
|
|
return inflater.inflate(R.layout.channel_fragment, container, false)
|
|
|
|
|
}
|
2020-04-07 16:04:58 -07:00
|
|
|
|
2020-04-09 16:33:42 -07:00
|
|
|
/// Called when the lock/unlock icon has changed
|
2020-04-08 10:55:28 -07:00
|
|
|
private fun onEditingChanged() {
|
|
|
|
|
val isEditing = editableCheckbox.isChecked
|
|
|
|
|
|
2020-06-14 00:11:08 -04:00
|
|
|
channelOptions.isEnabled = isEditing
|
2020-04-08 10:55:28 -07:00
|
|
|
shareButton.isEnabled = !isEditing
|
|
|
|
|
channelNameView.isEnabled = isEditing
|
2020-04-09 16:33:42 -07:00
|
|
|
if (isEditing) // Dim the (stale) QR code while editing...
|
|
|
|
|
qrView.setDim()
|
|
|
|
|
else
|
|
|
|
|
qrView.setOpaque()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Pull the latest data from the model (discarding any user edits)
|
|
|
|
|
private fun setGUIfromModel() {
|
2020-09-04 07:59:07 -04:00
|
|
|
val radioConfig = model.radioConfig.value
|
|
|
|
|
val channel = UIViewModel.getChannel(radioConfig)
|
2020-04-09 16:33:42 -07:00
|
|
|
|
|
|
|
|
editableCheckbox.isChecked = false // start locked
|
|
|
|
|
if (channel != null) {
|
|
|
|
|
qrView.visibility = View.VISIBLE
|
|
|
|
|
channelNameEdit.visibility = View.VISIBLE
|
2020-08-12 12:31:37 -07:00
|
|
|
channelNameEdit.setText(channel.humanName)
|
2020-04-13 16:00:48 -07:00
|
|
|
|
|
|
|
|
// For now, we only let the user edit/save channels while the radio is awake - because the service
|
|
|
|
|
// doesn't cache radioconfig writes.
|
|
|
|
|
val connected = model.isConnected.value == MeshService.ConnectionState.CONNECTED
|
|
|
|
|
editableCheckbox.isEnabled = connected
|
2020-04-09 16:33:42 -07:00
|
|
|
|
|
|
|
|
qrView.setImageBitmap(channel.getChannelQR())
|
2020-09-04 07:59:07 -04:00
|
|
|
|
|
|
|
|
val modemConfig = radioConfig?.channelSettings?.modemConfig
|
|
|
|
|
val channelOption = ChannelOption.fromConfig(modemConfig)
|
2020-09-08 12:29:28 -07:00
|
|
|
filled_exposed_dropdown.setText(
|
|
|
|
|
getString(
|
|
|
|
|
channelOption?.configRes ?: R.string.modem_config_unrecognized
|
|
|
|
|
), false
|
|
|
|
|
)
|
2020-09-04 07:59:07 -04:00
|
|
|
|
2020-04-09 16:33:42 -07:00
|
|
|
} else {
|
|
|
|
|
qrView.visibility = View.INVISIBLE
|
|
|
|
|
channelNameEdit.visibility = View.INVISIBLE
|
|
|
|
|
editableCheckbox.isEnabled = false
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
onEditingChanged() // we just locked the gui
|
2020-06-14 00:11:08 -04:00
|
|
|
val modemConfigs = ChannelOption.values()
|
|
|
|
|
val modemConfigList = modemConfigs.map { getString(it.configRes) }
|
2020-04-09 16:33:42 -07:00
|
|
|
val adapter = ArrayAdapter(
|
|
|
|
|
requireContext(),
|
|
|
|
|
R.layout.dropdown_menu_popup_item,
|
2020-06-14 00:11:08 -04:00
|
|
|
modemConfigList
|
2020-04-09 16:33:42 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
filled_exposed_dropdown.setAdapter(adapter)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private fun shareChannel() {
|
|
|
|
|
UIViewModel.getChannel(model.radioConfig.value)?.let { channel ->
|
|
|
|
|
|
|
|
|
|
GeeksvilleApplication.analytics.track(
|
|
|
|
|
"share",
|
|
|
|
|
DataPair("content_type", "channel")
|
|
|
|
|
) // track how many times users share channels
|
|
|
|
|
|
|
|
|
|
val sendIntent: Intent = Intent().apply {
|
|
|
|
|
action = Intent.ACTION_SEND
|
|
|
|
|
putExtra(Intent.EXTRA_TEXT, channel.getChannelUrl().toString())
|
|
|
|
|
putExtra(
|
|
|
|
|
Intent.EXTRA_TITLE,
|
|
|
|
|
getString(R.string.url_for_join)
|
|
|
|
|
)
|
|
|
|
|
type = "text/plain"
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
val shareIntent = Intent.createChooser(sendIntent, null)
|
|
|
|
|
requireActivity().startActivity(shareIntent)
|
|
|
|
|
}
|
2020-04-08 10:55:28 -07:00
|
|
|
}
|
|
|
|
|
|
2020-04-07 16:04:58 -07:00
|
|
|
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
|
|
|
|
|
super.onViewCreated(view, savedInstanceState)
|
2020-04-07 17:42:31 -07:00
|
|
|
|
2020-04-09 16:33:42 -07:00
|
|
|
channelNameEdit.on(EditorInfo.IME_ACTION_DONE) {
|
|
|
|
|
requireActivity().hideKeyboard()
|
|
|
|
|
}
|
2020-04-08 10:55:28 -07:00
|
|
|
|
2020-09-08 12:29:28 -07:00
|
|
|
// Note: Do not use setOnCheckedChanged here because we don't want to be called when we programmatically disable editing
|
|
|
|
|
editableCheckbox.setOnClickListener { _ ->
|
|
|
|
|
val checked = editableCheckbox.isChecked
|
2020-08-12 12:31:37 -07:00
|
|
|
if (checked) {
|
|
|
|
|
// User just unlocked for editing - remove the # goo around the channel name
|
|
|
|
|
UIViewModel.getChannel(model.radioConfig.value)?.let { channel ->
|
|
|
|
|
channelNameEdit.setText(channel.name)
|
|
|
|
|
}
|
|
|
|
|
} else {
|
2020-04-09 16:33:42 -07:00
|
|
|
// User just locked it, we should warn and then apply changes to radio
|
|
|
|
|
MaterialAlertDialogBuilder(requireContext())
|
2020-04-09 17:06:41 -07:00
|
|
|
.setTitle(R.string.change_channel)
|
|
|
|
|
.setMessage(R.string.are_you_sure_channel)
|
|
|
|
|
.setNeutralButton(R.string.cancel) { _, _ ->
|
2020-04-09 16:33:42 -07:00
|
|
|
setGUIfromModel()
|
2020-04-08 10:55:28 -07:00
|
|
|
}
|
2020-04-09 17:06:41 -07:00
|
|
|
.setPositiveButton(getString(R.string.accept)) { _, _ ->
|
2020-04-09 16:33:42 -07:00
|
|
|
// Generate a new channel with only the changes the user can change in the GUI
|
|
|
|
|
UIViewModel.getChannel(model.radioConfig.value)?.let { old ->
|
|
|
|
|
val newSettings = old.settings.toBuilder()
|
|
|
|
|
newSettings.name = channelNameEdit.text.toString().trim()
|
2020-06-12 20:26:10 -07:00
|
|
|
|
|
|
|
|
// Generate a new AES256 key (for any channel not named Default)
|
2020-06-12 20:38:43 -07:00
|
|
|
if (!newSettings.name.equals(
|
|
|
|
|
Channel.defaultChannelName,
|
|
|
|
|
ignoreCase = true
|
|
|
|
|
)
|
|
|
|
|
) {
|
2020-06-12 20:26:10 -07:00
|
|
|
debug("ASSIGNING NEW AES256 KEY")
|
|
|
|
|
val random = SecureRandom()
|
|
|
|
|
val bytes = ByteArray(32)
|
|
|
|
|
random.nextBytes(bytes)
|
|
|
|
|
newSettings.psk = ByteString.copyFrom(bytes)
|
2020-06-12 20:38:43 -07:00
|
|
|
} else {
|
|
|
|
|
debug("ASSIGNING NEW default AES128 KEY")
|
2020-09-08 12:29:28 -07:00
|
|
|
newSettings.name =
|
|
|
|
|
Channel.defaultChannelName // Fix any case errors
|
2020-06-12 20:38:43 -07:00
|
|
|
newSettings.psk = ByteString.copyFrom(Channel.channelDefaultKey)
|
2020-06-12 20:26:10 -07:00
|
|
|
}
|
2020-04-15 07:49:39 -07:00
|
|
|
|
2020-06-14 09:33:15 -07:00
|
|
|
val selectedChannelOptionString =
|
|
|
|
|
filled_exposed_dropdown.editableText.toString()
|
2020-06-15 22:28:25 -04:00
|
|
|
val modemConfig = getModemConfig(selectedChannelOptionString)
|
|
|
|
|
|
2020-07-08 07:57:22 -07:00
|
|
|
if (modemConfig != MeshProtos.ChannelSettings.ModemConfig.UNRECOGNIZED)
|
2020-06-15 22:28:25 -04:00
|
|
|
newSettings.modemConfig = modemConfig
|
2020-04-15 07:49:39 -07:00
|
|
|
// Try to change the radio, if it fails, tell the user why and throw away their redits
|
|
|
|
|
try {
|
|
|
|
|
model.setChannel(newSettings.build())
|
|
|
|
|
// Since we are writing to radioconfig, that will trigger the rest of the GUI update (QR code etc)
|
|
|
|
|
} catch (ex: RemoteException) {
|
2020-07-08 07:57:22 -07:00
|
|
|
errormsg("ignoring channel problem", ex)
|
2020-08-12 12:31:37 -07:00
|
|
|
|
2020-04-15 07:49:39 -07:00
|
|
|
setGUIfromModel() // Throw away user edits
|
2020-06-12 20:26:10 -07:00
|
|
|
|
2020-04-15 07:49:39 -07:00
|
|
|
// Tell the user to try again
|
|
|
|
|
Snackbar.make(
|
|
|
|
|
editableCheckbox,
|
|
|
|
|
R.string.radio_sleeping,
|
|
|
|
|
Snackbar.LENGTH_SHORT
|
|
|
|
|
).show()
|
|
|
|
|
}
|
2020-04-09 16:33:42 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
.show()
|
2020-04-07 17:42:31 -07:00
|
|
|
}
|
|
|
|
|
|
2020-04-09 16:33:42 -07:00
|
|
|
onEditingChanged() // update GUI on what user is allowed to edit/share
|
|
|
|
|
}
|
2020-04-07 17:42:31 -07:00
|
|
|
|
2020-04-09 16:33:42 -07:00
|
|
|
// Share this particular channel if someone clicks share
|
|
|
|
|
shareButton.setOnClickListener {
|
|
|
|
|
shareChannel()
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-04 07:59:07 -04:00
|
|
|
model.radioConfig.observe(viewLifecycleOwner, {
|
2020-04-13 16:00:48 -07:00
|
|
|
setGUIfromModel()
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
// If connection state changes, we might need to enable/disable buttons
|
2020-09-04 07:59:07 -04:00
|
|
|
model.isConnected.observe(viewLifecycleOwner, {
|
2020-04-09 16:33:42 -07:00
|
|
|
setGUIfromModel()
|
2020-04-07 17:42:31 -07:00
|
|
|
})
|
2020-04-07 16:04:58 -07:00
|
|
|
}
|
2020-06-14 00:11:08 -04:00
|
|
|
|
|
|
|
|
private fun getModemConfig(selectedChannelOptionString: String): MeshProtos.ChannelSettings.ModemConfig {
|
|
|
|
|
for (item in ChannelOption.values()) {
|
|
|
|
|
if (getString(item.configRes) == selectedChannelOptionString)
|
|
|
|
|
return item.modemConfig
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return MeshProtos.ChannelSettings.ModemConfig.UNRECOGNIZED
|
|
|
|
|
}
|
2020-04-07 16:04:58 -07:00
|
|
|
}
|