diff options
Diffstat (limited to 'app/src/main/java/org/pacien/tincapp/activities/LaunchActivity.kt')
-rw-r--r-- | app/src/main/java/org/pacien/tincapp/activities/LaunchActivity.kt | 118 |
1 files changed, 59 insertions, 59 deletions
diff --git a/app/src/main/java/org/pacien/tincapp/activities/LaunchActivity.kt b/app/src/main/java/org/pacien/tincapp/activities/LaunchActivity.kt index 74a059b..0179040 100644 --- a/app/src/main/java/org/pacien/tincapp/activities/LaunchActivity.kt +++ b/app/src/main/java/org/pacien/tincapp/activities/LaunchActivity.kt | |||
@@ -24,75 +24,75 @@ import java.io.FileNotFoundException | |||
24 | */ | 24 | */ |
25 | class LaunchActivity : AppCompatActivity() { | 25 | class LaunchActivity : AppCompatActivity() { |
26 | 26 | ||
27 | override fun onCreate(savedInstanceState: Bundle?) { | 27 | override fun onCreate(savedInstanceState: Bundle?) { |
28 | super.onCreate(savedInstanceState) | 28 | super.onCreate(savedInstanceState) |
29 | 29 | ||
30 | when (intent.action) { | 30 | when (intent.action) { |
31 | ACTION_CONNECT -> requestPerm() | 31 | ACTION_CONNECT -> requestPerm() |
32 | ACTION_DISCONNECT -> disconnect() | 32 | ACTION_DISCONNECT -> disconnect() |
33 | } | ||
34 | } | 33 | } |
35 | 34 | } | |
36 | override fun onActivityResult(request: Int, result: Int, data: Intent?) { | 35 | |
37 | if (request == PERMISSION_REQUEST_CODE && result == Activity.RESULT_OK) askPassphrase() | 36 | override fun onActivityResult(request: Int, result: Int, data: Intent?) { |
37 | if (request == PERMISSION_REQUEST_CODE && result == Activity.RESULT_OK) askPassphrase() | ||
38 | } | ||
39 | |||
40 | private fun requestPerm() = VpnService.prepare(this).let { | ||
41 | if (it != null) | ||
42 | startActivityForResult(it, PERMISSION_REQUEST_CODE) | ||
43 | else | ||
44 | onActivityResult(PERMISSION_REQUEST_CODE, Activity.RESULT_OK, null) | ||
45 | } | ||
46 | |||
47 | @SuppressLint("InflateParams") | ||
48 | private fun askPassphrase() { | ||
49 | val netName = intent.data.schemeSpecificPart | ||
50 | |||
51 | if (needPassphrase(netName) && intent.data.fragment == null) { | ||
52 | val dialog = layoutInflater.inflate(R.layout.dialog_decrypt_keys, null, false) | ||
53 | AlertDialog.Builder(this) | ||
54 | .setTitle(R.string.title_unlock_private_keys).setView(dialog) | ||
55 | .setPositiveButton(R.string.action_unlock) { _, _ -> connect(netName, dialog.passphrase.text.toString()) } | ||
56 | .setNegativeButton(R.string.action_cancel, { _, _ -> finish() }) | ||
57 | .show() | ||
58 | } else { | ||
59 | connect(netName, intent.data.fragment) | ||
38 | } | 60 | } |
61 | } | ||
39 | 62 | ||
40 | private fun requestPerm() = VpnService.prepare(this).let { | 63 | private fun needPassphrase(netName: String) = try { |
41 | if (it != null) | 64 | TincApp.listPrivateKeys(netName).filter { it.exists() }.any { PemUtils.isEncrypted(PemUtils.read(it)) } |
42 | startActivityForResult(it, PERMISSION_REQUEST_CODE) | 65 | } catch (e: FileNotFoundException) { |
43 | else | 66 | false |
44 | onActivityResult(PERMISSION_REQUEST_CODE, Activity.RESULT_OK, null) | 67 | } |
45 | } | ||
46 | 68 | ||
47 | @SuppressLint("InflateParams") | 69 | private fun connect(netName: String, passphrase: String? = null) { |
48 | private fun askPassphrase() { | 70 | TincVpnService.startVpn(netName, passphrase) |
49 | val netName = intent.data.schemeSpecificPart | 71 | finish() |
50 | 72 | } | |
51 | if (needPassphrase(netName) && intent.data.fragment == null) { | ||
52 | val dialog = layoutInflater.inflate(R.layout.dialog_decrypt_keys, null, false) | ||
53 | AlertDialog.Builder(this) | ||
54 | .setTitle(R.string.title_unlock_private_keys).setView(dialog) | ||
55 | .setPositiveButton(R.string.action_unlock) { _, _ -> connect(netName, dialog.passphrase.text.toString()) } | ||
56 | .setNegativeButton(R.string.action_cancel, { _, _ -> finish() }) | ||
57 | .show() | ||
58 | } else { | ||
59 | connect(netName, intent.data.fragment) | ||
60 | } | ||
61 | } | ||
62 | 73 | ||
63 | private fun needPassphrase(netName: String) = try { | 74 | private fun disconnect() { |
64 | TincApp.listPrivateKeys(netName).filter { it.exists() }.any { PemUtils.isEncrypted(PemUtils.read(it)) } | 75 | TincVpnService.stopVpn() |
65 | } catch (e: FileNotFoundException) { | 76 | finish() |
66 | false | 77 | } |
67 | } | ||
68 | 78 | ||
69 | private fun connect(netName: String, passphrase: String? = null) { | 79 | companion object { |
70 | TincVpnService.startVpn(netName, passphrase) | ||
71 | finish() | ||
72 | } | ||
73 | 80 | ||
74 | private fun disconnect() { | 81 | private val PERMISSION_REQUEST_CODE = 0 |
75 | TincVpnService.stopVpn() | ||
76 | finish() | ||
77 | } | ||
78 | 82 | ||
79 | companion object { | 83 | fun connect(netName: String, passphrase: String? = null) { |
80 | 84 | App.getContext().startActivity(Intent(App.getContext(), LaunchActivity::class.java) | |
81 | private val PERMISSION_REQUEST_CODE = 0 | 85 | .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) |
82 | 86 | .setAction(ACTION_CONNECT) | |
83 | fun connect(netName: String, passphrase: String? = null) { | 87 | .setData(Uri.Builder().scheme(TINC_SCHEME).opaquePart(netName).fragment(passphrase).build())) |
84 | App.getContext().startActivity(Intent(App.getContext(), LaunchActivity::class.java) | 88 | } |
85 | .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) | ||
86 | .setAction(ACTION_CONNECT) | ||
87 | .setData(Uri.Builder().scheme(TINC_SCHEME).opaquePart(netName).fragment(passphrase).build())) | ||
88 | } | ||
89 | |||
90 | fun disconnect() { | ||
91 | App.getContext().startActivity(Intent(App.getContext(), LaunchActivity::class.java) | ||
92 | .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) | ||
93 | .setAction(ACTION_DISCONNECT)) | ||
94 | } | ||
95 | 89 | ||
90 | fun disconnect() { | ||
91 | App.getContext().startActivity(Intent(App.getContext(), LaunchActivity::class.java) | ||
92 | .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) | ||
93 | .setAction(ACTION_DISCONNECT)) | ||
96 | } | 94 | } |
97 | 95 | ||
96 | } | ||
97 | |||
98 | } | 98 | } |