summaryrefslogtreecommitdiff
path: root/main/src/ui/java/de/blinkt/openvpn
diff options
context:
space:
mode:
authorArne Schwabe <arne@rfc2549.org>2021-04-15 19:19:12 +0200
committerArne Schwabe <arne@rfc2549.org>2021-04-15 19:19:12 +0200
commit4c7280b50e4fe044f39c42fadf1b3f1789c99658 (patch)
tree72dbdb9ba5479a8583aa9f635ed1df54202b8ab0 /main/src/ui/java/de/blinkt/openvpn
parent8d0f9d2abadbfd516b105294720f379f77fd9877 (diff)
Update gradle files, remove anko dependency
Diffstat (limited to 'main/src/ui/java/de/blinkt/openvpn')
-rw-r--r--main/src/ui/java/de/blinkt/openvpn/fragments/ImportASConfig.kt83
-rw-r--r--main/src/ui/java/de/blinkt/openvpn/fragments/PackageAdapter.kt6
-rw-r--r--main/src/ui/java/de/blinkt/openvpn/fragments/Settings_Allowed_Apps.kt2
3 files changed, 59 insertions, 32 deletions
diff --git a/main/src/ui/java/de/blinkt/openvpn/fragments/ImportASConfig.kt b/main/src/ui/java/de/blinkt/openvpn/fragments/ImportASConfig.kt
index 855caae6..71d1ee7c 100644
--- a/main/src/ui/java/de/blinkt/openvpn/fragments/ImportASConfig.kt
+++ b/main/src/ui/java/de/blinkt/openvpn/fragments/ImportASConfig.kt
@@ -16,17 +16,22 @@ import android.text.InputType
import android.util.Base64
import android.util.Base64.NO_WRAP
import android.util.Log
+import android.view.LayoutInflater
+import android.view.View
+import android.view.ViewGroup
import android.widget.CheckBox
import android.widget.EditText
import android.widget.Toast
import androidx.fragment.app.DialogFragment
+import androidx.lifecycle.lifecycleScope
import de.blinkt.openvpn.R
import de.blinkt.openvpn.activities.ConfigConverter
import de.blinkt.openvpn.core.Preferences
+import kotlinx.coroutines.Dispatchers
+import kotlinx.coroutines.launch
+import kotlinx.coroutines.withContext
import okhttp3.*
import okhttp3.internal.tls.OkHostnameVerifier
-import org.jetbrains.anko.doAsync
-import org.jetbrains.anko.runOnUiThread
import java.io.IOException
import java.security.MessageDigest
import java.security.cert.CertPathValidatorException
@@ -86,6 +91,8 @@ class ImportASConfig : DialogFragment() {
private lateinit var asServername: EditText
private lateinit var asUsername: EditText
private lateinit var asPassword: EditText
+ private lateinit var dialogView: View
+
internal fun getHostNameVerifier(prefs: SharedPreferences): HostnameVerifier {
@@ -210,67 +217,85 @@ class ImportASConfig : DialogFragment() {
return asUri
}
+ override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
+ return dialogView
+ }
+
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
val inflater = requireActivity().layoutInflater
- val view = inflater.inflate(R.layout.import_as_config, null);
+ dialogView = inflater.inflate(R.layout.import_as_config, null);
val builder = AlertDialog.Builder(requireContext())
- builder.setView(view)
-
-
+ builder.setView(dialogView)
builder.setTitle(R.string.import_from_as)
- asServername = view.findViewById(R.id.as_servername)
- asUsername = view.findViewById(R.id.username)
- asPassword = view.findViewById(R.id.password)
- asUseAutlogin = view.findViewById(R.id.request_autologin)
+ asServername = dialogView.findViewById(R.id.as_servername)
+ asUsername = dialogView.findViewById(R.id.username)
+ asPassword = dialogView.findViewById(R.id.password)
+ asUseAutlogin = dialogView.findViewById(R.id.request_autologin)
builder.setPositiveButton(R.string.import_config, null)
builder.setNegativeButton(android.R.string.cancel) { _, _ -> }
val dialog = builder.create()
- dialog.setOnShowListener() { d2 ->
+ return dialog
+ }
+
+ override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
+ super.onViewCreated(view, savedInstanceState)
+
+ dialog!!.setOnShowListener() { d2 ->
val d: AlertDialog = d2 as AlertDialog
d.getButton(AlertDialog.BUTTON_POSITIVE)?.setOnClickListener()
+
{ _ ->
- doAsImport(asUsername.text.toString(), asPassword.text.toString())
+ viewLifecycleOwner.lifecycleScope.launch {
+ doAsImport(asUsername.text.toString(), asPassword.text.toString())
+ }
}
}
- return dialog
}
val crvMessage = Pattern.compile(".*<Message>CRV1:R,E:(.*):(.*):(.*)</Message>.*", Pattern.DOTALL)
- internal fun doAsImport(user: String, password: String) {
- val ab = AlertDialog.Builder(requireContext())
- ab.setTitle("Downloading profile")
- ab.setMessage("Please wait")
- val pleaseWait = ab.show()
- Toast.makeText(context, "Downloading profile", Toast.LENGTH_LONG).show()
- val asProfileUri = getAsUrl(asServername.text.toString(), asUseAutlogin.isChecked)
+ suspend internal fun doAsImport(user: String, password: String) {
+ var pleaseWait:AlertDialog?
+ withContext(Dispatchers.IO)
+ {
+
+ withContext(Dispatchers.Main)
+ {
+ val ab = AlertDialog.Builder(requireContext())
+ ab.setTitle("Downloading profile")
+ ab.setMessage("Please wait")
+ pleaseWait = ab.show()
+
+ Toast.makeText(context, "Downloading profile", Toast.LENGTH_LONG).show()
+ }
+
+
+ val asProfileUri = getAsUrl(asServername.text.toString(), asUseAutlogin.isChecked)
- doAsync {
var e: Exception? = null
try {
val response = fetchProfile(requireContext(), asProfileUri, user, password)
-
if (response == null) {
throw Exception("No Response from Server")
}
val profile = response.body().string()
if (response.code() == 401 && crvMessage.matcher(profile).matches()) {
- requireContext().runOnUiThread {
+ withContext(Dispatchers.Main) {
pleaseWait?.dismiss()
showCRDialog(profile)
}
} else if (response.isSuccessful) {
- activity?.runOnUiThread {
+ withContext(Dispatchers.Main) {
pleaseWait?.dismiss()
val startImport = Intent(activity, ConfigConverter::class.java)
startImport.action = ConfigConverter.IMPORT_PROFILE_DATA
@@ -298,7 +323,7 @@ class ImportASConfig : DialogFragment() {
Log.i("OpenVPN", "Found cert with FP ${fp}: ${firstCert.subjectDN}")
- requireContext().runOnUiThread {
+ withContext(Dispatchers.Main) {
pleaseWait?.dismiss()
@@ -312,7 +337,7 @@ class ImportASConfig : DialogFragment() {
e = null
}
} else if (ce.message != null && ce.message!!.contains("Certificate pinning failure")) {
- requireContext().runOnUiThread {
+ withContext(Dispatchers.Main) {
pleaseWait?.dismiss()
AlertDialog.Builder(requireContext())
@@ -330,7 +355,7 @@ class ImportASConfig : DialogFragment() {
e = ge
}
if (e != null) {
- activity?.runOnUiThread() {
+ withContext(Dispatchers.Main) {
pleaseWait?.dismiss()
AlertDialog.Builder(requireContext())
.setTitle("Import failed")
@@ -364,7 +389,9 @@ class ImportASConfig : DialogFragment() {
.setView(entry)
.setNegativeButton(android.R.string.cancel, null)
.setPositiveButton(R.string.import_config) { _,_ ->
- doAsImport(username, pwprefix + entry.text.toString())
+ viewLifecycleOwner.lifecycleScope.launch {
+ doAsImport(username, pwprefix + entry.text.toString())
+ }
}
.show()
diff --git a/main/src/ui/java/de/blinkt/openvpn/fragments/PackageAdapter.kt b/main/src/ui/java/de/blinkt/openvpn/fragments/PackageAdapter.kt
index 29fcffc7..e2b8028d 100644
--- a/main/src/ui/java/de/blinkt/openvpn/fragments/PackageAdapter.kt
+++ b/main/src/ui/java/de/blinkt/openvpn/fragments/PackageAdapter.kt
@@ -6,6 +6,7 @@
package de.blinkt.openvpn.fragments
import android.Manifest
+import android.app.Activity
import android.content.Context
import android.content.pm.ApplicationInfo
import android.content.pm.PackageManager
@@ -17,7 +18,6 @@ import android.widget.*
import androidx.recyclerview.widget.RecyclerView
import de.blinkt.openvpn.R
import de.blinkt.openvpn.VpnProfile
-import org.jetbrains.anko.runOnUiThread
import java.util.*
internal class AppViewHolder(var rootView : View) : RecyclerView.ViewHolder(rootView) {
@@ -72,7 +72,7 @@ internal class PackageAdapter(c: Context, vp: VpnProfile) : RecyclerView.Adapter
}
- fun populateList(c: Context) {
+ fun populateList(a: Activity) {
val installedPackages = mPm.getInstalledApplications(PackageManager.GET_META_DATA)
// Remove apps not using Internet
@@ -99,7 +99,7 @@ internal class PackageAdapter(c: Context, vp: VpnProfile) : RecyclerView.Adapter
Collections.sort(apps, ApplicationInfo.DisplayNameComparator(mPm))
mPackages = apps
mFilteredData = apps
- c.runOnUiThread { notifyDataSetChanged() }
+ a.runOnUiThread { notifyDataSetChanged() }
}
override fun getItemId(position: Int): Long {
diff --git a/main/src/ui/java/de/blinkt/openvpn/fragments/Settings_Allowed_Apps.kt b/main/src/ui/java/de/blinkt/openvpn/fragments/Settings_Allowed_Apps.kt
index 2c113d71..9ad32a47 100644
--- a/main/src/ui/java/de/blinkt/openvpn/fragments/Settings_Allowed_Apps.kt
+++ b/main/src/ui/java/de/blinkt/openvpn/fragments/Settings_Allowed_Apps.kt
@@ -90,7 +90,7 @@ class Settings_Allowed_Apps : Fragment(), AdapterView.OnItemClickListener, View.
mListView.adapter = packageAdapter
Thread(Runnable {
- packageAdapter.populateList(requireContext())
+ packageAdapter.populateList(activity!!)
activity?.runOnUiThread({
(v.findViewById<View>(R.id.loading_container)).visibility = View.GONE
(v.findViewById<View>(R.id.app_recycler_view)).visibility = View.VISIBLE