Meshtastic-Android/app/src/main/java/com/geeksville/mesh/ui/UsersFragment.kt

322 lines
14 KiB
Kotlin
Raw Normal View History

2020-04-05 11:50:47 -07:00
package com.geeksville.mesh.ui
2020-04-08 15:25:57 -07:00
import android.os.Bundle
import android.text.method.LinkMovementMethod
2020-04-08 15:25:57 -07:00
import android.view.LayoutInflater
2022-09-30 15:57:04 -03:00
import android.view.MenuItem
2020-04-08 15:25:57 -07:00
import android.view.View
import android.view.ViewGroup
2022-09-30 15:57:04 -03:00
import androidx.appcompat.widget.PopupMenu
import androidx.core.content.ContextCompat
2022-04-22 16:56:27 -03:00
import androidx.core.os.bundleOf
import androidx.core.text.HtmlCompat
2020-04-08 15:25:57 -07:00
import androidx.fragment.app.activityViewModels
2022-04-22 16:56:27 -03:00
import androidx.fragment.app.setFragmentResult
2020-04-08 15:25:57 -07:00
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.geeksville.mesh.NodeInfo
2020-04-05 11:50:47 -07:00
import com.geeksville.mesh.R
2022-09-30 15:57:04 -03:00
import com.geeksville.mesh.android.Logging
import com.geeksville.mesh.databinding.AdapterNodeLayoutBinding
import com.geeksville.mesh.databinding.NodelistFragmentBinding
2020-04-08 15:25:57 -07:00
import com.geeksville.mesh.model.UIViewModel
2022-09-04 22:52:40 -03:00
import com.geeksville.mesh.util.formatAgo
2022-09-30 15:57:04 -03:00
import com.google.android.material.dialog.MaterialAlertDialogBuilder
import dagger.hilt.android.AndroidEntryPoint
import java.net.URLEncoder
2020-04-08 15:25:57 -07:00
@AndroidEntryPoint
2020-04-08 15:25:57 -07:00
class UsersFragment : ScreenFragment("Users"), Logging {
private var _binding: NodelistFragmentBinding? = null
// This property is only valid between onCreateView and onDestroyView.
private val binding get() = _binding!!
2020-04-08 15:25:57 -07:00
private val model: UIViewModel by activityViewModels()
// Provide a direct reference to each of the views within a data item
// Used to cache the views within the item layout for fast access
class ViewHolder(itemView: AdapterNodeLayoutBinding) : RecyclerView.ViewHolder(itemView.root) {
2022-09-27 16:29:41 -03:00
val chipNode = itemView.chipNode
2020-04-08 15:25:57 -07:00
val nodeNameView = itemView.nodeNameView
val distanceView = itemView.distanceView
val coordsView = itemView.coordsView
val batteryPctView = itemView.batteryPercentageView
val lastTime = itemView.lastConnectionView
val powerIcon = itemView.batteryIcon
2021-03-22 21:10:58 -07:00
val signalView = itemView.signalView
2022-09-12 18:23:59 -03:00
val envMetrics = itemView.envMetrics
2020-04-08 15:25:57 -07:00
}
private val nodesAdapter = object : RecyclerView.Adapter<ViewHolder>() {
2022-09-30 15:57:04 -03:00
private var nodes = arrayOf<NodeInfo>()
private fun popup(view: View, position: Int) {
val node = nodes[position]
val user = node.user
2022-10-10 18:09:20 -03:00
val showAdmin = position == 0 || model.adminChannelIndex > 0
2022-09-30 15:57:04 -03:00
val popup = PopupMenu(requireContext(), view)
popup.inflate(R.menu.menu_nodes)
popup.menu.setGroupVisible(R.id.group_remote, position > 0)
2022-09-30 15:57:04 -03:00
popup.menu.setGroupVisible(R.id.group_admin, showAdmin)
popup.setOnMenuItemClickListener { item: MenuItem ->
when (item.itemId) {
R.id.direct_message -> {
if (position > 0 && user != null) {
debug("calling MessagesFragment filter: 0${user.id}")
setFragmentResult(
"requestKey",
bundleOf(
"contactKey" to "0${user.id}",
"contactName" to user.longName
)
)
parentFragmentManager.beginTransaction()
.replace(R.id.mainActivityLayout, MessagesFragment())
.addToBackStack(null)
.commit()
}
}
R.id.request_position -> {
if (position > 0 && user != null) {
debug("requesting position for ${user.longName}")
model.requestPosition(node.num)
}
}
2022-09-30 15:57:04 -03:00
R.id.reboot -> {
MaterialAlertDialogBuilder(requireContext())
.setTitle("${getString(R.string.reboot)}\n${user?.longName}?")
.setIcon(R.drawable.ic_twotone_warning_24)
.setNeutralButton(R.string.cancel) { _, _ ->
}
.setPositiveButton(getString(R.string.okay)) { _, _ ->
debug("User clicked requestReboot")
model.requestReboot(node.num)
}
.show()
}
R.id.shutdown -> {
MaterialAlertDialogBuilder(requireContext())
.setTitle("${getString(R.string.shutdown)}\n${user?.longName}?")
.setIcon(R.drawable.ic_twotone_warning_24)
.setNeutralButton(R.string.cancel) { _, _ ->
}
.setPositiveButton(getString(R.string.okay)) { _, _ ->
debug("User clicked requestShutdown")
model.requestShutdown(node.num)
}
.show()
}
R.id.factory_reset -> {
MaterialAlertDialogBuilder(requireContext())
.setTitle("${getString(R.string.factory_reset)}\n${user?.longName}?")
.setIcon(R.drawable.ic_twotone_warning_24)
.setMessage(R.string.factory_reset_description)
.setNeutralButton(R.string.cancel) { _, _ ->
}
.setPositiveButton(R.string.okay) { _, _ ->
debug("User clicked requestFactoryReset")
model.requestFactoryReset(node.num)
}
.show()
}
R.id.nodedb_reset -> {
MaterialAlertDialogBuilder(requireContext())
.setTitle("${getString(R.string.nodedb_reset)}\n${user?.longName}?")
.setIcon(R.drawable.ic_twotone_warning_24)
.setMessage(R.string.nodedb_reset_description)
.setNeutralButton(R.string.cancel) { _, _ ->
}
.setPositiveButton(getString(R.string.okay)) { _, _ ->
debug("User clicked requestNodedbReset")
model.requestNodedbReset(node.num)
}
.show()
}
}
true
}
popup.show()
}
2020-04-08 15:25:57 -07:00
/**
* Called when RecyclerView needs a new [ViewHolder] of the given type to represent
* an item.
*
*
* This new ViewHolder should be constructed with a new View that can represent the items
* of the given type. You can either create a new View manually or inflate it from an XML
* layout file.
*
*
* The new ViewHolder will be used to display items of the adapter using
* [.onBindViewHolder]. Since it will be re-used to display
* different items in the data set, it is a good idea to cache references to sub views of
* the View to avoid unnecessary [View.findViewById] calls.
*
* @param parent The ViewGroup into which the new View will be added after it is bound to
* an adapter position.
* @param viewType The view type of the new View.
*
* @return A new ViewHolder that holds a View of the given view type.
* @see .getItemViewType
* @see .onBindViewHolder
*/
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
val inflater = LayoutInflater.from(requireContext())
// Inflate the custom layout
val contactView = AdapterNodeLayoutBinding.inflate(inflater, parent, false)
2020-04-05 11:50:47 -07:00
2020-04-08 15:25:57 -07:00
// Return a new holder instance
return ViewHolder(contactView)
}
2020-04-05 11:50:47 -07:00
2020-04-08 15:25:57 -07:00
/**
* Returns the total number of items in the data set held by the adapter.
*
* @return The total number of items in this adapter.
*/
override fun getItemCount(): Int = nodes.size
2020-04-05 11:50:47 -07:00
2020-04-08 15:25:57 -07:00
/**
* Called by RecyclerView to display the data at the specified position. This method should
* update the contents of the [ViewHolder.itemView] to reflect the item at the given
* position.
*
*
* Note that unlike [android.widget.ListView], RecyclerView will not call this method
* again if the position of the item changes in the data set unless the item itself is
* invalidated or the new position cannot be determined. For this reason, you should only
* use the `position` parameter while acquiring the related data item inside
* this method and should not keep a copy of it. If you need the position of an item later
* on (e.g. in a click listener), use [ViewHolder.getAdapterPosition] which will
* have the updated adapter position.
*
* Override [.onBindViewHolder] instead if Adapter can
* handle efficient partial bind.
*
* @param holder The ViewHolder which should be updated to represent the contents of the
* item at the given position in the data set.
* @param position The position of the item within the adapter's data set.
*/
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
val n = nodes[position]
2022-09-27 16:29:41 -03:00
val user = n.user
holder.chipNode.text = user?.shortName ?: "UNK"
val name = user?.longName ?: "Unknown node"
holder.nodeNameView.text = name
2020-04-05 11:50:47 -07:00
2021-03-29 20:33:06 +08:00
val pos = n.validPosition
if (pos != null) {
2022-08-30 17:25:11 -03:00
val html = "<a href='geo:${pos.latitude},${pos.longitude}?z=17&label=${
URLEncoder.encode(name, "utf-8")
}'>${model.gpsString(pos)}</a>"
holder.coordsView.text = HtmlCompat.fromHtml(html, HtmlCompat.FROM_HTML_MODE_LEGACY)
holder.coordsView.movementMethod = LinkMovementMethod.getInstance()
holder.coordsView.visibility = View.VISIBLE
} else {
holder.coordsView.visibility = View.INVISIBLE
}
2020-04-08 16:49:27 -07:00
2023-02-02 17:13:44 -03:00
val ourNodeInfo = model.ourNodeInfo.value
2020-04-08 16:49:27 -07:00
val distance = ourNodeInfo?.distanceStr(n)
if (distance != null) {
holder.distanceView.text = distance
holder.distanceView.visibility = View.VISIBLE
2020-04-08 16:49:27 -07:00
} else {
holder.distanceView.visibility = View.INVISIBLE
2020-04-08 16:49:27 -07:00
}
2022-08-30 17:25:11 -03:00
renderBattery(n.batteryLevel, n.voltage, holder)
2021-03-29 20:33:06 +08:00
holder.lastTime.text = formatAgo(n.lastHeard)
2022-09-12 18:23:59 -03:00
if (n.envMetricStr.isNotEmpty()) {
holder.envMetrics.text = n.envMetricStr
holder.envMetrics.visibility = View.VISIBLE
} else {
holder.envMetrics.visibility = View.GONE
}
if (n.num == ourNodeInfo?.num) {
val info = model.myNodeInfo.value
if (info != null) {
val text =
String.format(
"ChUtil %.1f%% AirUtilTX %.1f%%",
2022-04-04 19:10:15 -03:00
n.deviceMetrics?.channelUtilization ?: info.channelUtilization,
n.deviceMetrics?.airUtilTx ?: info.airUtilTx
)
holder.signalView.text = text
holder.signalView.visibility = View.VISIBLE
}
} else {
if ((n.snr < 100f) && (n.rssi < 0)) {
val text = String.format("rssi:%d snr:%.1f", n.rssi, n.snr)
holder.signalView.text = text
holder.signalView.visibility = View.VISIBLE
} else {
holder.signalView.visibility = View.INVISIBLE
}
}
2022-09-27 16:29:41 -03:00
holder.chipNode.setOnClickListener {
2022-09-30 15:57:04 -03:00
popup(it, position)
2022-09-27 16:29:41 -03:00
}
holder.itemView.setOnLongClickListener {
2022-09-30 15:57:04 -03:00
popup(it, position)
2022-04-22 16:56:27 -03:00
true
}
2020-04-08 15:25:57 -07:00
}
/// Called when our node DB changes
2022-04-22 16:56:27 -03:00
fun onNodesChanged(nodesIn: Array<NodeInfo>) {
if (nodesIn.size > 1)
nodesIn.sortWith(compareByDescending { it.lastHeard }, 1)
nodes = nodesIn
2020-04-08 15:25:57 -07:00
notifyDataSetChanged() // FIXME, this is super expensive and redraws all nodes
}
}
private fun renderBattery(
battery: Int?,
voltage: Float?,
holder: ViewHolder
) {
val (image, text) = when (battery) {
in 0..100 -> Pair(
R.drawable.ic_battery_full_24,
String.format("%d%% %.2fV", battery, voltage ?: 0)
)
101 -> Pair(R.drawable.ic_power_plug_24, "")
else -> Pair(R.drawable.ic_battery_full_24, "?")
}
holder.batteryPctView.text = text
holder.powerIcon.setImageDrawable(context?.let {
2022-09-27 16:29:41 -03:00
ContextCompat.getDrawable(it, image)
})
}
2020-04-08 15:25:57 -07:00
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View {
_binding = NodelistFragmentBinding.inflate(inflater, container, false)
return binding.root
2020-04-08 15:25:57 -07:00
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
binding.nodeListView.adapter = nodesAdapter
binding.nodeListView.layoutManager = LinearLayoutManager(requireContext())
2020-04-08 15:25:57 -07:00
model.nodeDB.nodes.observe(viewLifecycleOwner) {
2022-04-22 16:56:27 -03:00
nodesAdapter.onNodesChanged(it.values.toTypedArray())
2020-04-05 11:50:47 -07:00
}
}
}