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 115280d4..3b370159 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 @@ -14,6 +14,7 @@ import androidx.annotation.RequiresApi import androidx.core.content.ContextCompat import androidx.lifecycle.MutableLiveData import com.hiddify.hiddify.Application +import com.hiddify.hiddify.R import com.hiddify.hiddify.Settings import com.hiddify.hiddify.constant.Action import com.hiddify.hiddify.constant.Alert @@ -140,6 +141,9 @@ class BoxService( private suspend fun startService(delayStart: Boolean = false) { try { Log.d(TAG, "starting service") + withContext(Dispatchers.Main) { + notification.show(activeProfileName, R.string.status_starting) + } val selectedConfigPath = Settings.activeConfigPath if (selectedConfigPath.isBlank()) { @@ -168,6 +172,7 @@ class BoxService( } withContext(Dispatchers.Main) { + notification.show(activeProfileName, R.string.status_starting) binder.broadcast { it.onServiceResetLogs(listOf()) } @@ -194,8 +199,9 @@ class BoxService( status.postValue(Status.Started) withContext(Dispatchers.Main) { - notification.show(activeProfileName) + notification.show(activeProfileName, R.string.status_started) } + notification.start() } catch (e: Exception) { stopAndAlert(Alert.StartService, e.message) return 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 4dcdd67e..5eb08620 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 @@ -9,6 +9,7 @@ import android.content.Context import android.content.Intent import android.content.IntentFilter import android.os.Build +import androidx.annotation.StringRes import androidx.core.app.NotificationCompat import androidx.core.app.ServiceCompat import androidx.lifecycle.MutableLiveData @@ -79,25 +80,27 @@ class ServiceNotification(private val status: MutableLiveData, private v } } - suspend fun show(profileName: String) { + fun show(profileName: String, @StringRes contentTextId: Int) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { Application.notification.createNotificationChannel( - NotificationChannel( - notificationChannel, "hiddify service", NotificationManager.IMPORTANCE_LOW - ) + NotificationChannel( + notificationChannel, "hiddify service", NotificationManager.IMPORTANCE_LOW + ) ) } service.startForeground( - notificationId, notificationBuilder + notificationId, notificationBuilder .setContentTitle(profileName.takeIf { it.isNotBlank() } ?: "Hiddify Next") - .setContentText("service started").build() + .setContentText(service.getString(contentTextId)).build() ) - withContext(Dispatchers.IO) { - if (Settings.dynamicNotification) { - commandClient.connect() - withContext(Dispatchers.Main) { - registerReceiver() - } + } + + + suspend fun start() { + if (Settings.dynamicNotification) { + commandClient.connect() + withContext(Dispatchers.Main) { + registerReceiver() } } } 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 da38b11a..7830c1e2 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 @@ -165,19 +165,14 @@ class VPNService : VpnService(), PlatformInterfaceWrapper { } } - if (options.isHTTPProxyEnabled) { + if (options.isHTTPProxyEnabled && Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { systemProxyAvailable = true systemProxyEnabled = Settings.systemProxyEnabled - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { - if (systemProxyEnabled) builder.setHttpProxy( - ProxyInfo.buildDirectProxy( - options.httpProxyServer, - options.httpProxyServerPort - ) + if (systemProxyEnabled) builder.setHttpProxy( + ProxyInfo.buildDirectProxy( + options.httpProxyServer, options.httpProxyServerPort ) - } else { - error("android: tun.platform.http_proxy requires android 10 or higher") - } + ) } else { systemProxyAvailable = false systemProxyEnabled = false diff --git a/android/app/src/main/res/values/strings.xml b/android/app/src/main/res/values/strings.xml index 31c16b69..608ae199 100644 --- a/android/app/src/main/res/values/strings.xml +++ b/android/app/src/main/res/values/strings.xml @@ -2,4 +2,6 @@ Stop Toggle + Service starting… + Service started \ No newline at end of file