diff options
author | pacien | 2018-02-16 18:23:01 +0100 |
---|---|---|
committer | pacien | 2018-02-16 18:23:01 +0100 |
commit | c359d78bcd45cb506bac51a616ef62af0845df85 (patch) | |
tree | e526bbf710d301310fdee6a0da6399fb79c71f66 | |
parent | 680fe07b6ea000ee29ac28e2f48665433e7011df (diff) | |
download | tincapp-c359d78bcd45cb506bac51a616ef62af0845df85.tar.gz |
Refactor activities and service, locking app at daemon startup and shutdown
11 files changed, 323 insertions, 240 deletions
diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index f03a640..6826920 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml | |||
@@ -24,6 +24,11 @@ | |||
24 | <action android:name="android.intent.action.MAIN"/> | 24 | <action android:name="android.intent.action.MAIN"/> |
25 | <category android:name="android.intent.category.LAUNCHER"/> | 25 | <category android:name="android.intent.category.LAUNCHER"/> |
26 | </intent-filter> | 26 | </intent-filter> |
27 | <intent-filter> | ||
28 | <action android:name="org.pacien.tincapp.intent.action.CONNECT"/> | ||
29 | <data android:scheme="tinc"/> | ||
30 | <category android:name="android.intent.category.DEFAULT"/> | ||
31 | </intent-filter> | ||
27 | </activity> | 32 | </activity> |
28 | 33 | ||
29 | <activity | 34 | <activity |
@@ -37,16 +42,6 @@ | |||
37 | android:name=".activities.StatusActivity" | 42 | android:name=".activities.StatusActivity" |
38 | android:configChanges="orientation|screenSize" | 43 | android:configChanges="orientation|screenSize" |
39 | android:theme="@style/AppTheme.NoActionBar"> | 44 | android:theme="@style/AppTheme.NoActionBar"> |
40 | </activity> | ||
41 | |||
42 | <activity | ||
43 | android:name=".activities.LaunchActivity" | ||
44 | android:theme="@style/AppTheme.NoActionBar"> | ||
45 | <intent-filter> | ||
46 | <action android:name="org.pacien.tincapp.intent.action.CONNECT"/> | ||
47 | <data android:scheme="tinc"/> | ||
48 | <category android:name="android.intent.category.DEFAULT"/> | ||
49 | </intent-filter> | ||
50 | <intent-filter> | 45 | <intent-filter> |
51 | <action android:name="org.pacien.tincapp.intent.action.DISCONNECT"/> | 46 | <action android:name="org.pacien.tincapp.intent.action.DISCONNECT"/> |
52 | <category android:name="android.intent.category.DEFAULT"/> | 47 | <category android:name="android.intent.category.DEFAULT"/> |
diff --git a/app/src/main/java/org/pacien/tincapp/activities/LaunchActivity.kt b/app/src/main/java/org/pacien/tincapp/activities/LaunchActivity.kt deleted file mode 100644 index 0179040..0000000 --- a/app/src/main/java/org/pacien/tincapp/activities/LaunchActivity.kt +++ /dev/null | |||
@@ -1,98 +0,0 @@ | |||
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.Uri | ||
7 | import android.net.VpnService | ||
8 | import android.os.Bundle | ||
9 | import android.support.v7.app.AlertDialog | ||
10 | import android.support.v7.app.AppCompatActivity | ||
11 | import kotlinx.android.synthetic.main.dialog_decrypt_keys.view.* | ||
12 | import org.pacien.tincapp.R | ||
13 | import org.pacien.tincapp.commands.TincApp | ||
14 | import org.pacien.tincapp.context.App | ||
15 | import org.pacien.tincapp.intent.action.ACTION_CONNECT | ||
16 | import org.pacien.tincapp.intent.action.ACTION_DISCONNECT | ||
17 | import org.pacien.tincapp.intent.action.TINC_SCHEME | ||
18 | import org.pacien.tincapp.service.TincVpnService | ||
19 | import org.pacien.tincapp.utils.PemUtils | ||
20 | import java.io.FileNotFoundException | ||
21 | |||
22 | /** | ||
23 | * @author pacien | ||
24 | */ | ||
25 | class LaunchActivity : AppCompatActivity() { | ||
26 | |||
27 | override fun onCreate(savedInstanceState: Bundle?) { | ||
28 | super.onCreate(savedInstanceState) | ||
29 | |||
30 | when (intent.action) { | ||
31 | ACTION_CONNECT -> requestPerm() | ||
32 | ACTION_DISCONNECT -> disconnect() | ||
33 | } | ||
34 | } | ||
35 | |||
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) | ||
60 | } | ||
61 | } | ||
62 | |||
63 | private fun needPassphrase(netName: String) = try { | ||
64 | TincApp.listPrivateKeys(netName).filter { it.exists() }.any { PemUtils.isEncrypted(PemUtils.read(it)) } | ||
65 | } catch (e: FileNotFoundException) { | ||
66 | false | ||
67 | } | ||
68 | |||
69 | private fun connect(netName: String, passphrase: String? = null) { | ||
70 | TincVpnService.startVpn(netName, passphrase) | ||
71 | finish() | ||
72 | } | ||
73 | |||
74 | private fun disconnect() { | ||
75 | TincVpnService.stopVpn() | ||
76 | finish() | ||
77 | } | ||
78 | |||
79 | companion object { | ||
80 | |||
81 | private val PERMISSION_REQUEST_CODE = 0 | ||
82 | |||
83 | fun connect(netName: String, passphrase: String? = null) { | ||
84 | App.getContext().startActivity(Intent(App.getContext(), LaunchActivity::class.java) | ||
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 | |||
96 | } | ||
97 | |||
98 | } | ||
diff --git a/app/src/main/java/org/pacien/tincapp/activities/StartActivity.kt b/app/src/main/java/org/pacien/tincapp/activities/StartActivity.kt index 719bbc1..9fa5e44 100644 --- a/app/src/main/java/org/pacien/tincapp/activities/StartActivity.kt +++ b/app/src/main/java/org/pacien/tincapp/activities/StartActivity.kt | |||
@@ -1,8 +1,13 @@ | |||
1 | package org.pacien.tincapp.activities | 1 | package org.pacien.tincapp.activities |
2 | 2 | ||
3 | import android.app.Activity | ||
4 | import android.app.ProgressDialog | ||
3 | import android.content.Intent | 5 | import android.content.Intent |
6 | import android.content.IntentFilter | ||
7 | import android.net.VpnService | ||
4 | import android.os.Bundle | 8 | import android.os.Bundle |
5 | import android.support.v4.widget.SwipeRefreshLayout | 9 | import android.support.v4.widget.SwipeRefreshLayout |
10 | import android.support.v7.app.AlertDialog | ||
6 | import android.view.Menu | 11 | import android.view.Menu |
7 | import android.view.MenuItem | 12 | import android.view.MenuItem |
8 | import android.view.View | 13 | import android.view.View |
@@ -10,29 +15,117 @@ import android.widget.AdapterView | |||
10 | import android.widget.ArrayAdapter | 15 | import android.widget.ArrayAdapter |
11 | import android.widget.TextView | 16 | import android.widget.TextView |
12 | import kotlinx.android.synthetic.main.base.* | 17 | import kotlinx.android.synthetic.main.base.* |
18 | import kotlinx.android.synthetic.main.dialog_decrypt_keys.view.* | ||
13 | import kotlinx.android.synthetic.main.fragment_list_view.* | 19 | import kotlinx.android.synthetic.main.fragment_list_view.* |
14 | import kotlinx.android.synthetic.main.fragment_network_list_header.* | 20 | import kotlinx.android.synthetic.main.fragment_network_list_header.* |
15 | import org.pacien.tincapp.R | 21 | import org.pacien.tincapp.R |
16 | import org.pacien.tincapp.context.AppPaths | 22 | import org.pacien.tincapp.context.AppPaths |
17 | import org.pacien.tincapp.extensions.Android.setElements | 23 | import org.pacien.tincapp.extensions.Android.setElements |
24 | import org.pacien.tincapp.intent.Actions | ||
25 | import org.pacien.tincapp.intent.SimpleBroadcastReceiver | ||
18 | import org.pacien.tincapp.service.TincVpnService | 26 | import org.pacien.tincapp.service.TincVpnService |
27 | import org.pacien.tincapp.utils.TincKeyring | ||
19 | 28 | ||
20 | /** | 29 | /** |
21 | * @author pacien | 30 | * @author pacien |
22 | */ | 31 | */ |
23 | class StartActivity : BaseActivity(), AdapterView.OnItemClickListener, SwipeRefreshLayout.OnRefreshListener { | 32 | class StartActivity : BaseActivity() { |
33 | companion object { | ||
34 | private const val PERMISSION_REQUEST = 0 | ||
35 | } | ||
36 | |||
37 | private val networkList = object : AdapterView.OnItemClickListener, SwipeRefreshLayout.OnRefreshListener { | ||
38 | private var networkListAdapter: ArrayAdapter<String>? = null | ||
39 | |||
40 | fun init() { | ||
41 | networkListAdapter = ArrayAdapter(this@StartActivity, R.layout.fragment_list_item) | ||
42 | layoutInflater.inflate(R.layout.fragment_list_view, main_content) | ||
43 | list_wrapper.setOnRefresh |