Various bugfixes

This commit is contained in:
Mygod
2018-01-03 21:28:56 +08:00
parent aab5a6a432
commit 3e7fae95cf
3 changed files with 16 additions and 10 deletions

View File

@@ -2,7 +2,7 @@ package be.mygod.vpnhotspot
import java.io.File import java.io.File
class ArpCache(downstream: String) : HashMap<String, String>() { class ArpCache(downstream: String? = null) : HashMap<String, String>() {
companion object { companion object {
private val spaces = " +".toPattern() private val spaces = " +".toPattern()
private val mac = "^([0-9a-f]{2}:){5}[0-9a-f]{2}$".toPattern() private val mac = "^([0-9a-f]{2}:){5}[0-9a-f]{2}$".toPattern()
@@ -13,7 +13,8 @@ class ArpCache(downstream: String) : HashMap<String, String>() {
for (line in it) { for (line in it) {
val parts = line.split(spaces) val parts = line.split(spaces)
// IP address HW type Flags HW address Mask Device // IP address HW type Flags HW address Mask Device
if (parts.size >= 6 && parts[5] == downstream && mac.matcher(parts[3]).matches()) put(parts[3], parts[0]) if (parts.size >= 4 && (downstream == null || parts.getOrNull(5) == downstream) &&
mac.matcher(parts[3]).matches()) put(parts[3], parts[0])
} }
} }
} }

View File

@@ -80,7 +80,9 @@ class HotspotService : Service(), WifiP2pManager.ChannelListener {
doStart(group) doStart(group)
} else startFailure("Something went wrong, please check logcat.") } else startFailure("Something went wrong, please check logcat.")
} }
this@HotspotService.group = group
binder.data?.onGroupChanged() binder.data?.onGroupChanged()
showNotification(group)
Log.d(TAG, "${intent.action}: $info, $net, $group") Log.d(TAG, "${intent.action}: $info, $net, $group")
} }
WifiP2pManager.WIFI_P2P_THIS_DEVICE_CHANGED_ACTION -> { WifiP2pManager.WIFI_P2P_THIS_DEVICE_CHANGED_ACTION -> {
@@ -147,7 +149,7 @@ class HotspotService : Service(), WifiP2pManager.ChannelListener {
private fun startFailure(msg: String) { private fun startFailure(msg: String) {
Toast.makeText(this@HotspotService, msg, Toast.LENGTH_SHORT).show() Toast.makeText(this@HotspotService, msg, Toast.LENGTH_SHORT).show()
startForeground(0, NotificationCompat.Builder(this@HotspotService, CHANNEL).build()) showNotification()
clean() clean()
} }
private fun doStart() = p2pManager.createGroup(channel, object : WifiP2pManager.ActionListener { private fun doStart() = p2pManager.createGroup(channel, object : WifiP2pManager.ActionListener {
@@ -157,16 +159,19 @@ class HotspotService : Service(), WifiP2pManager.ChannelListener {
private fun doStart(group: WifiP2pGroup) { private fun doStart(group: WifiP2pGroup) {
this.group = group this.group = group
status = Status.ACTIVE status = Status.ACTIVE
startForeground(1, NotificationCompat.Builder(this@HotspotService, CHANNEL) showNotification(group)
}
private fun showNotification(group: WifiP2pGroup? = null) = startForeground(1,
NotificationCompat.Builder(this@HotspotService, CHANNEL)
.setWhen(0) .setWhen(0)
.setColor(ContextCompat.getColor(this@HotspotService, R.color.colorPrimary)) .setColor(ContextCompat.getColor(this@HotspotService, R.color.colorPrimary))
.setContentTitle(group.networkName) .setContentTitle(group?.networkName)
.setContentText(group.passphrase) .setContentText(group?.passphrase)
.setSubText("${group?.clientList?.size ?: 0} connected device(s)")
.setSmallIcon(R.drawable.ic_device_wifi_tethering) .setSmallIcon(R.drawable.ic_device_wifi_tethering)
.setContentIntent(PendingIntent.getActivity(this, 0, .setContentIntent(PendingIntent.getActivity(this, 0,
Intent(this, MainActivity::class.java), PendingIntent.FLAG_UPDATE_CURRENT)) Intent(this, MainActivity::class.java), PendingIntent.FLAG_UPDATE_CURRENT))
.build()) .build())
}
private fun clean() { private fun clean() {
if (receiverRegistered) { if (receiverRegistered) {

View File

@@ -38,9 +38,9 @@ class MainActivity : AppCompatActivity(), ServiceConnection {
val binder = binder val binder = binder
when (binder?.status) { when (binder?.status) {
HotspotService.Status.IDLE -> HotspotService.Status.IDLE ->
ContextCompat.startForegroundService(this@MainActivity, if (value) ContextCompat.startForegroundService(this@MainActivity,
Intent(this@MainActivity, HotspotService::class.java)) Intent(this@MainActivity, HotspotService::class.java))
HotspotService.Status.ACTIVE -> binder.shutdown() HotspotService.Status.ACTIVE -> if (!value) binder.shutdown()
} }
} }
@@ -71,7 +71,7 @@ class MainActivity : AppCompatActivity(), ServiceConnection {
if (data.running) { if (data.running) {
owner = binder.service.group.owner owner = binder.service.group.owner
clients = binder.service.group.clientList clients = binder.service.group.clientList
arpCache = ArpCache(binder.service.downstream!!) arpCache = ArpCache(binder.service.downstream)
} else owner = null } else owner = null
notifyDataSetChanged() // recreate everything notifyDataSetChanged() // recreate everything
} }