mirror of
https://github.com/gedoor/legado.git
synced 2024-07-06 23:47:49 +08:00
web服务通知栏支持多个地址
This commit is contained in:
parent
328f01d0bf
commit
f15cbe2d73
@ -76,13 +76,14 @@ class WebService : BaseService() {
|
|||||||
upTile(true)
|
upTile(true)
|
||||||
networkChangedListener.register()
|
networkChangedListener.register()
|
||||||
networkChangedListener.onNetworkChanged = {
|
networkChangedListener.onNetworkChanged = {
|
||||||
val address = NetworkUtils.getLocalIPAddress()
|
val addressList = NetworkUtils.getLocalIPAddress()
|
||||||
if (address == null) {
|
if (addressList.any()) {
|
||||||
hostAddress = getString(R.string.network_connection_unavailable)
|
val hostList = addressList.map { address -> getString(R.string.http_ip, address.hostAddress, getPort()) }
|
||||||
notificationContent = hostAddress
|
hostAddress = hostList.first()
|
||||||
|
notificationContent = hostList.joinToString(separator = "\n")
|
||||||
startForegroundNotification()
|
startForegroundNotification()
|
||||||
} else {
|
} else {
|
||||||
hostAddress = getString(R.string.http_ip, address.hostAddress, getPort())
|
hostAddress = getString(R.string.network_connection_unavailable)
|
||||||
notificationContent = hostAddress
|
notificationContent = hostAddress
|
||||||
startForegroundNotification()
|
startForegroundNotification()
|
||||||
}
|
}
|
||||||
@ -130,18 +131,19 @@ class WebService : BaseService() {
|
|||||||
if (webSocketServer?.isAlive == true) {
|
if (webSocketServer?.isAlive == true) {
|
||||||
webSocketServer?.stop()
|
webSocketServer?.stop()
|
||||||
}
|
}
|
||||||
val address = NetworkUtils.getLocalIPAddress()
|
val addressList = NetworkUtils.getLocalIPAddress()
|
||||||
if (address != null) {
|
if (addressList.any()) {
|
||||||
val port = getPort()
|
val port = getPort()
|
||||||
httpServer = HttpServer(port)
|
httpServer = HttpServer(port)
|
||||||
webSocketServer = WebSocketServer(port + 1)
|
webSocketServer = WebSocketServer(port + 1)
|
||||||
try {
|
try {
|
||||||
httpServer?.start()
|
httpServer?.start()
|
||||||
webSocketServer?.start(1000 * 30) // 通信超时设置
|
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
|
isRun = true
|
||||||
postEvent(EventBus.WEB_SERVICE, hostAddress)
|
postEvent(EventBus.WEB_SERVICE, hostAddress)
|
||||||
notificationContent = hostAddress
|
|
||||||
startForegroundNotification()
|
startForegroundNotification()
|
||||||
} catch (e: IOException) {
|
} catch (e: IOException) {
|
||||||
toastOnUi(e.localizedMessage ?: "")
|
toastOnUi(e.localizedMessage ?: "")
|
||||||
|
@ -188,16 +188,16 @@ object NetworkUtils {
|
|||||||
/**
|
/**
|
||||||
* Get local Ip address.
|
* Get local Ip address.
|
||||||
*/
|
*/
|
||||||
fun getLocalIPAddress(): InetAddress? {
|
fun getLocalIPAddress(): List<InetAddress> {
|
||||||
val enumeration: Enumeration<NetworkInterface>
|
val enumeration: Enumeration<NetworkInterface>
|
||||||
try {
|
try {
|
||||||
enumeration = NetworkInterface.getNetworkInterfaces()
|
enumeration = NetworkInterface.getNetworkInterfaces()
|
||||||
} catch (e: SocketException) {
|
} catch (e: SocketException) {
|
||||||
e.printOnDebug()
|
e.printOnDebug()
|
||||||
return null
|
return mutableListOf()
|
||||||
}
|
}
|
||||||
|
|
||||||
var fallbackAddress: InetAddress? = null
|
var fallbackAddress: MutableList<InetAddress> = mutableListOf()
|
||||||
|
|
||||||
while (enumeration.hasMoreElements()) {
|
while (enumeration.hasMoreElements()) {
|
||||||
val nif = enumeration.nextElement()
|
val nif = enumeration.nextElement()
|
||||||
@ -205,11 +205,8 @@ object NetworkUtils {
|
|||||||
while (addresses.hasMoreElements()) {
|
while (addresses.hasMoreElements()) {
|
||||||
val address = addresses.nextElement()
|
val address = addresses.nextElement()
|
||||||
if (!address.isLoopbackAddress && isIPv4Address(address.hostAddress)) {
|
if (!address.isLoopbackAddress && isIPv4Address(address.hostAddress)) {
|
||||||
if (nif.name?.startsWith("wl") == true) {
|
if (nif.name?.startsWith("wlan") == true) {
|
||||||
return address
|
fallbackAddress.add(address)
|
||||||
}
|
|
||||||
if (fallbackAddress == null) {
|
|
||||||
fallbackAddress = address
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user