diff --git a/mobile/src/main/java/be/mygod/vpnhotspot/ArpCache.kt b/mobile/src/main/java/be/mygod/vpnhotspot/ArpCache.kt index 238dea61..d421d6bd 100644 --- a/mobile/src/main/java/be/mygod/vpnhotspot/ArpCache.kt +++ b/mobile/src/main/java/be/mygod/vpnhotspot/ArpCache.kt @@ -2,7 +2,7 @@ package be.mygod.vpnhotspot import java.io.File -class ArpCache(downstream: String) : HashMap() { +class ArpCache(downstream: String? = null) : HashMap() { 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() { 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]) } } } diff --git a/mobile/src/main/java/be/mygod/vpnhotspot/HotspotService.kt b/mobile/src/main/java/be/mygod/vpnhotspot/HotspotService.kt index f5f70dfa..735ad334 100644 --- a/mobile/src/main/java/be/mygod/vpnhotspot/HotspotService.kt +++ b/mobile/src/main/java/be/mygod/vpnhotspot/HotspotService.kt @@ -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) { diff --git a/mobile/src/main/java/be/mygod/vpnhotspot/MainActivity.kt b/mobile/src/main/java/be/mygod/vpnhotspot/MainActivity.kt index e54f9558..a11b20aa 100644 --- a/mobile/src/main/java/be/mygod/vpnhotspot/MainActivity.kt +++ b/mobile/src/main/java/be/mygod/vpnhotspot/MainActivity.kt @@ -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 }