diff --git a/android/app/src/main/kotlin/com/hiddify/hiddify/bg/BoxService.kt b/android/app/src/main/kotlin/com/hiddify/hiddify/bg/BoxService.kt index 2702b835..6896952c 100644 --- a/android/app/src/main/kotlin/com/hiddify/hiddify/bg/BoxService.kt +++ b/android/app/src/main/kotlin/com/hiddify/hiddify/bg/BoxService.kt @@ -5,9 +5,12 @@ import android.content.BroadcastReceiver import android.content.Context import android.content.Intent import android.content.IntentFilter +import android.os.Build import android.os.IBinder import android.os.ParcelFileDescriptor +import android.os.PowerManager import android.util.Log +import androidx.annotation.RequiresApi import androidx.core.content.ContextCompat import androidx.lifecycle.MutableLiveData import com.hiddify.hiddify.Application @@ -22,6 +25,7 @@ import io.nekohasekai.libbox.CommandServerHandler import io.nekohasekai.libbox.Libbox import io.nekohasekai.libbox.PProfServer import io.nekohasekai.libbox.PlatformInterface +import io.nekohasekai.libbox.SystemProxyStatus import io.nekohasekai.mobile.Mobile import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.GlobalScope @@ -111,6 +115,12 @@ class BoxService( Action.SERVICE_RELOAD -> { serviceReload() } + + PowerManager.ACTION_DEVICE_IDLE_MODE_CHANGED -> { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { + serviceUpdateIdleMode() + } + } } } } @@ -174,6 +184,7 @@ class BoxService( } override fun serviceReload() { + status.postValue(Status.Starting) GlobalScope.launch(Dispatchers.IO) { val pfd = fileDescriptor if (pfd != null) { @@ -194,6 +205,28 @@ class BoxService( } } + override fun getSystemProxyStatus(): SystemProxyStatus { + val status = SystemProxyStatus() + if (service is VPNService) { + status.available = service.systemProxyAvailable + status.enabled = service.systemProxyEnabled + } + return status + } + + override fun setSystemProxyEnabled(isEnabled: Boolean) { + serviceReload() + } + + @RequiresApi(Build.VERSION_CODES.M) + private fun serviceUpdateIdleMode() { + if (Application.powerManager.isDeviceIdleMode) { + boxService?.sleep() + } else { + boxService?.wake() + } + } + private fun stopService() { if (status.value != Status.Started) return status.value = Status.Stopping @@ -257,6 +290,9 @@ class BoxService( ContextCompat.registerReceiver(service, receiver, IntentFilter().apply { addAction(Action.SERVICE_CLOSE) addAction(Action.SERVICE_RELOAD) + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { + addAction(PowerManager.ACTION_DEVICE_IDLE_MODE_CHANGED) + } }, ContextCompat.RECEIVER_NOT_EXPORTED) receiverRegistered = true } diff --git a/android/app/src/main/kotlin/com/hiddify/hiddify/bg/ServiceNotification.kt b/android/app/src/main/kotlin/com/hiddify/hiddify/bg/ServiceNotification.kt index 7dccf6dc..d899cc82 100644 --- a/android/app/src/main/kotlin/com/hiddify/hiddify/bg/ServiceNotification.kt +++ b/android/app/src/main/kotlin/com/hiddify/hiddify/bg/ServiceNotification.kt @@ -24,10 +24,7 @@ class ServiceNotification(private val service: Service) { if (Build.VERSION.SDK_INT < Build.VERSION_CODES.TIRAMISU) { return true } - if (Application.notification.areNotificationsEnabled()) { - return true - } - return false + return Application.notification.areNotificationsEnabled() } } diff --git a/android/app/src/main/kotlin/com/hiddify/hiddify/bg/VPNService.kt b/android/app/src/main/kotlin/com/hiddify/hiddify/bg/VPNService.kt index 76b19b69..044d19c5 100644 --- a/android/app/src/main/kotlin/com/hiddify/hiddify/bg/VPNService.kt +++ b/android/app/src/main/kotlin/com/hiddify/hiddify/bg/VPNService.kt @@ -32,6 +32,9 @@ class VPNService : VpnService(), PlatformInterfaceWrapper { protect(fd) } + var systemProxyAvailable = false + var systemProxyEnabled = true + override fun openTun(options: TunOptions): Int { if (prepare(this) != null) error("android: missing vpn permission") @@ -124,8 +127,10 @@ class VPNService : VpnService(), PlatformInterfaceWrapper { } if (options.isHTTPProxyEnabled) { + systemProxyAvailable = true +// systemProxyEnabled = Settings.systemProxyEnabled if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { - builder.setHttpProxy( + if (systemProxyEnabled) builder.setHttpProxy( ProxyInfo.buildDirectProxy( options.httpProxyServer, options.httpProxyServerPort @@ -134,6 +139,9 @@ class VPNService : VpnService(), PlatformInterfaceWrapper { } else { error("android: tun.platform.http_proxy requires android 10 or higher") } + } else { + systemProxyAvailable = false + systemProxyEnabled = false } val pfd =