Skip to content

Commit 46c97a3

Browse files
authored
Merge pull request #4487 from kiwix/Fixes#4484
Using the server address provided by libkiwix.
2 parents 5142ea1 + d01e2dc commit 46c97a3

File tree

8 files changed

+46
-12
lines changed

8 files changed

+46
-12
lines changed

app/src/androidTest/java/org/kiwix/kiwixmobile/download/DownloadTest.kt

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ import androidx.test.uiautomator.UiDevice
3838
import com.adevinta.android.barista.interaction.BaristaSleepInteractions
3939
import com.google.android.apps.common.testing.accessibility.framework.AccessibilityCheckResultUtils.matchesCheck
4040
import com.google.android.apps.common.testing.accessibility.framework.checks.DuplicateClickableBoundsCheck
41+
import com.google.android.apps.common.testing.accessibility.framework.checks.SpeakableTextPresentCheck
4142
import com.google.android.apps.common.testing.accessibility.framework.integrations.espresso.AccessibilityValidator
4243
import leakcanary.LeakAssertions
4344
import org.hamcrest.Matchers.anyOf
@@ -113,8 +114,16 @@ class DownloadTest : BaseActivityTest() {
113114
)
114115
}
115116
}
116-
val accessibilityValidator = AccessibilityValidator().setRunChecksFromRootView(true).apply {
117-
setSuppressingResultMatcher(
117+
val accessibilityValidator = AccessibilityValidator().setRunChecksFromRootView(true)
118+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.UPSIDE_DOWN_CAKE) {
119+
accessibilityValidator.setSuppressingResultMatcher(
120+
anyOf(
121+
matchesCheck(DuplicateClickableBoundsCheck::class.java),
122+
matchesCheck(SpeakableTextPresentCheck::class.java)
123+
)
124+
)
125+
} else {
126+
accessibilityValidator.setSuppressingResultMatcher(
118127
anyOf(
119128
matchesCheck(DuplicateClickableBoundsCheck::class.java)
120129
)

app/src/androidTest/java/org/kiwix/kiwixmobile/initial/download/InitialDownloadTest.kt

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
package org.kiwix.kiwixmobile.initial.download
2020

21+
import android.os.Build
2122
import androidx.compose.ui.test.junit4.accessibility.enableAccessibilityChecks
2223
import androidx.compose.ui.test.junit4.createComposeRule
2324
import androidx.core.content.edit
@@ -30,6 +31,7 @@ import androidx.test.uiautomator.UiDevice
3031
import com.adevinta.android.barista.interaction.BaristaSleepInteractions
3132
import com.google.android.apps.common.testing.accessibility.framework.AccessibilityCheckResultUtils.matchesCheck
3233
import com.google.android.apps.common.testing.accessibility.framework.checks.DuplicateClickableBoundsCheck
34+
import com.google.android.apps.common.testing.accessibility.framework.checks.SpeakableTextPresentCheck
3335
import com.google.android.apps.common.testing.accessibility.framework.integrations.espresso.AccessibilityValidator
3436
import leakcanary.LeakAssertions
3537
import org.hamcrest.Matchers.anyOf
@@ -94,8 +96,16 @@ class InitialDownloadTest : BaseActivityTest() {
9496
)
9597
}
9698
}
97-
val accessibilityValidator = AccessibilityValidator().setRunChecksFromRootView(true).apply {
98-
setSuppressingResultMatcher(
99+
val accessibilityValidator = AccessibilityValidator().setRunChecksFromRootView(true)
100+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.UPSIDE_DOWN_CAKE) {
101+
accessibilityValidator.setSuppressingResultMatcher(
102+
anyOf(
103+
matchesCheck(DuplicateClickableBoundsCheck::class.java),
104+
matchesCheck(SpeakableTextPresentCheck::class.java)
105+
)
106+
)
107+
} else {
108+
accessibilityValidator.setSuppressingResultMatcher(
99109
anyOf(
100110
matchesCheck(DuplicateClickableBoundsCheck::class.java)
101111
)

app/src/main/java/org/kiwix/kiwixmobile/webserver/KiwixServer.kt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,15 @@ class KiwixServer @Inject constructor(
8686
}
8787
}
8888

89+
fun getServerAddress(): String? {
90+
val serverAccessUrls = jniKiwixServer.serverAccessUrls
91+
return if (serverAccessUrls.size > 0) {
92+
serverAccessUrls[0]
93+
} else {
94+
null
95+
}
96+
}
97+
8998
fun startServer(port: Int): Boolean {
9099
jniKiwixServer.setPort(port)
91100
return jniKiwixServer.start()

app/src/main/java/org/kiwix/kiwixmobile/webserver/WebServerHelper.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,8 @@ class WebServerHelper @Inject constructor(
108108
ServerUtils.isServerStarted = isStarted
109109
}
110110

111+
fun getServerAddress(): String = kiwixServer?.getServerAddress().orEmpty()
112+
111113
/**
112114
* Starts polling for a valid IP address using a [Flow].
113115
* - Polls every [FINDING_IP_ADDRESS_RETRY_TIME] milliseconds.

app/src/main/java/org/kiwix/kiwixmobile/webserver/ZimHostFragment.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -350,7 +350,7 @@ class ZimHostFragment : BaseFragment(), ZimHostCallbacks, ZimHostContract.View {
350350
presenter.loadBooks(sharedPreferenceUtil.hostedBooks)
351351
}
352352
if (ServerUtils.isServerStarted) {
353-
ip = ServerUtils.getSocketAddress()
353+
ip = ServerUtils.serverAddress
354354
layoutServerStarted()
355355
} else {
356356
layoutServerStopped()
@@ -442,6 +442,7 @@ class ZimHostFragment : BaseFragment(), ZimHostCallbacks, ZimHostContract.View {
442442
// Dismiss dialog when server started.
443443
alertDialogShower.dismiss()
444444
this.ip = ip
445+
ServerUtils.serverAddress = ip
445446
layoutServerStarted()
446447
}
447448

app/src/main/java/org/kiwix/kiwixmobile/webserver/wifi_hotspot/HotspotService.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ import kotlinx.coroutines.withContext
3131
import org.kiwix.kiwixmobile.KiwixApp
3232
import org.kiwix.kiwixmobile.core.R
3333
import org.kiwix.kiwixmobile.core.extensions.registerReceiver
34-
import org.kiwix.kiwixmobile.core.utils.ServerUtils.getSocketAddress
34+
import org.kiwix.kiwixmobile.core.utils.ServerUtils.serverAddress
3535
import org.kiwix.kiwixmobile.webserver.WebServerHelper
3636
import org.kiwix.kiwixmobile.webserver.ZimHostCallbacks
3737
import org.kiwix.kiwixmobile.webserver.ZimHostFragment
@@ -88,7 +88,7 @@ class HotspotService :
8888
webServerHelper?.startServerHelper(it, restartServer)
8989
}
9090
if (serverStatus?.isServerStarted == true) {
91-
zimHostCallbacks?.onServerStarted(getSocketAddress())
91+
zimHostCallbacks?.onServerStarted(webServerHelper?.getServerAddress().orEmpty())
9292
startForegroundNotificationHelper()
9393
if (!restartServer) {
9494
Toast.makeText(
@@ -141,7 +141,7 @@ class HotspotService :
141141
private fun startForegroundNotificationHelper() {
142142
startForeground(
143143
HotspotNotificationManager.HOTSPOT_NOTIFICATION_ID,
144-
hotspotNotificationManager?.buildForegroundNotification(getSocketAddress())
144+
hotspotNotificationManager?.buildForegroundNotification(serverAddress)
145145
)
146146
}
147147

buildSrc/src/main/kotlin/Versions.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ object Versions {
8888

8989
const val androidx_activity: String = "1.9.3"
9090

91-
const val libkiwix: String = "2.2.4"
91+
const val libkiwix: String = "2.4.0"
9292

9393
const val material: String = "1.12.0"
9494

core/src/main/java/org/kiwix/kiwixmobile/core/utils/ServerUtils.kt

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,12 @@ object ServerUtils {
2626
@JvmField var port = 0
2727

2828
@JvmField var isServerStarted = false
29+
30+
/**
31+
* Stores the server address returned by libkiwix so that when the Hotspot screen
32+
* becomes visible again, the server address can be displayed.
33+
*/
34+
@JvmField var serverAddress = ""
2935
const val INVALID_IP = "-1" // To remove extra characters from IP for Android Pie
3036
private const val TAG = "ServerUtils"
3137

@@ -62,9 +68,6 @@ object ServerUtils {
6268
return ipRegex.find(ip, 0)?.value ?: throw IllegalArgumentException()
6369
}
6470

65-
@JvmStatic fun getSocketAddress(): String =
66-
"http://${getIpAddress()}:$port".replace("\n", "")
67-
6871
@JvmStatic fun getIp(): String? =
6972
getIpAddress()?.replace("\n", "")?.takeIf(String::isNotEmpty) ?: INVALID_IP
7073
}

0 commit comments

Comments
 (0)