Fix android bugs

This commit is contained in:
problematicconsumer
2024-02-09 14:12:41 +03:30
parent 7394f7c4c3
commit 8a7a907fd3
4 changed files with 29 additions and 23 deletions

View File

@@ -14,6 +14,7 @@ import androidx.annotation.RequiresApi
import androidx.core.content.ContextCompat import androidx.core.content.ContextCompat
import androidx.lifecycle.MutableLiveData import androidx.lifecycle.MutableLiveData
import com.hiddify.hiddify.Application import com.hiddify.hiddify.Application
import com.hiddify.hiddify.R
import com.hiddify.hiddify.Settings import com.hiddify.hiddify.Settings
import com.hiddify.hiddify.constant.Action import com.hiddify.hiddify.constant.Action
import com.hiddify.hiddify.constant.Alert import com.hiddify.hiddify.constant.Alert
@@ -140,6 +141,9 @@ class BoxService(
private suspend fun startService(delayStart: Boolean = false) { private suspend fun startService(delayStart: Boolean = false) {
try { try {
Log.d(TAG, "starting service") Log.d(TAG, "starting service")
withContext(Dispatchers.Main) {
notification.show(activeProfileName, R.string.status_starting)
}
val selectedConfigPath = Settings.activeConfigPath val selectedConfigPath = Settings.activeConfigPath
if (selectedConfigPath.isBlank()) { if (selectedConfigPath.isBlank()) {
@@ -168,6 +172,7 @@ class BoxService(
} }
withContext(Dispatchers.Main) { withContext(Dispatchers.Main) {
notification.show(activeProfileName, R.string.status_starting)
binder.broadcast { binder.broadcast {
it.onServiceResetLogs(listOf()) it.onServiceResetLogs(listOf())
} }
@@ -194,8 +199,9 @@ class BoxService(
status.postValue(Status.Started) status.postValue(Status.Started)
withContext(Dispatchers.Main) { withContext(Dispatchers.Main) {
notification.show(activeProfileName) notification.show(activeProfileName, R.string.status_started)
} }
notification.start()
} catch (e: Exception) { } catch (e: Exception) {
stopAndAlert(Alert.StartService, e.message) stopAndAlert(Alert.StartService, e.message)
return return

View File

@@ -9,6 +9,7 @@ import android.content.Context
import android.content.Intent import android.content.Intent
import android.content.IntentFilter import android.content.IntentFilter
import android.os.Build import android.os.Build
import androidx.annotation.StringRes
import androidx.core.app.NotificationCompat import androidx.core.app.NotificationCompat
import androidx.core.app.ServiceCompat import androidx.core.app.ServiceCompat
import androidx.lifecycle.MutableLiveData import androidx.lifecycle.MutableLiveData
@@ -79,7 +80,7 @@ class ServiceNotification(private val status: MutableLiveData<Status>, private v
} }
} }
suspend fun show(profileName: String) { fun show(profileName: String, @StringRes contentTextId: Int) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
Application.notification.createNotificationChannel( Application.notification.createNotificationChannel(
NotificationChannel( NotificationChannel(
@@ -90,9 +91,12 @@ class ServiceNotification(private val status: MutableLiveData<Status>, private v
service.startForeground( service.startForeground(
notificationId, notificationBuilder notificationId, notificationBuilder
.setContentTitle(profileName.takeIf { it.isNotBlank() } ?: "Hiddify Next") .setContentTitle(profileName.takeIf { it.isNotBlank() } ?: "Hiddify Next")
.setContentText("service started").build() .setContentText(service.getString(contentTextId)).build()
) )
withContext(Dispatchers.IO) { }
suspend fun start() {
if (Settings.dynamicNotification) { if (Settings.dynamicNotification) {
commandClient.connect() commandClient.connect()
withContext(Dispatchers.Main) { withContext(Dispatchers.Main) {
@@ -100,7 +104,6 @@ class ServiceNotification(private val status: MutableLiveData<Status>, private v
} }
} }
} }
}
private fun registerReceiver() { private fun registerReceiver() {
service.registerReceiver(this, IntentFilter().apply { service.registerReceiver(this, IntentFilter().apply {

View File

@@ -165,19 +165,14 @@ class VPNService : VpnService(), PlatformInterfaceWrapper {
} }
} }
if (options.isHTTPProxyEnabled) { if (options.isHTTPProxyEnabled && Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
systemProxyAvailable = true systemProxyAvailable = true
systemProxyEnabled = Settings.systemProxyEnabled systemProxyEnabled = Settings.systemProxyEnabled
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
if (systemProxyEnabled) builder.setHttpProxy( if (systemProxyEnabled) builder.setHttpProxy(
ProxyInfo.buildDirectProxy( ProxyInfo.buildDirectProxy(
options.httpProxyServer, options.httpProxyServer, options.httpProxyServerPort
options.httpProxyServerPort
) )
) )
} else {
error("android: tun.platform.http_proxy requires android 10 or higher")
}
} else { } else {
systemProxyAvailable = false systemProxyAvailable = false
systemProxyEnabled = false systemProxyEnabled = false

View File

@@ -2,4 +2,6 @@
<resources> <resources>
<string name="stop">Stop</string> <string name="stop">Stop</string>
<string name="quick_toggle">Toggle</string> <string name="quick_toggle">Toggle</string>
<string name="status_starting">Service starting…</string>
<string name="status_started">Service started</string>
</resources> </resources>