diff options
Diffstat (limited to 'app/src/main/java/org/pacien/tincapp/activities/StartActivity.kt')
-rw-r--r-- | app/src/main/java/org/pacien/tincapp/activities/StartActivity.kt | 82 |
1 files changed, 82 insertions, 0 deletions
diff --git a/app/src/main/java/org/pacien/tincapp/activities/StartActivity.kt b/app/src/main/java/org/pacien/tincapp/activities/StartActivity.kt new file mode 100644 index 0000000..6d39a9d --- /dev/null +++ b/app/src/main/java/org/pacien/tincapp/activities/StartActivity.kt | |||
@@ -0,0 +1,82 @@ | |||
1 | package org.pacien.tincapp.activities | ||
2 | |||
3 | import android.annotation.SuppressLint | ||
4 | import android.app.Activity | ||
5 | import android.content.Intent | ||
6 | import android.net.VpnService | ||
7 | import android.os.Bundle | ||
8 | import android.support.v7.app.AlertDialog | ||
9 | import android.view.View | ||
10 | import android.view.ViewGroup | ||
11 | import android.widget.EditText | ||
12 | import android.widget.FrameLayout | ||
13 | import kotlinx.android.synthetic.main.base.* | ||
14 | |||
15 | import org.pacien.tincapp.R | ||
16 | import org.pacien.tincapp.commands.PermissionFixer | ||
17 | import org.pacien.tincapp.context.AppPaths | ||
18 | import org.pacien.tincapp.service.TincVpnService | ||
19 | |||
20 | /** | ||
21 | * @author pacien | ||
22 | */ | ||
23 | class StartActivity : BaseActivity() { | ||
24 | |||
25 | override fun onCreate(savedInstanceState: Bundle?) { | ||
26 | super.onCreate(savedInstanceState) | ||
27 | layoutInflater.inflate(R.layout.page_start, main_content) | ||
28 | } | ||
29 | |||
30 | override fun onActivityResult(request: Int, result: Int, data: Intent?) { | ||
31 | notify(if (result == Activity.RESULT_OK) R.string.message_vpn_permissions_granted else R.string.message_vpn_permissions_denied) | ||
32 | } | ||
33 | |||
34 | fun requestVpnPermission(@Suppress("UNUSED_PARAMETER") v: View) { | ||
35 | val askPermIntent = VpnService.prepare(this) | ||
36 | |||
37 | if (askPermIntent != null) | ||
38 | startActivityForResult(askPermIntent, 0) | ||
39 | else | ||
40 | onActivityResult(0, Activity.RESULT_OK, null) | ||
41 | } | ||
42 | |||
43 | fun startVpnDialog(@Suppress("UNUSED_PARAMETER") v: View) { | ||
44 | val i = EditText(this) | ||
45 | i.layoutParams = FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT) | ||
46 | i.setHint(R.string.field_net_name) | ||
47 | |||
48 | @SuppressLint("InflateParams") | ||
49 | val vg = layoutInflater.inflate(R.layout.dialog_frame, null) as ViewGroup | ||
50 | vg.addView(i) | ||
51 | |||
52 | AlertDialog.Builder(this) | ||
53 | .setTitle(R.string.title_connect_to_network) | ||
54 | .setView(vg) | ||
55 | .setPositiveButton(R.string.action_connect) { _, _ -> startVpn(i.text.toString()) } | ||
56 | .setNegativeButton(R.string.action_close) { _, _ -> /* nop */ } | ||
57 | .show() | ||
58 | } | ||
59 | |||
60 | fun confDirDialog(@Suppress("UNUSED_PARAMETER") v: View) { | ||
61 | val confDir = AppPaths.confDir(this).path | ||
62 | |||
63 | AlertDialog.Builder(this) | ||
64 | .setTitle(R.string.title_tinc_config_dir) | ||
65 | .setMessage(confDir) | ||
66 | .setNeutralButton(R.string.action_fix_perms) { _, _ -> fixPerms() } | ||
67 | .setNegativeButton(R.string.action_copy) { _, _ -> copyIntoClipboard(resources.getString(R.string.title_tinc_config_dir), confDir) } | ||
68 | .setPositiveButton(R.string.action_close) { _, _ -> /* nop */ } | ||
69 | .show() | ||
70 | } | ||
71 | |||
72 | private fun startVpn(netName: String) { | ||
73 | startService(Intent(this, TincVpnService::class.java) | ||
74 | .putExtra(TincVpnService.INTENT_EXTRA_NET_NAME, netName)) | ||
75 | } | ||
76 | |||
77 | private fun fixPerms() { | ||
78 | val ok = PermissionFixer.makePrivateDirsPublic(applicationContext) | ||
79 | notify(if (ok) R.string.message_perms_fixed else R.string.message_perms_fix_failure) | ||
80 | } | ||
81 | |||
82 | } | ||