Skip to content

Commit

Permalink
try sync status
Browse files Browse the repository at this point in the history
  • Loading branch information
rikaaa0928 committed Dec 12, 2024
1 parent 3149db3 commit 0e4efd3
Show file tree
Hide file tree
Showing 5 changed files with 109 additions and 5 deletions.
5 changes: 5 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@
<!-- <uses-permission android:name="android.permission.CAPTURE_VOICE_COMMUNICATION_OUTPUT" />-->
<uses-permission android:name="android.permission.RECORD_AUDIO" />

<permission android:name="moe.rikaaa0928.uot.SHOW_MESSAGE" />
<permission android:name="moe.rikaaa0928.uot.permission.SHOW_MESSAGE"
android:protectionLevel="signature"/>
<uses-permission android:name="moe.rikaaa0928.uot.permission.SHOW_MESSAGE" />

<application
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
Expand Down
64 changes: 64 additions & 0 deletions app/src/main/java/moe/rikaaa0928/uot/MainActivity.kt
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
package moe.rikaaa0928.uot

import android.app.AlertDialog
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.graphics.Color
import android.os.Build
import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.widget.Button
import android.widget.EditText
Expand All @@ -20,6 +25,7 @@ class MainActivity : AppCompatActivity() {
init {
System.loadLibrary("uog")
}
const val MESSAGE_ACTION = "moe.rikaaa0928.uot.action.SHOW_MESSAGE"
}

private lateinit var configRecyclerView: RecyclerView
Expand All @@ -28,7 +34,25 @@ class MainActivity : AppCompatActivity() {
private lateinit var configAdapter: ConfigAdapter
private var configList: MutableList<Config> = mutableListOf()
private var activeConfigPosition: Int = -1
private val messageReceiver = object : BroadcastReceiver() {
override fun onReceive(context: Context?, intent: Intent?) {
Log.d("MainActivity", "Received broadcast: ${intent?.action}")
intent?.getStringExtra("message")?.let { message ->
Log.d("MainActivity", "Message content: $message")
showMessage(message)
}
}
}

// 在 MainActivity 类中添加自定义队列类
private class LimitedQueue<T> : ArrayList<T>() {
override fun add(element: T): Boolean {
if (size >= 3) {
removeFirstOrNull()
}
return super.add(element)
}
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
Expand Down Expand Up @@ -58,6 +82,26 @@ class MainActivity : AppCompatActivity() {

val initer = Init()
initer.callInit(baseContext)

// 根据 Android 版本使用不同的注册方式
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
registerReceiver(
messageReceiver,
IntentFilter(MESSAGE_ACTION),
Context.RECEIVER_NOT_EXPORTED
)
} else {
registerReceiver(
messageReceiver,
IntentFilter(MESSAGE_ACTION)
)
}
}

override fun onDestroy() {
super.onDestroy()
// 取消注册广播接收器
unregisterReceiver(messageReceiver)
}

private fun setupRecyclerView() {
Expand Down Expand Up @@ -88,6 +132,13 @@ class MainActivity : AppCompatActivity() {
activeConfigPosition = sharedPreferences.getInt("activeConfig", -1)
configAdapter.setActivePosition(activeConfigPosition) // 设置活跃配置
switchStart.isChecked = sharedPreferences.getBoolean("isStarted", false)
showMessage(
String.format(
"switch %s-%s",
switchStart.isChecked,
sharedPreferences.getBoolean("isStarted", false)
)
)
if (switchStart.isChecked) {
startGrpcService()
}
Expand Down Expand Up @@ -215,6 +266,19 @@ class MainActivity : AppCompatActivity() {
.setPositiveButton("确定", null)
.show()
}

private fun showMessage(message: String) {
com.google.android.material.snackbar.Snackbar.make(
findViewById(android.R.id.content),
message,
com.google.android.material.snackbar.Snackbar.LENGTH_LONG
).apply {
// 设置半透明背景
view.setBackgroundColor(Color.parseColor("#CC323232"))
// 添加点击监听,点击任意位置消失
view.setOnClickListener { dismiss() }
}.show()
}
}

data class Config(
Expand Down
18 changes: 17 additions & 1 deletion app/src/main/java/moe/rikaaa0928/uot/UogClient.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package moe.rikaaa0928.uot

import android.content.Context
import android.content.Intent
import android.net.ConnectivityManager
import android.net.Network
import android.net.NetworkCapabilities
Expand All @@ -22,7 +24,8 @@ class UogClient(
val lPort: Int,
val endpoint: String,
val password: String,
val connectivityManager: ConnectivityManager
val connectivityManager: ConnectivityManager,
private val context: Context
) :
ConnectivityManager.NetworkCallback() {
// private var service: UdpServiceGrpcKt.UdpServiceCoroutineStub? = null
Expand All @@ -38,9 +41,11 @@ class UogClient(
c.compareAndSet(null, UogRust())
val res = c.get()?.client("127.0.0.1:$lPort", endpoint, password)
Log.e("UogClient", "startClient exit $res")
sendMessage("startClient exit $res")
c.getAndSet(null)?.stop()
} catch (e: Throwable) {
Log.e("UogClient", "all", e)
sendMessage("Client error: ${e.message}")
} finally {
val l = waitNet.get()
if (l != null) {
Expand All @@ -51,9 +56,20 @@ class UogClient(
}
}
Log.d("UotClient", "exit main loop")
sendMessage("UotClient exit main loop")
}
}

private fun sendMessage(message: String) {
val intent = Intent(MainActivity.MESSAGE_ACTION).apply {
setPackage(context.packageName) // 确保广播只发送给本应用
putExtra("message", message)
}
context.sendBroadcast(intent)
// 添加日志以便调试
Log.d("UogClient", "Sending broadcast message: $message")
}

fun stop() {
stop.set(true)
c.getAndSet(null)?.stop()
Expand Down
25 changes: 22 additions & 3 deletions app/src/main/java/moe/rikaaa0928/uot/UogGrpc.kt
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ class UogGrpc : Service() {
val configJson = intent?.getStringExtra("config")
if (configJson != null) {
val config = Gson().fromJson(configJson, Config::class.java)
if (client != null) {
connectivityManager?.unregisterNetworkCallback(client!!)
client?.stop()
sendMessage("Client stopped")
}
initializeClient(config)
client?.start()
} else {
Expand All @@ -46,7 +51,8 @@ class UogGrpc : Service() {
config.listenPort.toInt(),
config.grpcEndpoint,
config.password,
connectivityManager!!
connectivityManager!!,
applicationContext
)

val networkRequest = NetworkRequest.Builder()
Expand All @@ -58,8 +64,11 @@ class UogGrpc : Service() {

override fun onDestroy() {
super.onDestroy()
client?.stop()
connectivityManager?.unregisterNetworkCallback(client!!)
if (client != null) {
client?.stop()
// sendMessage("Service destroyed, client stopped")
connectivityManager?.unregisterNetworkCallback(client!!)
}
}

override fun onBind(intent: Intent?): IBinder? {
Expand Down Expand Up @@ -110,4 +119,14 @@ class UogGrpc : Service() {
// ...
}
}

private fun sendMessage(message: String) {
val intent = Intent(MainActivity.MESSAGE_ACTION).apply {
setPackage(packageName) // 确保广播只发送给本应用
putExtra("message", message)
}
sendBroadcast(intent)
// 添加日志以便调试
Log.d("UogGrpc", "Sending broadcast message: $message")
}
}
2 changes: 1 addition & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[versions]
agp = "8.7.1"
agp = "8.7.3"
kotlin = "1.9.24"
coreKtx = "1.13.1"
junit = "4.13.2"
Expand Down

0 comments on commit 0e4efd3

Please sign in to comment.