11package com.follow.clash
22
3+ import com.follow.clash.common.GlobalState
34import com.follow.clash.common.ServiceDelegate
45import com.follow.clash.common.formatString
56import com.follow.clash.common.intent
@@ -8,6 +9,7 @@ import com.follow.clash.service.ICallbackInterface
89import com.follow.clash.service.IEventInterface
910import com.follow.clash.service.IRemoteInterface
1011import com.follow.clash.service.IResultInterface
12+ import com.follow.clash.service.IVoidInterface
1113import com.follow.clash.service.RemoteService
1214import com.follow.clash.service.models.NotificationParams
1315import com.follow.clash.service.models.VpnOptions
@@ -40,7 +42,7 @@ object Service {
4042 delegate.unbind()
4143 }
4244
43- suspend fun invokeAction (data : String , cb : (result: String ) -> Unit ): Result <Unit > {
45+ suspend fun invokeAction (data : String , cb : (( result: String ) -> Unit ) ? ): Result <Unit > {
4446 val res = mutableListOf<ByteArray >()
4547 return delegate.useService {
4648 it.invokeAction(
@@ -51,38 +53,75 @@ object Service {
5153 res.add(result ? : byteArrayOf())
5254 ack?.onAck()
5355 if (isSuccess) {
54- cb(res.formatString())
56+ cb?.let { cb ->
57+ cb(res.formatString())
58+ }
5559 }
5660 }
5761 })
5862 }
5963 }
6064
65+ suspend fun quickSetup (
66+ initParamsString : String ,
67+ setupParamsString : String ,
68+ onStarted : (() -> Unit )? ,
69+ onResult : ((result: String ) -> Unit )? ,
70+ ): Result <Unit > {
71+ val res = mutableListOf<ByteArray >()
72+ return delegate.useService {
73+ it.quickSetup(
74+ initParamsString,
75+ setupParamsString,
76+ object : ICallbackInterface .Stub () {
77+ override fun onResult (
78+ result : ByteArray? , isSuccess : Boolean , ack : IAckInterface ?
79+ ) {
80+ res.add(result ? : byteArrayOf())
81+ ack?.onAck()
82+ if (isSuccess) {
83+ onResult?.let { cb ->
84+ cb(res.formatString())
85+ }
86+ }
87+ }
88+ },
89+ object : IVoidInterface .Stub () {
90+ override fun invoke () {
91+ onStarted?.let { onStarted ->
92+ onStarted()
93+ }
94+ }
95+ }
96+ )
97+ }
98+ }
99+
61100 suspend fun setEventListener (
62101 cb : ((result: String? ) -> Unit )?
63102 ): Result <Unit > {
64103 val results = HashMap <String , MutableList <ByteArray >>()
65104 return delegate.useService {
66105 it.setEventListener(
67106 when (cb != null ) {
68- true -> object : IEventInterface .Stub () {
69- override fun onEvent (
70- id : String , data : ByteArray? , isSuccess : Boolean , ack : IAckInterface ?
71- ) {
72- if (results[id] == null ) {
73- results[id] = mutableListOf ()
74- }
75- results[id]?.add(data ? : byteArrayOf())
76- ack?.onAck()
77- if (isSuccess) {
78- cb(results[id]?.formatString())
79- results.remove(id)
107+ true -> object : IEventInterface .Stub () {
108+ override fun onEvent (
109+ id : String , data : ByteArray? , isSuccess : Boolean , ack : IAckInterface ?
110+ ) {
111+ if (results[id] == null ) {
112+ results[id] = mutableListOf ()
113+ }
114+ results[id]?.add(data ? : byteArrayOf())
115+ ack?.onAck()
116+ if (isSuccess) {
117+ cb(results[id]?.formatString())
118+ results.remove(id)
119+ }
80120 }
81121 }
82- }
83122
84- false -> null
85- })
123+ false -> null
124+ })
86125 }
87126 }
88127
@@ -116,6 +155,7 @@ object Service {
116155 try {
117156 block(callback)
118157 } catch (e: Exception ) {
158+ GlobalState .log(" awaitIResultInterface $e " )
119159 if (continuation.isActive) {
120160 continuation.resumeWithException(e)
121161 }
0 commit comments