Modern-Apps.git
Publicgit clone https://git.maproj.org/Modern-Apps.gitModern-Apps.git / vpn / src / main / java / com / vayunmathur / vpn / MainActivity.kt
MainActivity.kt69 lines · 2.3 KiBRaw
| 1 | package com.vayunmathur.vpn |
| 2 | |
| 3 | import android.content.Intent |
| 4 | import androidx.activity.viewModels |
| 5 | import androidx.compose.runtime.Composable |
| 6 | import androidx.compose.runtime.getValue |
| 7 | import androidx.compose.runtime.mutableStateOf |
| 8 | import androidx.compose.runtime.setValue |
| 9 | import androidx.lifecycle.lifecycleScope |
| 10 | import com.vayunmathur.library.network.NetworkClient |
| 11 | import com.vayunmathur.library.network.TrustBundle |
| 12 | import com.vayunmathur.library.ui.MaActivity |
| 13 | import com.vayunmathur.library.util.OfflineAware |
| 14 | import com.vayunmathur.vpn.data.ConnectionLogDao |
| 15 | import com.vayunmathur.vpn.data.VpnConfigDao |
| 16 | import com.vayunmathur.vpn.data.VpnDatabase |
| 17 | import com.vayunmathur.vpn.platform.VpnViewModel |
| 18 | import com.vayunmathur.vpn.platform.VpnViewModelFactory |
| 19 | import com.vayunmathur.vpn.util.VpnNative |
| 20 | import kotlinx.coroutines.Dispatchers |
| 21 | import kotlinx.coroutines.launch |
| 22 | import kotlinx.coroutines.withContext |
| 23 | |
| 24 | /** Main Activity: a ma activity implementation for main activity. */ |
| 25 | class MainActivity : MaActivity() { |
| 26 | private lateinit var configDao: VpnConfigDao |
| 27 | private lateinit var logDao: ConnectionLogDao |
| 28 | private val vm: VpnViewModel by viewModels { |
| 29 | VpnViewModelFactory(application, configDao, logDao) |
| 30 | } |
| 31 | private var ready by mutableStateOf(false) |
| 32 | |
| 33 | override fun onCreateBeforeContent() { |
| 34 | // SYSTEM: user-supplied VPN endpoint dynamic host, cannot pin |
| 35 | NetworkClient.init(this, TrustBundle.SYSTEM) |
| 36 | |
| 37 | lifecycleScope.launch(Dispatchers.IO) { |
| 38 | runCatching { VpnNative.init() } |
| 39 | val db = VpnDatabase.get(this@MainActivity) |
| 40 | configDao = db.vpnConfigDao() |
| 41 | logDao = db.connectionLogDao() |
| 42 | withContext(Dispatchers.Main) { |
| 43 | ready = true |
| 44 | handleIntent(intent) |
| 45 | } |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | @Composable |
| 50 | override fun Content() { |
| 51 | OfflineAware { |
| 52 | if (ready) Navigation(vm) |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | override fun onNewIntent(intent: Intent) { |
| 57 | super.onNewIntent(intent) |
| 58 | handleIntent(intent) |
| 59 | } |
| 60 | |
| 61 | private fun handleIntent(intent: Intent?) { |
| 62 | intent ?: return |
| 63 | // Accept VIEW intents for .conf / .mavpn files from Files / Downloads etc. |
| 64 | if (intent.action == Intent.ACTION_VIEW) { |
| 65 | val uri = intent.data ?: return |
| 66 | vm.importFromUri(this, uri) |
| 67 | } |
| 68 | } |
| 69 | } |