From 00b10ed048cc1f6490e7ca3e0530dd4471ca5a40 Mon Sep 17 00:00:00 2001 From: Arne Schwabe Date: Wed, 27 May 2020 13:39:55 +0200 Subject: Implement internal webview for authenticating when OPEN_URL is used --- .../blinkt/openvpn/activities/InternalWebView.kt | 99 ++++++++++++++++++++++ 1 file changed, 99 insertions(+) create mode 100644 main/src/ui/java/de/blinkt/openvpn/activities/InternalWebView.kt (limited to 'main/src/ui/java/de/blinkt/openvpn/activities/InternalWebView.kt') diff --git a/main/src/ui/java/de/blinkt/openvpn/activities/InternalWebView.kt b/main/src/ui/java/de/blinkt/openvpn/activities/InternalWebView.kt new file mode 100644 index 00000000..2bad9e81 --- /dev/null +++ b/main/src/ui/java/de/blinkt/openvpn/activities/InternalWebView.kt @@ -0,0 +1,99 @@ +/* + * Copyright (c) 2012-2020 Arne Schwabe + * Distributed under the GNU GPL v2 with additional terms. For full terms see the file doc/LICENSE.txt + */ + +package de.blinkt.openvpn.activities + +import android.annotation.SuppressLint +import android.annotation.TargetApi +import android.content.Intent +import android.os.Build +import android.os.Bundle +import android.util.Log +import android.webkit.JavascriptInterface +import android.webkit.WebResourceRequest +import android.webkit.WebView +import android.webkit.WebViewClient +import android.widget.TextView +import androidx.annotation.RequiresApi +import androidx.appcompat.app.AppCompatActivity +import de.blinkt.openvpn.R +import de.blinkt.openvpn.VpnProfile +import org.json.JSONObject + +class InternalWebView : AppCompatActivity() { + + lateinit var webView: WebView + lateinit var urlTextView: TextView + + @SuppressLint("SetJavaScriptEnabled") + override fun onCreate(savedInstanceState: Bundle?) { + + super.onCreate(savedInstanceState) + setContentView(R.layout.webview_internal) + + webView = findViewById(R.id.internal_webview) + urlTextView = findViewById(R.id.url_textview) + + + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) + attachMessageHandler() + + webView.loadUrl(intent.data.toString()) + + webView.settings.javaScriptEnabled = true + webView.settings.userAgentString = VpnProfile.getVersionEnvString(this) + + webView.webViewClient = object: WebViewClient() { + @RequiresApi(Build.VERSION_CODES.LOLLIPOP) + override fun shouldOverrideUrlLoading(view: WebView?, request: WebResourceRequest?): Boolean { + urlTextView.text = request?.url?.toString(); + return super.shouldOverrideUrlLoading(view, request) + } + + } + urlTextView.text = intent.data.toString() + + setTitle(R.string.internal_web_view) + + } + + @JavascriptInterface + fun postMessage(json: String?, transferList: String?): Boolean { + val jObejct = JSONObject(json) + + val action = jObejct.getString("type") + Log.i("OpenVPN,InternalWebview", json + " ---- " + transferList) + + if (action == "ACTION_REQUIRED") { + // Should show the hidden webview, nothing for us to do + return true + } + + if (action == "CONNECT_SUCCESS" || action == "CONNECT_FAILED") { + runOnUiThread({finish()}) + } + + /* runOnUiThread({ + Toast.makeText(this, json + " ---- " + transferList, Toast.LENGTH_LONG).show() + }) */ + return true + } + + + @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1) + private fun attachMessageHandler() { + webView.addJavascriptInterface(this, "appEvent") + } + + override fun onResume() { + super.onResume() + + } + + override fun onNewIntent(intent: Intent?) { + super.onNewIntent(intent) + + } +} \ No newline at end of file -- cgit v1.2.3