[WIP] refactor service

This commit is contained in:
Kr328 committed 2020-01-17 00:17:40 +08:00
1 parent fa6ac580d0
commit e979bec321
7 files changed
+126 -31

No files matched your search

@@ -122,7 +122,7 @@ class Clash(
return Bridge.setSelectedProxy(name, selected)
}
fun startUrlTest(name: String): CompletableFuture<Unit> {
fun startHealthCheck(name: String): CompletableFuture<Unit> {
return DoneCallbackImpl().apply {
Bridge.startUrlTest(name, this)
}
@@ -1,9 +1,5 @@
package com.github.kr328.clash.core.event;
parcelable ProcessEvent;
parcelable LogEvent;
parcelable TrafficEvent;
parcelable BandwidthEvent;
parcelable ErrorEvent;
parcelable ProfileChangedEvent;
parcelable ProfileReloadEvent;
parcelable BandwidthEvent;
@@ -1,4 +1,4 @@
package com.github.kr328.clash.core.model;
parcelable CompressedProxyList;
parcelable ProxyGroup;
parcelable General;
@@ -1,20 +1,26 @@
package com.github.kr328.clash.service;
import com.github.kr328.clash.service.IClashEventObserver;
import com.github.kr328.clash.service.IClashEventService;
import com.github.kr328.clash.service.IClashProfileService;
import com.github.kr328.clash.service.IClashSettingService;
import com.github.kr328.clash.service.ipc.IPCParcelables;
import com.github.kr328.clash.callback.IUrlTestCallback;
import com.github.kr328.clash.core.event.Event;
import com.github.kr328.clash.core.model.Packet;
interface IClashManager {
// Control
ParcelableCompletedFuture setSelectProxy(String proxy, String selected);
boolean setSelectProxy(String proxy, String selected);
ParcelableCompletedFuture startHealthCheck(String group);
// Query
CompressedProxyList queryAllProxies();
ProxyGroup[] queryAllProxies();
General queryGeneral();
ParcelableCompletedFuture startUrlTest(String group);
// Profiles
ParcelableCompletedFuture addProfile(String url);
ParcelableCompletedFuture updateProfile(int id);
ParcelableCompletedFuture queryAllProfiles();
// Events
ParcelablePipe openBandwidthEvent();
ParcelablePipe openLogEvent();
// Settings
}
@@ -0,0 +1,66 @@
package com.github.kr328.clash.service
import com.github.kr328.clash.core.Clash
import com.github.kr328.clash.core.model.General
import com.github.kr328.clash.core.model.ProxyGroup
import com.github.kr328.clash.service.ipc.ParcelableCompletedFuture
import com.github.kr328.clash.service.ipc.ParcelablePipe
class ClashManager(val clash: Clash): IClashManager.Stub() {
override fun updateProfile(id: Int): ParcelableCompletedFuture {
TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
}
override fun addProfile(url: String?): ParcelableCompletedFuture {
TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
}
override fun queryAllProxies(): Array<ProxyGroup> {
return clash.queryProxyGroups().toTypedArray()
}
override fun queryGeneral(): General {
return clash.queryGeneral()
}
override fun setSelectProxy(proxy: String?, selected: String?): Boolean {
require(proxy != null && selected != null)
return clash.setSelectedProxy(proxy, selected)
}
override fun openBandwidthEvent(): ParcelablePipe {
TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
}
override fun queryAllProfiles(): ParcelableCompletedFuture {
TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
}
override fun startHealthCheck(group: String?): ParcelableCompletedFuture {
require(group != null)
return ParcelableCompletedFuture().apply {
clash.startHealthCheck(group).whenComplete { _: Unit?, u: Throwable? ->
if ( u != null )
this.completeExceptionally(u)
else
this.complete(null)
}
}
}
override fun openLogEvent(): ParcelablePipe {
return object: ParcelablePipe() {
val stream = clash.openLogEvent().apply {
onEvent {
send(it)
}
}
override fun onClose() {
stream.close()
}
}
}
}
@@ -65,7 +65,7 @@ class ClashServiceImpl(clashService: ClashService) : IClashService.Stub() {
val count = AtomicInteger(proxies.size)
proxies.forEach {
clash.startUrlTest(it)
clash.startHealthCheck(it)
}
}
@@ -5,30 +5,40 @@ import android.os.IBinder
import android.os.Parcel
import android.os.Parcelable
class ParcelablePipe() : Parcelable {
private inner class Slave: Binder() {
open class ParcelablePipe private constructor(val type: Type) : Parcelable {
enum class Type {
MASTER, SLAVE
}
private inner class Slave : Binder() {
override fun onTransact(code: Int, data: Parcel, reply: Parcel?, flags: Int): Boolean {
if ( code == TRANSACT_CODE_SEND_PARCELABLE ) {
if (code == TRANSACT_CODE_SEND_PARCELABLE) {
receiveCallback(data.readParcelable(ParcelablePipe::class.java.classLoader))
return true
}
return false
}
}
private inner class Master: Binder() {
private inner class Master : Binder() {
override fun onTransact(code: Int, data: Parcel, reply: Parcel?, flags: Int): Boolean {
if ( code == TRANSACT_CODE_SET_SLAVE ) {
slave = data.readStrongBinder()
if (code == TRANSACT_CODE_SET_SLAVE) {
connection = data.readStrongBinder()
return true
}
if (code == TRANSACT_CODE_CLOSE) {
onClose()
}
return false
}
}
private var slave: IBinder? = null
private var connection: IBinder? = null
private var receiveCallback: (Parcelable?) -> Unit = {}
constructor(parcel: Parcel) : this() {
constructor() : this(Type.MASTER)
constructor(parcel: Parcel) : this(Type.SLAVE) {
val master = parcel.readStrongBinder()
val data = Parcel.obtain()
@@ -36,14 +46,15 @@ class ParcelablePipe() : Parcelable {
data.writeStrongBinder(Slave())
master.transact(TRANSACT_CODE_SET_SLAVE, data, null, 0)
}
finally {
connection = master
} finally {
data.recycle()
}
}
override fun writeToParcel(dest: Parcel?, flags: Int) {
if ( dest == null )
if (dest == null)
return
dest.writeStrongBinder(Master())
@@ -52,28 +63,44 @@ class ParcelablePipe() : Parcelable {
override fun describeContents(): Int = 0
fun send(parcelable: Parcelable?): Boolean {
val s = slave ?: return false
if (type != Type.MASTER)
return false
val s = connection ?: return false
val data = Parcel.obtain()
try {
data.writeParcelable(parcelable, 0)
s.transact(TRANSACT_CODE_SEND_PARCELABLE, data, null, 0)
}
finally {
} finally {
data.recycle()
}
return true
}
fun close() {
val s = connection ?: return
val data = Parcel.obtain()
try {
s.transact(TRANSACT_CODE_CLOSE, data, null, 0)
} finally {
data.recycle()
}
}
fun onReceive(callback: (Parcelable?) -> Unit) {
this.receiveCallback = callback
}
open fun onClose() {}
companion object {
private const val TRANSACT_CODE_SET_SLAVE = 1
private const val TRANSACT_CODE_SEND_PARCELABLE = 2
private const val TRANSACT_CODE_CLOSE = 3
@JvmField
val CREATOR = object : Parcelable.Creator<ParcelablePipe> {