fix crash on tile start

reset clash bandwidth on start
add version name to about dialog
This commit is contained in:
Kr328 committed 2019-12-20 10:51:55 +08:00
1 parent a287e287b9
commit 382f083fd1
11 files changed
+111 -81

No files matched your search

+2 -2
View File
@@ -13,8 +13,8 @@ android {
applicationId "com.github.kr328.clash"
minSdkVersion 24
targetSdkVersion 29
versionCode 10027
versionName "1.0.27-alpha"
versionCode 10028
versionName "1.0.28-alpha"
}
buildTypes {
release {
@@ -3,8 +3,12 @@ package com.github.kr328.clash
import android.app.Activity
import android.app.AlertDialog
import android.content.Intent
import android.content.pm.PackageInfo
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import com.github.kr328.clash.core.event.*
import com.github.kr328.clash.core.model.General
import com.github.kr328.clash.core.utils.ByteFormatter
@@ -194,7 +198,11 @@ class MainActivity : BaseActivity() {
}
private fun showAboutDialog() {
AlertDialog.Builder(this).setView(R.layout.dialog_about).show()
val view = LayoutInflater.from(this).inflate(R.layout.dialog_about, window.decorView as ViewGroup?, false)
view.findViewById<TextView>(android.R.id.summary).text = packageManager.getPackageInfo(packageName, 0).let(PackageInfo::versionName)
AlertDialog.Builder(this).setView(view).show()
}
override fun onErrorEvent(event: ErrorEvent?) {
+1 -1
View File
@@ -19,9 +19,9 @@
android:layout_height="wrap_content"
android:layout_toEndOf="@android:id/icon" />
<TextView
android:id="@android:id/summary"
android:layout_alignStart="@android:id/title"
android:layout_below="@android:id/title"
android:text="@string/about_power_by_clash"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</RelativeLayout>
+9
View File
@@ -2,6 +2,15 @@ package bridge
import "github.com/Dreamacro/clash/constant"
import "github.com/kr328/cfa/profile"
import "github.com/Dreamacro/clash/tunnel"
func Init(home string) {
constant.SetHomeDir(home)
}
func Reset() {
profile.LoadDefault()
tunnel.DefaultManager.Reset()
}
-4
View File
@@ -6,10 +6,6 @@ func LoadProfileFile(path string) error {
return profile.LoadFromFile(path)
}
func LoadProfileDefault() {
profile.LoadDefault()
}
func CheckProfileValid(profileData string) error {
return profile.CheckValid(profileData)
}
@@ -62,7 +62,7 @@ class Clash(
listener(currentProcess)
loadDefault()
Bridge.reset()
}
fun stop() {
@@ -73,7 +73,7 @@ class Clash(
listener(currentProcess)
loadDefault()
Bridge.reset()
}
fun startTunDevice(
@@ -173,10 +173,6 @@ class Clash(
})
}
private fun loadDefault() {
Bridge.loadProfileDefault()
}
private fun enforceStarted() {
if ( currentProcess == ProcessEvent.STOPPED )
throw IllegalStateException("Clash Stopped")
@@ -75,4 +75,10 @@ class ClashEventPoll(private val clash: Clash, private val master: Master) {
logs = null
}
}
fun shutdown() {
stopTrafficPoll()
stopBandwidthPoll()
stopLogPoll()
}
}
@@ -20,79 +20,87 @@ class ClashEventService(private val master: Master) : IClashEventService.Stub()
)
private val observers = mutableMapOf<String, EventObserverRecord>()
private val handler = Executors.newSingleThreadExecutor()
private val executor = Executors.newSingleThreadExecutor()
private var currentProcessEvent = ProcessEvent.STOPPED
fun performProcessEvent(event: ProcessEvent) {
handler.submit {
currentProcessEvent = event
if (!executor.isShutdown)
executor.submit {
currentProcessEvent = event
observers.values.forEach {
it.observer.onProcessEvent(event)
observers.values.forEach {
it.observer.onProcessEvent(event)
}
}
}
}
fun performLogEvent(event: LogEvent) {
handler.submit {
observers.values.forEach {
if (it.acquiredEvent.contains(Event.EVENT_LOG))
it.observer.onLogEvent(event)
if (!executor.isShutdown)
executor.submit {
observers.values.forEach {
if (it.acquiredEvent.contains(Event.EVENT_LOG))
it.observer.onLogEvent(event)
}
}
}
}
fun performSpeedEvent(event: TrafficEvent) {
handler.submit {
observers.values.forEach {
if (it.acquiredEvent.contains(Event.EVENT_TRAFFIC))
it.observer.onTrafficEvent(event)
if (!executor.isShutdown)
executor.submit {
observers.values.forEach {
if (it.acquiredEvent.contains(Event.EVENT_TRAFFIC))
it.observer.onTrafficEvent(event)
}
}
}
}
fun performBandwidthEvent(event: BandwidthEvent) {
handler.submit {
observers.values.forEach {
if ( it.acquiredEvent.contains(Event.EVENT_BANDWIDTH) )
it.observer.onBandwidthEvent(event)
if (!executor.isShutdown)
executor.submit {
observers.values.forEach {
if (it.acquiredEvent.contains(Event.EVENT_BANDWIDTH))
it.observer.onBandwidthEvent(event)
}
}
}
}
fun performErrorEvent(event: ErrorEvent) {
handler.submit {
observers.values.forEach {
it.observer.onErrorEvent(event)
if (!executor.isShutdown)
executor.submit {
observers.values.forEach {
it.observer.onErrorEvent(event)
}
}
}
}
fun performProfileChangedEvent(event: ProfileChangedEvent) {
handler.submit {
observers.values.forEach {
it.observer.onProfileChanged(event)
if (!executor.isShutdown)
executor.submit {
observers.values.forEach {
it.observer.onProfileChanged(event)
}
}
}
}
fun performProfileReloadEvent(event: ProfileReloadEvent) {
handler.submit {
observers.values.forEach {
it.observer.onProfileReloaded(event)
if (!executor.isShutdown)
executor.submit {
observers.values.forEach {
it.observer.onProfileReloaded(event)
}
}
}
}
override fun unregisterEventObserver(id: String?) {
handler.submit {
require(id != null)
if (!executor.isShutdown)
executor.submit {
require(id != null)
observers.remove(id)
observers.remove(id)
recastEventRequirement()
}
recastEventRequirement()
}
}
override fun registerEventObserver(
@@ -100,38 +108,40 @@ class ClashEventService(private val master: Master) : IClashEventService.Stub()
observer: IClashEventObserver?,
events: IntArray?
) {
handler.submit {
require(id != null && observer != null && events != null)
if (!executor.isShutdown)
executor.submit {
require(id != null && observer != null && events != null)
val initial = !observers.containsKey(id)
val initial = !observers.containsKey(id)
observers[id] = EventObserverRecord(observer, events.toSet())
observers[id] = EventObserverRecord(observer, events.toSet())
observer.asBinder().linkToDeath({
unregisterEventObserver(id)
}, 0)
observer.asBinder().linkToDeath({
unregisterEventObserver(id)
}, 0)
recastEventRequirement()
recastEventRequirement()
if (initial) {
observer.onProcessEvent(currentProcessEvent)
if (initial) {
observer.onProcessEvent(currentProcessEvent)
}
}
}
}
fun shutdown() {
handler.shutdown()
}
fun recastEventRequirement() {
handler.submit {
val req = observers.values.flatMap {
it.acquiredEvent
}.toSet()
val rel = EVENT_SET - req
if (!executor.isShutdown)
executor.submit {
val req = observers.values.flatMap {
it.acquiredEvent
}.toSet()
val rel = EVENT_SET - req
req.forEach(master::acquireEvent)
rel.forEach(master::releaseEvent)
}
req.forEach(master::acquireEvent)
rel.forEach(master::releaseEvent)
}
}
fun shutdown() {
executor.shutdown()
}
}
@@ -113,6 +113,7 @@ class ClashServiceImpl(clashService: ClashService) : IClashService.Stub() {
}
fun shutdown() {
eventPoll.shutdown()
eventService.shutdown()
}
}
@@ -10,13 +10,17 @@ import android.os.*
import com.github.kr328.clash.core.event.*
import com.github.kr328.clash.core.utils.Log
import com.github.kr328.clash.service.net.DefaultNetworkObserver
import java.net.Inet4Address
import java.net.InetAddress
class TunService : VpnService(), IClashEventObserver {
companion object {
// from https://github.com/shadowsocks/shadowsocks-android/blob/master/core/src/main/java/com/github/shadowsocks/bg/VpnService.kt
private const val VPN_MTU = 1500
private const val PRIVATE_VLAN_DNS = "172.19.0.2"
private const val PRIVATE_VLAN4_CLIENT = "172.19.0.1"
private const val PRIVATE_VLAN4_SUBNET = 30
private const val PRIVATE_VLAN4_CLIENT = "172.255.255.253"
private const val PRIVATE_VLAN_DNS = "172.255.255.254"
private const val VLAN4_ANY = "0.0.0.0"
}
private var start = true
@@ -114,7 +118,7 @@ class TunService : VpnService(), IClashEventObserver {
if ( settings.isDnsHijackingEnabled ) {
clash.clash.startTunDevice(
fileDescriptor.fd, VPN_MTU,
"$PRIVATE_VLAN4_CLIENT/30", "0.0.0.0"
"$PRIVATE_VLAN4_CLIENT/$PRIVATE_VLAN4_SUBNET", VLAN4_ANY
) {
protect(it.toInt())
}
@@ -122,7 +126,7 @@ class TunService : VpnService(), IClashEventObserver {
else {
clash.clash.startTunDevice(
fileDescriptor.fd, VPN_MTU,
"$PRIVATE_VLAN4_CLIENT/30", PRIVATE_VLAN_DNS
"$PRIVATE_VLAN4_CLIENT/$PRIVATE_VLAN4_SUBNET", PRIVATE_VLAN_DNS
) {
protect(it.toInt())
}
@@ -195,7 +199,7 @@ class TunService : VpnService(), IClashEventObserver {
}
private fun Builder.addAddress(): Builder {
addAddress(PRIVATE_VLAN4_CLIENT, 30)
addAddress(PRIVATE_VLAN4_CLIENT, PRIVATE_VLAN4_SUBNET)
return this
}