Various bugfixes
This commit is contained in:
@@ -2,7 +2,7 @@ package be.mygod.vpnhotspot
|
||||
|
||||
import java.io.File
|
||||
|
||||
class ArpCache(downstream: String) : HashMap<String, String>() {
|
||||
class ArpCache(downstream: String? = null) : HashMap<String, String>() {
|
||||
companion object {
|
||||
private val spaces = " +".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) {
|
||||
val parts = line.split(spaces)
|
||||
// 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])
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -80,7 +80,9 @@ class HotspotService : Service(), WifiP2pManager.ChannelListener {
|
||||
doStart(group)
|
||||
} else startFailure("Something went wrong, please check logcat.")
|
||||
}
|
||||
this@HotspotService.group = group
|
||||
binder.data?.onGroupChanged()
|
||||
showNotification(group)
|
||||
Log.d(TAG, "${intent.action}: $info, $net, $group")
|
||||
}
|
||||
WifiP2pManager.WIFI_P2P_THIS_DEVICE_CHANGED_ACTION -> {
|
||||
@@ -147,7 +149,7 @@ class HotspotService : Service(), WifiP2pManager.ChannelListener {
|
||||
|
||||
private fun startFailure(msg: String) {
|
||||
Toast.makeText(this@HotspotService, msg, Toast.LENGTH_SHORT).show()
|
||||
startForeground(0, NotificationCompat.Builder(this@HotspotService, CHANNEL).build())
|
||||
showNotification()
|
||||
clean()
|
||||
}
|
||||
private fun doStart() = p2pManager.createGroup(channel, object : WifiP2pManager.ActionListener {
|
||||
@@ -157,16 +159,19 @@ class HotspotService : Service(), WifiP2pManager.ChannelListener {
|
||||
private fun doStart(group: WifiP2pGroup) {
|
||||
this.group = group
|
||||
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)
|
||||
.setColor(ContextCompat.getColor(this@HotspotService, R.color.colorPrimary))
|
||||
.setContentTitle(group.networkName)
|
||||
.setContentText(group.passphrase)
|
||||
.setContentTitle(group?.networkName)
|
||||
.setContentText(group?.passphrase)
|
||||
.setSubText("${group?.clientList?.size ?: 0} connected device(s)")
|
||||
.setSmallIcon(R.drawable.ic_device_wifi_tethering)
|
||||
.setContentIntent(PendingIntent.getActivity(this, 0,
|
||||
Intent(this, MainActivity::class.java), PendingIntent.FLAG_UPDATE_CURRENT))
|
||||
.build())
|
||||
}
|
||||
|
||||
private fun clean() {
|
||||
if (receiverRegistered) {
|
||||
|
||||
@@ -38,9 +38,9 @@ class MainActivity : AppCompatActivity(), ServiceConnection {
|
||||
val binder = binder
|
||||
when (binder?.status) {
|
||||
HotspotService.Status.IDLE ->
|
||||
ContextCompat.startForegroundService(this@MainActivity,
|
||||
if (value) ContextCompat.startForegroundService(this@MainActivity,
|
||||
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) {
|
||||
owner = binder.service.group.owner
|
||||
clients = binder.service.group.clientList
|
||||
arpCache = ArpCache(binder.service.downstream!!)
|
||||
arpCache = ArpCache(binder.service.downstream)
|
||||
} else owner = null
|
||||
notifyDataSetChanged() // recreate everything
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user