فهرست منبع

Fix: fix time zone

kr328 4 سال پیش
والد
کامیت
654c488ed8

+ 10 - 0
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,

+ 7 - 0
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{}{}

+ 5 - 0
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)
+}

+ 4 - 0
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}" }
 

+ 1 - 0
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()

+ 1 - 0
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 {

+ 1 - 0
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 {

+ 22 - 0
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()
+        }
+    }
+}