165 lines
5.3 KiB
Kotlin

package com.jihe.punchnet.screen
import android.app.Activity.RESULT_CANCELED
import android.app.Activity.RESULT_OK
import android.content.Context
import android.content.Intent
import android.os.Build
import android.widget.Toast
import androidx.activity.compose.rememberLauncherForActivityResult
import androidx.activity.result.contract.ActivityResultContracts
import androidx.compose.foundation.Image
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxHeight
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Settings
import androidx.compose.material3.Button
import androidx.compose.material3.Icon
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.MutableState
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.jihe.punchnet.PunchnetService
import com.jihe.punchnet.PunchnetServiceArgument
import com.jihe.punchnet.R
import com.jihe.punchnet.RouteInfo
import com.jihe.punchnet.data.RouteViewModel
import com.jihe.punchnet.data.ServiceViewModel
@Composable
fun HomeScreen(
serviceViewModel: ServiceViewModel,
routeViewModel: RouteViewModel,
// started: MutableState<Boolean>,
modifier: Modifier = Modifier,
) {
val context = LocalContext.current
val vpnLauncher = rememberLauncherForActivityResult(
contract = ActivityResultContracts.StartActivityForResult()
) {result ->
when(result.resultCode) {
RESULT_OK -> {
serviceViewModel.setVPNPermission(true)
}
RESULT_CANCELED -> {
serviceViewModel.setVPNPermission(false)
Toast.makeText(context, "vpn permission denied", Toast.LENGTH_SHORT).show()
}
}
}
CustomHeaderScreen(
"",
onBack = null,
onMenu = {
Icon(
Icons.Default.Settings,
contentDescription = "settings",
modifier = Modifier.fillMaxHeight()
)
// NavListMenu(navController, dbdao)
/*
navController.navigate(
Screen.ActionDetail.route.replace(
"{action_name}",
""
)
)
*/
}
) {
Column(
modifier = Modifier.fillMaxWidth()
.padding(top=50.dp),
horizontalAlignment = Alignment.CenterHorizontally,
) {
Image(
painter = painterResource(R.drawable.punchnet_log),
contentDescription = "logo",
modifier = Modifier.size(150.dp)
)
Text(
"Connecting the Infinite",
fontSize = 30.sp,
style = MaterialTheme.typography.titleLarge,
fontWeight = FontWeight.Bold,
modifier = Modifier.padding(top=24.dp)
)
Text(
"Welcome to PunchNet",
style = MaterialTheme.typography.titleSmall,
modifier = Modifier.padding(top=8.dp)
)
Button(
onClick = {
if (serviceViewModel.isRunning.value) {
// if is running, should stop service
serviceViewModel.stopVpnService(context)
} else {
serviceViewModel.startService(
context,
{intent->
vpnLauncher.launch(intent)
},
)
}
// started.value = !started.value
},
shape = RoundedCornerShape(10.dp),
modifier = Modifier.padding(top=48.dp)
.width(120.dp)
.height(40.dp)
) {
Text(
if (serviceViewModel.isRunning.value) {
"停止"
} else {
"启动"
}
)
}
}
// BasicCardList(dbdao = dbdao, navController)
}
}
private fun startVpnService(context: Context) {
val intent = Intent(context, PunchnetService::class.java)
intent.putExtra("argument", PunchnetServiceArgument(
"",
arrayOf(
RouteInfo(
"192.168.80.0/24", "10.211.188.2"
)
)
)
)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
context.startForegroundService(intent)
} else {
context.startService(intent)
}
Toast.makeText(context, "VPN service started", Toast.LENGTH_SHORT).show()
}