From f15cbe2d732b374b81de23e0453af5eb0f9a0517 Mon Sep 17 00:00:00 2001 From: Antecer Date: Sat, 6 Apr 2024 23:09:54 +0800 Subject: [PATCH] =?UTF-8?q?web=E6=9C=8D=E5=8A=A1=E9=80=9A=E7=9F=A5?= =?UTF-8?q?=E6=A0=8F=E6=94=AF=E6=8C=81=E5=A4=9A=E4=B8=AA=E5=9C=B0=E5=9D=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/io/legado/app/service/WebService.kt | 20 ++++++++++--------- .../java/io/legado/app/utils/NetworkUtils.kt | 13 +++++------- 2 files changed, 16 insertions(+), 17 deletions(-) diff --git a/app/src/main/java/io/legado/app/service/WebService.kt b/app/src/main/java/io/legado/app/service/WebService.kt index 6340f7fa8..446693307 100644 --- a/app/src/main/java/io/legado/app/service/WebService.kt +++ b/app/src/main/java/io/legado/app/service/WebService.kt @@ -76,13 +76,14 @@ class WebService : BaseService() { upTile(true) networkChangedListener.register() networkChangedListener.onNetworkChanged = { - val address = NetworkUtils.getLocalIPAddress() - if (address == null) { - hostAddress = getString(R.string.network_connection_unavailable) - notificationContent = hostAddress + val addressList = NetworkUtils.getLocalIPAddress() + if (addressList.any()) { + val hostList = addressList.map { address -> getString(R.string.http_ip, address.hostAddress, getPort()) } + hostAddress = hostList.first() + notificationContent = hostList.joinToString(separator = "\n") startForegroundNotification() } else { - hostAddress = getString(R.string.http_ip, address.hostAddress, getPort()) + hostAddress = getString(R.string.network_connection_unavailable) notificationContent = hostAddress startForegroundNotification() } @@ -130,18 +131,19 @@ class WebService : BaseService() { if (webSocketServer?.isAlive == true) { webSocketServer?.stop() } - val address = NetworkUtils.getLocalIPAddress() - if (address != null) { + val addressList = NetworkUtils.getLocalIPAddress() + if (addressList.any()) { val port = getPort() httpServer = HttpServer(port) webSocketServer = WebSocketServer(port + 1) try { httpServer?.start() webSocketServer?.start(1000 * 30) // 通信超时设置 - hostAddress = getString(R.string.http_ip, address.hostAddress, port) + val hostList = addressList.map { address -> getString(R.string.http_ip, address.hostAddress, getPort()) } + hostAddress = hostList.first() + notificationContent = hostList.joinToString(separator = "\n") isRun = true postEvent(EventBus.WEB_SERVICE, hostAddress) - notificationContent = hostAddress startForegroundNotification() } catch (e: IOException) { toastOnUi(e.localizedMessage ?: "") diff --git a/app/src/main/java/io/legado/app/utils/NetworkUtils.kt b/app/src/main/java/io/legado/app/utils/NetworkUtils.kt index d74e88f32..a0e47b649 100644 --- a/app/src/main/java/io/legado/app/utils/NetworkUtils.kt +++ b/app/src/main/java/io/legado/app/utils/NetworkUtils.kt @@ -188,16 +188,16 @@ object NetworkUtils { /** * Get local Ip address. */ - fun getLocalIPAddress(): InetAddress? { + fun getLocalIPAddress(): List { val enumeration: Enumeration try { enumeration = NetworkInterface.getNetworkInterfaces() } catch (e: SocketException) { e.printOnDebug() - return null + return mutableListOf() } - var fallbackAddress: InetAddress? = null + var fallbackAddress: MutableList = mutableListOf() while (enumeration.hasMoreElements()) { val nif = enumeration.nextElement() @@ -205,11 +205,8 @@ object NetworkUtils { while (addresses.hasMoreElements()) { val address = addresses.nextElement() if (!address.isLoopbackAddress && isIPv4Address(address.hostAddress)) { - if (nif.name?.startsWith("wl") == true) { - return address - } - if (fallbackAddress == null) { - fallbackAddress = address + if (nif.name?.startsWith("wlan") == true) { + fallbackAddress.add(address) } } }