summaryrefslogtreecommitdiff
path: root/main/src/ui/java/de/blinkt/openvpn/activities/InternalWebView.kt
blob: 2bad9e812d0150c1b2deab2571a168f0de84dacb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
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)

    }
}