|
| 1 | +package fr.free.nrw.commons.activity |
| 2 | + |
| 3 | +import android.annotation.SuppressLint |
| 4 | +import android.content.Context |
| 5 | +import android.content.Intent |
| 6 | +import android.os.Bundle |
| 7 | +import android.webkit.ConsoleMessage |
| 8 | +import android.webkit.WebChromeClient |
| 9 | +import android.webkit.WebResourceRequest |
| 10 | +import android.webkit.WebView |
| 11 | +import android.webkit.WebViewClient |
| 12 | +import androidx.activity.ComponentActivity |
| 13 | +import androidx.activity.compose.setContent |
| 14 | +import androidx.activity.enableEdgeToEdge |
| 15 | +import androidx.compose.foundation.layout.fillMaxSize |
| 16 | +import androidx.compose.foundation.layout.padding |
| 17 | +import androidx.compose.material.icons.Icons |
| 18 | +import androidx.compose.material.icons.automirrored.filled.ArrowBack |
| 19 | +import androidx.compose.material3.ExperimentalMaterial3Api |
| 20 | +import androidx.compose.material3.Icon |
| 21 | +import androidx.compose.material3.IconButton |
| 22 | +import androidx.compose.material3.Scaffold |
| 23 | +import androidx.compose.material3.Text |
| 24 | +import androidx.compose.material3.TopAppBar |
| 25 | +import androidx.compose.runtime.Composable |
| 26 | +import androidx.compose.runtime.mutableStateOf |
| 27 | +import androidx.compose.runtime.remember |
| 28 | +import androidx.compose.ui.Modifier |
| 29 | +import androidx.compose.ui.viewinterop.AndroidView |
| 30 | +import fr.free.nrw.commons.R |
| 31 | +import timber.log.Timber |
| 32 | + |
| 33 | +/** |
| 34 | + * SingleWebViewActivity is a reusable activity webView based on a given url(initial url) and |
| 35 | + * closes itself when a specified success URL is reached to success url. |
| 36 | + */ |
| 37 | +class SingleWebViewActivity : ComponentActivity() { |
| 38 | + @OptIn(ExperimentalMaterial3Api::class) |
| 39 | + override fun onCreate(savedInstanceState: Bundle?) { |
| 40 | + super.onCreate(savedInstanceState) |
| 41 | + val url = intent.getStringExtra(VANISH_ACCOUNT_URL) |
| 42 | + val successUrl = intent.getStringExtra(VANISH_ACCOUNT_SUCCESS_URL) |
| 43 | + if (url == null || successUrl == null) { |
| 44 | + finish() |
| 45 | + return |
| 46 | + } |
| 47 | + enableEdgeToEdge() |
| 48 | + setContent { |
| 49 | + Scaffold( |
| 50 | + topBar = { |
| 51 | + TopAppBar( |
| 52 | + modifier = Modifier, |
| 53 | + title = { Text(getString(R.string.vanish_account)) }, |
| 54 | + navigationIcon = { |
| 55 | + IconButton( |
| 56 | + onClick = { |
| 57 | + // Close the WebView Activity if the user taps the back button |
| 58 | + finish() |
| 59 | + }, |
| 60 | + ) { |
| 61 | + Icon( |
| 62 | + imageVector = Icons.AutoMirrored.Filled.ArrowBack, |
| 63 | + // TODO("Add contentDescription) |
| 64 | + contentDescription = "" |
| 65 | + ) |
| 66 | + } |
| 67 | + } |
| 68 | + ) |
| 69 | + }, |
| 70 | + content = { |
| 71 | + WebViewComponent( |
| 72 | + url = url, |
| 73 | + successUrl = successUrl, |
| 74 | + onSuccess = { |
| 75 | + // TODO Redirect the user to login screen like we do when the user logout's |
| 76 | + finish() |
| 77 | + }, |
| 78 | + modifier = Modifier |
| 79 | + .fillMaxSize() |
| 80 | + .padding(it) |
| 81 | + ) |
| 82 | + } |
| 83 | + ) |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + |
| 88 | + /** |
| 89 | + * @param url The initial URL which we are loading in the WebView. |
| 90 | + * @param successUrl The URL that, when reached, triggers the `onSuccess` callback. |
| 91 | + * @param onSuccess A callback that is invoked when the current url of webView is successUrl. |
| 92 | + * This is used when we want to close when the webView once a success url is hit. |
| 93 | + * @param modifier An optional [Modifier] to customize the layout or appearance of the WebView. |
| 94 | + */ |
| 95 | + @SuppressLint("SetJavaScriptEnabled") |
| 96 | + @Composable |
| 97 | + private fun WebViewComponent( |
| 98 | + url: String, |
| 99 | + successUrl: String, |
| 100 | + onSuccess: () -> Unit, |
| 101 | + modifier: Modifier = Modifier |
| 102 | + ) { |
| 103 | + val webView = remember { mutableStateOf<WebView?>(null) } |
| 104 | + AndroidView( |
| 105 | + modifier = modifier, |
| 106 | + factory = { |
| 107 | + WebView(it).apply { |
| 108 | + settings.apply { |
| 109 | + javaScriptEnabled = true |
| 110 | + domStorageEnabled = true |
| 111 | + javaScriptCanOpenWindowsAutomatically = true |
| 112 | + |
| 113 | + } |
| 114 | + webViewClient = object : WebViewClient() { |
| 115 | + override fun shouldOverrideUrlLoading( |
| 116 | + view: WebView?, |
| 117 | + request: WebResourceRequest? |
| 118 | + ): Boolean { |
| 119 | + |
| 120 | + request?.url?.let { url -> |
| 121 | + Timber.d("URL Loading: $url") |
| 122 | + if (url.toString() == successUrl) { |
| 123 | + Timber.d("Success URL detected. Closing WebView.") |
| 124 | + onSuccess() // Close the activity |
| 125 | + return true |
| 126 | + } |
| 127 | + return false |
| 128 | + } |
| 129 | + return false |
| 130 | + } |
| 131 | + |
| 132 | + override fun onPageFinished(view: WebView?, url: String?) { |
| 133 | + super.onPageFinished(view, url) |
| 134 | + } |
| 135 | + |
| 136 | + } |
| 137 | + |
| 138 | + webChromeClient = object : WebChromeClient() { |
| 139 | + override fun onConsoleMessage(message: ConsoleMessage): Boolean { |
| 140 | + Timber.d("Console: ${message.message()} -- From line ${message.lineNumber()} of ${message.sourceId()}") |
| 141 | + return true |
| 142 | + } |
| 143 | + } |
| 144 | + |
| 145 | + loadUrl(url) |
| 146 | + } |
| 147 | + }, |
| 148 | + update = { |
| 149 | + webView.value = it |
| 150 | + } |
| 151 | + ) |
| 152 | + |
| 153 | + } |
| 154 | + |
| 155 | + companion object { |
| 156 | + private const val VANISH_ACCOUNT_URL = "VanishAccountUrl" |
| 157 | + private const val VANISH_ACCOUNT_SUCCESS_URL = "vanishAccountSuccessUrl" |
| 158 | + |
| 159 | + /** |
| 160 | + * Launch the WebViewActivity with the specified URL and success URL. |
| 161 | + * @param context The context from which the activity is launched. |
| 162 | + * @param url The initial URL to load in the WebView. |
| 163 | + * @param successUrl The URL that triggers the WebView to close when matched. |
| 164 | + */ |
| 165 | + fun showWebView( |
| 166 | + context: Context, |
| 167 | + url: String, |
| 168 | + successUrl: String |
| 169 | + ) { |
| 170 | + val intent = Intent( |
| 171 | + context, |
| 172 | + SingleWebViewActivity::class.java |
| 173 | + ).apply { |
| 174 | + putExtra(VANISH_ACCOUNT_URL, url) |
| 175 | + putExtra(VANISH_ACCOUNT_SUCCESS_URL, successUrl) |
| 176 | + } |
| 177 | + context.startActivity(intent) |
| 178 | + } |
| 179 | + } |
| 180 | +} |
| 181 | + |
0 commit comments