Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion app/src/main/java/com/hwb/wifidebughelper/ConnectList.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import com.tencent.mmkv.MMKV
object ConnectList {
const val KEY_CONNECT_LIST = "connect_list"
const val KEY_CONNECT_SELECT = "connect_select"
const val KEY_CONNECT_SWITCH = "connect_switch"


enum class ErrorCode {
Expand All @@ -13,14 +14,24 @@ object ConnectList {
KEY_NOT_EXIST
}

fun setConnectSwitch(switch: Boolean) {
val mmkv = MMKV.mmkvWithID(KEY_CONNECT_SWITCH)
mmkv.encode("connect_switch", switch)
}

fun getConnectSwitch(): Boolean {
val mmkv = MMKV.mmkvWithID(KEY_CONNECT_SWITCH)
return mmkv.decodeBool("connect_switch", false)
}

fun getSelectId(): ConnectData? {
val mmkv = MMKV.mmkvWithID(KEY_CONNECT_SELECT)
val selectedId = mmkv.decodeString("selectedId", "")
if (selectedId.isNullOrEmpty()) {
return null
}
val mmkv2 = MMKV.mmkvWithID(KEY_CONNECT_LIST)
return mmkv2.decodeParcelable(selectedId, ConnectData::class.java)
return mmkv2.decodeParcelable(selectedId, ConnectData::class.java)
}

fun setSelectId(id: String?) {
Expand Down
6 changes: 1 addition & 5 deletions app/src/main/java/com/hwb/wifidebughelper/ConnectListVM.kt
Original file line number Diff line number Diff line change
@@ -1,15 +1,10 @@
package com.hwb.wifidebughelper

import android.util.Log
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateListOf
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.lifecycle.ViewModel
import com.hjq.toast.Toaster
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.asStateFlow

class ConnectListVM : ViewModel() {

Expand All @@ -22,6 +17,7 @@ class ConnectListVM : ViewModel() {
init {
_items.addAll(ConnectList.getList())
_item.value = ConnectList.getSelectId()
isConnect.value = ConnectList.getConnectSwitch()
Log.d("ViewModel", "Item updated: ${_item.value}")
}
//val items = _items.asStateFlow()
Expand Down
3 changes: 1 addition & 2 deletions app/src/main/java/com/hwb/wifidebughelper/HelpDialog.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ import androidx.compose.ui.window.Dialog
fun HelpDialog(onDismiss: () -> Unit) {
Dialog(onDismissRequest = onDismiss) {
Surface(shape = MaterialTheme.shapes.medium, color = MaterialTheme.colorScheme.background) {
Column(modifier = Modifier.padding(16.dp), horizontalAlignment = Alignment.CenterHorizontally) {

Column(modifier = Modifier.padding(20.dp), horizontalAlignment = Alignment.CenterHorizontally) {

Text(text = "使用说明", fontSize = 20.sp, fontWeight = FontWeight.Bold)
Spacer(modifier = Modifier.height(12.dp))
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/java/com/hwb/wifidebughelper/MainActivity2.kt
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ import androidx.navigation.compose.composable
import androidx.navigation.compose.rememberNavController
import androidx.navigation.navigation
import com.example.wifidebughelper.R
import com.hwb.wifidebughelper.ConnectList.setConnectSwitch
import com.hwb.wifidebughelper.ui.theme.WifiDebugHelperTheme

@Composable
Expand Down Expand Up @@ -119,6 +120,7 @@ fun Main2Activity(navController: NavHostController) {
if (viewModel.isConnect.value) {
ServiceUtil.startService(viewModel._item.value?.serverIp, viewModel._item.value?.tcpIp)
}
setConnectSwitch(viewModel.isConnect.value)
// viewModel._item.value = connectData
// ConnectList.setSelectId(connectData?.id)
// ServiceUtil.startService(connectData?.serverIp, connectData?.tcpIp)
Expand Down
25 changes: 12 additions & 13 deletions app/src/main/java/com/hwb/wifidebughelper/MainListActivity.kt
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package com.hwb.wifidebughelper

import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.activity.enableEdgeToEdge
import androidx.compose.animation.animateColorAsState
import androidx.compose.foundation.ExperimentalFoundationApi
import androidx.compose.foundation.background
Expand All @@ -29,7 +26,6 @@ import androidx.compose.material3.Button
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.Icon
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Surface
import androidx.compose.material3.SwipeToDismissBox
import androidx.compose.material3.SwipeToDismissBoxValue
Expand All @@ -55,7 +51,6 @@ import androidx.constraintlayout.compose.Dimension
import androidx.lifecycle.viewmodel.compose.viewModel
import androidx.navigation.NavHostController
import com.hjq.toast.Toaster
import com.hwb.wifidebughelper.ui.theme.WifiDebugHelperTheme

class MainListActivity : ComponentActivity() {
// override fun onCreate(savedInstanceState: Bundle?) {
Expand Down Expand Up @@ -91,13 +86,15 @@ fun MainListActivity(navController: NavHostController) {
"WiFiDebugHelper",
fontSize = 20.sp,
fontWeight = FontWeight.Bold,
modifier = Modifier.constrainAs(title) {
top.linkTo(parent.top, margin = 50.dp)
start.linkTo(parent.start)
end.linkTo(parent.end)
}.clickable{
navController.popBackStack()
}
modifier = Modifier
.constrainAs(title) {
top.linkTo(parent.top, margin = 50.dp)
start.linkTo(parent.start)
end.linkTo(parent.end)
}
.clickable {
navController.popBackStack()
}
)

Button(
Expand Down Expand Up @@ -232,7 +229,9 @@ fun ConnectItem(connectData: ConnectData?) {
.clickable(onClick = {
viewModel._item.value = connectData
ConnectList.setSelectId(connectData?.id)
viewModel.isConnect.value = true
if (viewModel.isConnect.value) {
ServiceUtil.startService(connectData?.serverIp, connectData?.tcpIp)
}
})
.padding(start = 16.dp, end = 16.dp, top = 8.dp, bottom = 8.dp)
.background(
Expand Down
27 changes: 15 additions & 12 deletions app/src/main/java/com/hwb/wifidebughelper/MyService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import com.hwb.wifidebughelper.MainActivity.Companion.SP_KEY_ADB_TCOIP_PORT
import com.hwb.wifidebughelper.MainActivity.Companion.SP_KEY_SERVER_ADDRESS
import java.net.Inet4Address
import androidx.core.content.edit
import com.hwb.wifidebughelper.ConnectList.getConnectSwitch

class MyService : Service() {

Expand All @@ -46,12 +47,14 @@ class MyService : Service() {
object : ConnectivityManager.NetworkCallback() {
override fun onLinkPropertiesChanged(network: Network, linkProperties: LinkProperties) {
super.onLinkPropertiesChanged(network, linkProperties)
val find = linkProperties.linkAddresses.find { it.address is Inet4Address }
find?.address?.hostAddress?.let {
//ip 发生变化,重新上报
if (it != lastIPTemp) {
reportIp(it)
lastIPTemp = it
if (getConnectSwitch()) {
val find = linkProperties.linkAddresses.find { it.address is Inet4Address }
find?.address?.hostAddress?.let {
//ip 发生变化,重新上报
if (it != lastIPTemp) {
reportIp(it)
lastIPTemp = it
}
}
}
}
Expand All @@ -66,7 +69,7 @@ class MyService : Service() {
val port = getSharedPreferences().getString(SP_KEY_ADB_TCOIP_PORT, DEF_TPICP_PORT)
val url = "http://$serverAddress/string?address=$ip:$port"
Log.d("NetworkRequest", "尝试连接URL: $url")

val stringRequest = object : StringRequest(
Method.GET,
url,
Expand All @@ -83,9 +86,9 @@ class MyService : Service() {
val errorMsg = if (error.message != null) error.message else "连接失败"
val errorClass = error.javaClass.simpleName
val errorDetails = "错误类型: $errorClass, 错误信息: $errorMsg"

Log.e("NetworkRequest", "请求失败: $errorDetails", error)

// 处理服务器错误,尝试获取服务器返回的错误信息
if (errorClass.contains("ServerError") && error.networkResponse != null) {
try {
Expand Down Expand Up @@ -129,7 +132,7 @@ class MyService : Service() {
return super.parseNetworkResponse(response)
}
}

queue.add(stringRequest)
}
}
Expand Down Expand Up @@ -163,12 +166,12 @@ class MyService : Service() {
.setContentText("ip 监听中")
.setContentIntent(pendingIntent)
.build()

// 使用ServiceCompat替代直接调用startForeground
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
ServiceCompat.startForeground(
this,
1,
1,
notification,
ServiceInfo.FOREGROUND_SERVICE_TYPE_CONNECTED_DEVICE
)
Expand Down