diff --git a/core/src/main/cpp/main.c b/core/src/main/cpp/main.c index 6c1f2ecebe9a7f6adbd39b902c557153a446344a..f1c632b0a71c73a7dbb815a9d50d3b992fd0eb13 100644 --- a/core/src/main/cpp/main.c +++ b/core/src/main/cpp/main.c @@ -84,6 +84,16 @@ Java_com_github_kr328_clash_core_bridge_Bridge_nativeNotifyDnsChanged(JNIEnv *en notifyDnsChanged(_dns_list); } +JNIEXPORT void JNICALL +Java_com_github_kr328_clash_core_bridge_Bridge_nativeNotifyTimeZoneChanged(JNIEnv *env, jobject thiz, + jstring name, jint offset) { + TRACE_METHOD(); + + scoped_string _name = get_string(name); + + notifyTimeZoneChanged(_name, offset); +} + JNIEXPORT void JNICALL Java_com_github_kr328_clash_core_bridge_Bridge_nativeNotifyInstalledAppChanged(JNIEnv *env, jobject thiz, diff --git a/core/src/main/golang/native/app.go b/core/src/main/golang/native/app.go index a2b5342baa3d445a551881bc921c2c29e5067516..b21224f156a121f20c50c56cf1ca6da88ee9e809 100644 --- a/core/src/main/golang/native/app.go +++ b/core/src/main/golang/native/app.go @@ -8,6 +8,7 @@ import ( "unsafe" "cfa/native/app" + "github.com/Dreamacro/clash/log" ) @@ -42,6 +43,12 @@ func notifyInstalledAppsChanged(uids C.c_string) { app.NotifyInstallAppsChanged(u) } +//export notifyTimeZoneChanged +func notifyTimeZoneChanged(name C.c_string, offset C.int) { + app.NotifyTimeZoneChanged(C.GoString(name), int(offset)) +} + + //export queryConfiguration func queryConfiguration() *C.char { response := &struct{}{} diff --git a/core/src/main/golang/native/app/app.go b/core/src/main/golang/native/app/app.go index a946c6ab81d26c1eae20ea45545eda21272d7891..7575d1e99da7322977bdef8b55eaa671da7c8f31 100644 --- a/core/src/main/golang/native/app/app.go +++ b/core/src/main/golang/native/app/app.go @@ -3,6 +3,7 @@ package app import ( "strconv" "strings" + "time" ) var appVersionName string @@ -46,3 +47,7 @@ func NotifyInstallAppsChanged(uidList string) { func QueryAppByUid(uid int) string { return installedAppsUid[uid] } + +func NotifyTimeZoneChanged(name string, offset int) { + time.Local = time.FixedZone(name, offset) +} \ No newline at end of file diff --git a/core/src/main/java/com/github/kr328/clash/core/Clash.kt b/core/src/main/java/com/github/kr328/clash/core/Clash.kt index eac180e169e9787e09c05906e64401061581e24e..3b2d5bb833642d2fd4436141899632d521fc87f1 100644 --- a/core/src/main/java/com/github/kr328/clash/core/Clash.kt +++ b/core/src/main/java/com/github/kr328/clash/core/Clash.kt @@ -52,6 +52,10 @@ object Clash { Bridge.nativeNotifyDnsChanged(dns.joinToString(separator = ",")) } + fun notifyTimeZoneChanged(name: String, offset: Int) { + Bridge.nativeNotifyTimeZoneChanged(name, offset) + } + fun notifyInstalledAppsChanged(uids: List<Pair<Int, String>>) { val uidList = uids.joinToString(separator = ",") { "${it.first}:${it.second}" } diff --git a/core/src/main/java/com/github/kr328/clash/core/bridge/Bridge.kt b/core/src/main/java/com/github/kr328/clash/core/bridge/Bridge.kt index a775853c79e2c013d2fdda47a20d9e6824dff53d..b77588a317957010ea91e03f18422de4d6cf3014 100644 --- a/core/src/main/java/com/github/kr328/clash/core/bridge/Bridge.kt +++ b/core/src/main/java/com/github/kr328/clash/core/bridge/Bridge.kt @@ -16,6 +16,7 @@ object Bridge { external fun nativeQueryTrafficNow(): Long external fun nativeQueryTrafficTotal(): Long external fun nativeNotifyDnsChanged(dnsList: String) + external fun nativeNotifyTimeZoneChanged(name: String, offset: Int) external fun nativeNotifyInstalledAppChanged(uidList: String) external fun nativeStartTun(fd: Int, mtu: Int, dns: String, blocking: String, cb: TunInterface) external fun nativeStopTun() diff --git a/service/src/main/java/com/github/kr328/clash/service/ClashService.kt b/service/src/main/java/com/github/kr328/clash/service/ClashService.kt index dd58a6449531d4ed08a40e8effaa3c81b038d767..14bde28bd2602b49f22411fc3fce0a4b4f704593 100644 --- a/service/src/main/java/com/github/kr328/clash/service/ClashService.kt +++ b/service/src/main/java/com/github/kr328/clash/service/ClashService.kt @@ -35,6 +35,7 @@ class ClashService : BaseService() { install(StaticNotificationModule(self)) install(AppListCacheModule(self)) + install(TimeZoneModule(self)) install(SuspendModule(self)) try { diff --git a/service/src/main/java/com/github/kr328/clash/service/TunService.kt b/service/src/main/java/com/github/kr328/clash/service/TunService.kt index f861145de6f8b2f6e9d9c5a74db33673b26ee741..a08a9312e6bdf5314cf7fa7c32802432ca37194f 100644 --- a/service/src/main/java/com/github/kr328/clash/service/TunService.kt +++ b/service/src/main/java/com/github/kr328/clash/service/TunService.kt @@ -40,6 +40,7 @@ class TunService : VpnService(), CoroutineScope by CoroutineScope(Dispatchers.De install(StaticNotificationModule(self)) install(AppListCacheModule(self)) + install(TimeZoneModule(self)) install(SuspendModule(self)) try { diff --git a/service/src/main/java/com/github/kr328/clash/service/clash/module/TimeZoneModule.kt b/service/src/main/java/com/github/kr328/clash/service/clash/module/TimeZoneModule.kt new file mode 100644 index 0000000000000000000000000000000000000000..08159517339ad7cfefe9114cc207e89b0336a935 --- /dev/null +++ b/service/src/main/java/com/github/kr328/clash/service/clash/module/TimeZoneModule.kt @@ -0,0 +1,22 @@ +package com.github.kr328.clash.service.clash.module + +import android.app.Service +import android.content.Intent +import com.github.kr328.clash.core.Clash +import java.util.* + +class TimeZoneModule(service: Service) : Module<Unit>(service) { + override suspend fun run() { + val timeZones = receiveBroadcast { + addAction(Intent.ACTION_TIMEZONE_CHANGED) + } + + while (true) { + val timeZone = TimeZone.getDefault() + + Clash.notifyTimeZoneChanged(timeZone.id, timeZone.rawOffset) + + timeZones.receive() + } + } +} \ No newline at end of file