Fix: fix time zone

This commit is contained in:
kr328 2021-09-12 11:46:20 +08:00
parent ecfb339680
commit 654c488ed8
8 changed files with 51 additions and 0 deletions

View File

@ -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,

View File

@ -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{}{}

View File

@ -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)
}

View File

@ -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}" }

View File

@ -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()

View File

@ -35,6 +35,7 @@ class ClashService : BaseService() {
install(StaticNotificationModule(self))
install(AppListCacheModule(self))
install(TimeZoneModule(self))
install(SuspendModule(self))
try {

View File

@ -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 {

View File

@ -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()
}
}
}