@@ -5,6 +5,7 @@ import android.content.Context
5
5
import android.content.Intent
6
6
import android.os.Bundle
7
7
import android.webkit.ConsoleMessage
8
+ import android.webkit.CookieManager
8
9
import android.webkit.WebChromeClient
9
10
import android.webkit.WebResourceRequest
10
11
import android.webkit.WebView
@@ -28,13 +29,20 @@ import androidx.compose.runtime.remember
28
29
import androidx.compose.ui.Modifier
29
30
import androidx.compose.ui.viewinterop.AndroidView
30
31
import fr.free.nrw.commons.R
32
+ import fr.free.nrw.commons.di.ApplicationlessInjection
33
+ import fr.free.nrw.commons.wikidata.cookies.CommonsCookieJar
34
+ import okhttp3.HttpUrl.Companion.toHttpUrl
31
35
import timber.log.Timber
36
+ import javax.inject.Inject
32
37
33
38
/* *
34
39
* SingleWebViewActivity is a reusable activity webView based on a given url(initial url) and
35
40
* closes itself when a specified success URL is reached to success url.
36
41
*/
37
42
class SingleWebViewActivity : ComponentActivity () {
43
+ @Inject
44
+ lateinit var cookieJar: CommonsCookieJar
45
+
38
46
@OptIn(ExperimentalMaterial3Api ::class )
39
47
override fun onCreate (savedInstanceState : Bundle ? ) {
40
48
super .onCreate(savedInstanceState)
@@ -44,6 +52,11 @@ class SingleWebViewActivity : ComponentActivity() {
44
52
finish()
45
53
return
46
54
}
55
+ ApplicationlessInjection
56
+ .getInstance(applicationContext)
57
+ .commonsApplicationComponent
58
+ .inject(this )
59
+ setCookies(url)
47
60
enableEdgeToEdge()
48
61
setContent {
49
62
Scaffold (
@@ -131,6 +144,7 @@ class SingleWebViewActivity : ComponentActivity() {
131
144
132
145
override fun onPageFinished (view : WebView ? , url : String? ) {
133
146
super .onPageFinished(view, url)
147
+ setCookies(url.orEmpty())
134
148
}
135
149
136
150
}
@@ -152,6 +166,20 @@ class SingleWebViewActivity : ComponentActivity() {
152
166
153
167
}
154
168
169
+ /* *
170
+ * Sets cookies for the given URL using the cookies stored in the `CommonsCookieJar`.
171
+ *
172
+ * @param url The URL for which cookies need to be set.
173
+ */
174
+ private fun setCookies (url : String ) {
175
+ CookieManager .getInstance().let {
176
+ val cookies = cookieJar.loadForRequest(url.toHttpUrl())
177
+ for (cookie in cookies) {
178
+ it.setCookie(url, cookie.toString())
179
+ }
180
+ }
181
+ }
182
+
155
183
companion object {
156
184
private const val VANISH_ACCOUNT_URL = " VanishAccountUrl"
157
185
private const val VANISH_ACCOUNT_SUCCESS_URL = " vanishAccountSuccessUrl"
0 commit comments