diff options
Diffstat (limited to 'app/src/main/java/org/pacien/tincapp/activities/start/StartActivity.kt')
-rw-r--r-- | app/src/main/java/org/pacien/tincapp/activities/start/StartActivity.kt | 117 |
1 files changed, 117 insertions, 0 deletions
diff --git a/app/src/main/java/org/pacien/tincapp/activities/start/StartActivity.kt b/app/src/main/java/org/pacien/tincapp/activities/start/StartActivity.kt new file mode 100644 index 0000000..9cc6159 --- /dev/null +++ b/app/src/main/java/org/pacien/tincapp/activities/start/StartActivity.kt | |||
@@ -0,0 +1,117 @@ | |||
1 | /* | ||
2 | * Tinc App, an Android binding and user interface for the tinc mesh VPN daemon | ||
3 | * Copyright (C) 2017-2018 Pacien TRAN-GIRARD | ||
4 | * | ||
5 | * This program is free software: you can redistribute it and/or modify | ||
6 | * it under the terms of the GNU General Public License as published by | ||
7 | * the Free Software Foundation, either version 3 of the License, or | ||
8 | * (at your option) any later version. | ||
9 | * | ||
10 | * This program is distributed in the hope that it will be useful, | ||
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
13 | * GNU General Public License for more details. | ||
14 | * | ||
15 | * You should have received a copy of the GNU General Public License | ||
16 | * along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
17 | */ | ||
18 | |||
19 | package org.pacien.tincapp.activities.start | ||
20 | |||
21 | import android.app.Activity | ||
22 | import android.content.Intent | ||
23 | import android.os.Bundle | ||
24 | import android.support.v7.app.AlertDialog | ||
25 | import android.view.Menu | ||
26 | import android.view.MenuItem | ||
27 | import kotlinx.android.synthetic.main.start_activity.* | ||
28 | import org.pacien.tincapp.R | ||
29 | import org.pacien.tincapp.activities.BaseActivity | ||
30 | import org.pacien.tincapp.activities.common.ProgressModal | ||
31 | import org.pacien.tincapp.activities.configure.ConfigureActivity | ||
32 | import org.pacien.tincapp.activities.status.StatusActivity | ||
33 | import org.pacien.tincapp.intent.Actions | ||
34 | import org.pacien.tincapp.intent.BroadcastMapper | ||
35 | import org.pacien.tincapp.service.TincVpnService | ||
36 | |||
37 | /** | ||
38 | * @author pacien | ||
39 | */ | ||
40 | class StartActivity : BaseActivity() { | ||
41 | val permissionRequestCode = 0 | ||
42 | private val connectionStarter by lazy { ConnectionStarter(this) } | ||
43 | private val broadcastMapper = BroadcastMapper(mapOf( | ||
44 | Actions.EVENT_CONNECTED to this::onVpnStart, | ||
45 | Actions.EVENT_ABORTED to this::onVpnStartError | ||
46 | )) | ||
47 | |||
48 | var connectDialog: AlertDialog? = null | ||
49 | |||
50 | override fun onCreate(savedInstanceState: Bundle?) { | ||
51 | super.onCreate(savedInstanceState) | ||
52 | setContentView(R.layout.start_activity) | ||
53 | initNetworkListFragment() | ||
54 | |||
55 | if (intent.action == Actions.ACTION_CONNECT && intent.data?.schemeSpecificPart != null) | ||
56 | connectionStarter.tryStart(intent.data.schemeSpecificPart, intent.data.fragment, false) | ||
57 | } | ||
58 | |||
59 | private fun initNetworkListFragment() { | ||
60 | val fragment = start_activity_network_list_fragment as NetworkListFragment | ||
61 | fragment.connectToNetworkAction = { netName -> connectionStarter.tryStart(netName, displayStatus = true) } | ||
62 | } | ||
63 | |||
64 | override fun onCreateOptionsMenu(m: Menu): Boolean { | ||
65 | menuInflater.inflate(R.menu.menu_start, m) | ||
66 | return super.onCreateOptionsMenu(m) | ||
67 | } | ||
68 | |||
69 | override fun onDestroy() { | ||
70 | connectDialog?.dismiss() | ||
71 | super.onDestroy() | ||
72 | } | ||
73 | |||
74 | override fun onResume() { | ||
75 | super.onResume() | ||
76 | if (TincVpnService.isConnected()) openStatusActivity(false) | ||
77 | broadcastMapper.register() | ||
78 | handleRecentCrash() | ||
79 | } | ||
80 | |||
81 | override fun onPause() { | ||
82 | broadcastMapper.unregister() | ||
83 | super.onPause() | ||
84 | } | ||
85 | |||
86 | override fun onActivityResult(request: Int, result: Int, data: Intent?): Unit = when (request) { | ||
87 | permissionRequestCode -> if (result == Activity.RESULT_OK) connectionStarter.tryStart() else Unit | ||
88 | else -> throw IllegalArgumentException("Result for unknown request received.") | ||
89 | } | ||
90 | |||
91 | private fun onVpnStart() { | ||
92 | connectDialog?.dismiss() | ||
93 | if (connectionStarter.displayStatus()) openStatusActivity() | ||
94 | finish() | ||
95 | } | ||
96 | |||
97 | private fun onVpnStartError() { | ||
98 | connectDialog?.dismiss() | ||
99 | } | ||
100 | |||
101 | fun showConnectProgressDialog() { | ||
102 | connectDialog = ProgressModal.show(this, resources.getString(R.string.message_starting_vpn)) | ||
103 | } | ||
104 | |||
105 | @Suppress("UNUSED_PARAMETER") | ||
106 | fun openConfigureActivity(m: MenuItem) = | ||
107 | startActivity(Intent(this, ConfigureActivity::class.java)) | ||
108 | |||
109 | private fun openStatusActivity(transition: Boolean = true) { | ||
110 | val intent = Intent(this, StatusActivity::class.java) | ||
111 | .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) | ||
112 | .addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK) | ||
113 | .apply { if (!transition) addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION) } | ||
114 | |||
115 | startActivity(intent) | ||
116 | } | ||
117 | } | ||