Show host addresses

This commit is contained in:
Mygod
2018-01-21 22:09:33 -08:00
parent 881e0d09e7
commit 11edc1ae82
9 changed files with 79 additions and 18 deletions

View File

@@ -28,6 +28,7 @@ import be.mygod.vpnhotspot.net.IpNeighbour
import be.mygod.vpnhotspot.net.IpNeighbourMonitor
import be.mygod.vpnhotspot.net.ConnectivityManagerHelper
import be.mygod.vpnhotspot.net.TetherType
import java.net.NetworkInterface
class RepeaterFragment : Fragment(), ServiceConnection, Toolbar.OnMenuItemClickListener, IpNeighbourMonitor.Callback {
inner class Data : BaseObservable() {
@@ -56,6 +57,9 @@ class RepeaterFragment : Fragment(), ServiceConnection, Toolbar.OnMenuItemClickL
val ssid @Bindable get() = binder?.service?.ssid ?: getText(R.string.repeater_inactive)
val password @Bindable get() = binder?.service?.password ?: ""
val addresses @Bindable get(): String {
return NetworkInterface.getByName(p2pInterface ?: return "")?.formatAddresses() ?: ""
}
fun onStatusChanged() {
notifyPropertyChanged(BR.switchEnabled)
@@ -67,6 +71,7 @@ class RepeaterFragment : Fragment(), ServiceConnection, Toolbar.OnMenuItemClickL
notifyPropertyChanged(BR.ssid)
notifyPropertyChanged(BR.password)
p2pInterface = group?.`interface`
notifyPropertyChanged(BR.addresses)
adapter.p2p = group?.clientList ?: emptyList()
adapter.recreate()
}
@@ -80,7 +85,7 @@ class RepeaterFragment : Fragment(), ServiceConnection, Toolbar.OnMenuItemClickL
val ip = neighbour?.ip
val icon get() = TetherType.ofInterface(iface, p2pInterface).icon
val title get() = listOf(ip, mac).filter { !it.isNullOrEmpty() }.joinToString()
val title get() = listOf(ip, mac).filter { !it.isNullOrEmpty() }.joinToString("\t\t")
val description get() = getString(when (neighbour?.state) {
IpNeighbour.State.INCOMPLETE, null -> R.string.connected_state_incomplete
IpNeighbour.State.VALID -> R.string.connected_state_valid

View File

@@ -21,6 +21,8 @@ import be.mygod.vpnhotspot.databinding.FragmentTetheringBinding
import be.mygod.vpnhotspot.databinding.ListitemInterfaceBinding
import be.mygod.vpnhotspot.net.ConnectivityManagerHelper
import be.mygod.vpnhotspot.net.TetherType
import java.net.Inet4Address
import java.net.NetworkInterface
class TetheringFragment : Fragment(), ServiceConnection {
companion object {
@@ -42,11 +44,11 @@ class TetheringFragment : Fragment(), ServiceConnection {
private open class DefaultSorter<T : Comparable<T>> : BaseSorter<T>() {
override fun compareNonNull(o1: T, o2: T): Int = o1.compareTo(o2)
}
private object StringSorter : DefaultSorter<String>()
private object TetheredInterfaceSorter : DefaultSorter<TetheredInterface>()
inner class Data(val iface: String) : BaseObservable() {
val icon: Int get() = TetherType.ofInterface(iface).icon
var active = binder?.active?.contains(iface) == true
inner class Data(val iface: TetheredInterface) : BaseObservable() {
val icon: Int get() = TetherType.ofInterface(iface.name).icon
val active = binder?.active?.contains(iface.name) == true
}
private class InterfaceViewHolder(val binding: ListitemInterfaceBinding) : RecyclerView.ViewHolder(binding.root),
@@ -59,10 +61,9 @@ class TetheringFragment : Fragment(), ServiceConnection {
val context = itemView.context
val data = binding.data!!
if (data.active) context.startService(Intent(context, TetheringService::class.java)
.putExtra(TetheringService.EXTRA_REMOVE_INTERFACE, data.iface))
.putExtra(TetheringService.EXTRA_REMOVE_INTERFACE, data.iface.name))
else ContextCompat.startForegroundService(context, Intent(context, TetheringService::class.java)
.putExtra(TetheringService.EXTRA_ADD_INTERFACE, data.iface))
data.active = !data.active
.putExtra(TetheringService.EXTRA_ADD_INTERFACE, data.iface.name))
}
}
private class ManageViewHolder(view: View) : RecyclerView.ViewHolder(view), View.OnClickListener {
@@ -73,12 +74,18 @@ class TetheringFragment : Fragment(), ServiceConnection {
override fun onClick(v: View?) = itemView.context.startActivity(Intent()
.setClassName("com.android.settings", "com.android.settings.Settings\$TetherSettingsActivity"))
}
class TetheredInterface(val name: String, lookup: Map<String, NetworkInterface>) : Comparable<TetheredInterface> {
val addresses = lookup[name]!!.formatAddresses()
override fun compareTo(other: TetheredInterface) = name.compareTo(other.name)
}
inner class TetheringAdapter : RecyclerView.Adapter<RecyclerView.ViewHolder>() {
private val tethered = SortedList(String::class.java, StringSorter)
private val tethered = SortedList(TetheredInterface::class.java, TetheredInterfaceSorter)
fun update(data: Set<String>) {
val lookup = NetworkInterface.getNetworkInterfaces().asSequence().associateBy { it.name }
tethered.clear()
tethered.addAll(data)
tethered.addAll(data.map { TetheredInterface(it, lookup) })
notifyDataSetChanged()
}
@@ -104,7 +111,7 @@ class TetheringFragment : Fragment(), ServiceConnection {
private var binder: TetheringService.TetheringBinder? = null
val adapter = TetheringAdapter()
private val receiver = broadcastReceiver { _, intent ->
adapter.update(ConnectivityManagerHelper.getTetheredIfaces(intent.extras).toSet())
adapter.update(ConnectivityManagerHelper.getTetheredIfaces(intent.extras))
}
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {

View File

@@ -9,6 +9,8 @@ import android.support.annotation.DrawableRes
import android.util.Log
import android.widget.ImageView
import java.io.IOException
import java.net.Inet4Address
import java.net.NetworkInterface
fun debugLog(tag: String?, message: String?) {
if (BuildConfig.DEBUG) Log.d(tag, message)
@@ -27,6 +29,14 @@ fun intentFilter(vararg actions: String): IntentFilter {
@BindingAdapter("android:src")
fun setImageResource(imageView: ImageView, @DrawableRes resource: Int) = imageView.setImageResource(resource)
fun NetworkInterface.formatAddresses() =
(this.interfaceAddresses.asSequence()
.filter { !it.address.isLinkLocalAddress }
.map { "${it.address.hostAddress}/${it.networkPrefixLength}" }
.toList() +
listOfNotNull(this.hardwareAddress?.joinToString(":") { "%02x".format(it) }))
.joinToString("\n")
private const val NOISYSU_TAG = "NoisySU"
private const val NOISYSU_SUFFIX = "SUCCESS\n"
fun loggerSu(command: String): String? {