Skip to content

Commit

Permalink
Add android dynamic notification
Browse files Browse the repository at this point in the history
  • Loading branch information
0xConsumer committed Dec 14, 2023
1 parent b9f1e83 commit af64efe
Show file tree
Hide file tree
Showing 19 changed files with 251 additions and 95 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ class Application : Application() {
val connectivity by lazy { application.getSystemService<ConnectivityManager>()!! }
val packageManager by lazy { application.packageManager }
val powerManager by lazy { application.getSystemService<PowerManager>()!! }
val notificationManager by lazy { application.getSystemService<NotificationManager>()!! }
}

}
24 changes: 12 additions & 12 deletions android/app/src/main/kotlin/com/hiddify/hiddify/MethodHandler.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,18 @@ package com.hiddify.hiddify

import android.util.Log
import com.hiddify.hiddify.bg.BoxService
import com.hiddify.hiddify.constant.Alert
import com.hiddify.hiddify.constant.Status
import io.flutter.embedding.engine.plugins.FlutterPlugin
import io.flutter.plugin.common.MethodCall
import io.flutter.plugin.common.MethodChannel
import io.flutter.plugin.common.StandardMethodCodec
import io.nekohasekai.libbox.Libbox
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch

class MethodHandler(private val scope: CoroutineScope) : FlutterPlugin,
MethodChannel.MethodCallHandler {
MethodChannel.MethodCallHandler {
private var channel: MethodChannel? = null

companion object {
Expand All @@ -37,8 +35,8 @@ class MethodHandler(private val scope: CoroutineScope) : FlutterPlugin,

override fun onAttachedToEngine(flutterPluginBinding: FlutterPlugin.FlutterPluginBinding) {
channel = MethodChannel(
flutterPluginBinding.binaryMessenger,
channelName,
flutterPluginBinding.binaryMessenger,
channelName,
)
channel!!.setMethodCallHandler(this)
}
Expand Down Expand Up @@ -92,6 +90,7 @@ class MethodHandler(private val scope: CoroutineScope) : FlutterPlugin,
result.runCatching {
val args = call.arguments as Map<*, *>
Settings.activeConfigPath = args["path"] as String? ?: ""
Settings.activeProfileName = args["name"] as String? ?: ""
val mainActivity = MainActivity.instance
val started = mainActivity.serviceStatus.value == Status.Started
if (started) {
Expand Down Expand Up @@ -124,6 +123,7 @@ class MethodHandler(private val scope: CoroutineScope) : FlutterPlugin,
result.runCatching {
val args = call.arguments as Map<*, *>
Settings.activeConfigPath = args["path"] as String? ?: ""
Settings.activeProfileName = args["name"] as String? ?: ""
val mainActivity = MainActivity.instance
val started = mainActivity.serviceStatus.value == Status.Started
if (!started) return@launch success(true)
Expand All @@ -150,10 +150,10 @@ class MethodHandler(private val scope: CoroutineScope) : FlutterPlugin,
result.runCatching {
val args = call.arguments as Map<*, *>
Libbox.newStandaloneCommandClient()
.selectOutbound(
args["groupTag"] as String,
args["outboundTag"] as String
)
.selectOutbound(
args["groupTag"] as String,
args["outboundTag"] as String
)
success(true)
}
}
Expand All @@ -164,9 +164,9 @@ class MethodHandler(private val scope: CoroutineScope) : FlutterPlugin,
result.runCatching {
val args = call.arguments as Map<*, *>
Libbox.newStandaloneCommandClient()
.urlTest(
args["groupTag"] as String
)
.urlTest(
args["groupTag"] as String
)
success(true)
}
}
Expand Down
9 changes: 9 additions & 0 deletions android/app/src/main/kotlin/com/hiddify/hiddify/Settings.kt
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ object Settings {
get() = preferences.getString(SettingsKey.ACTIVE_CONFIG_PATH, "")!!
set(value) = preferences.edit().putString(SettingsKey.ACTIVE_CONFIG_PATH, value).apply()

var activeProfileName: String
get() = preferences.getString(SettingsKey.ACTIVE_PROFILE_NAME, "")!!
set(value) = preferences.edit().putString(SettingsKey.ACTIVE_PROFILE_NAME, value).apply()

var serviceMode: String
get() = preferences.getString(SettingsKey.SERVICE_MODE, ServiceMode.NORMAL)!!
set(value) = preferences.edit().putString(SettingsKey.SERVICE_MODE, value).apply()
Expand All @@ -71,6 +75,11 @@ object Settings {
set(value) =
preferences.edit().putBoolean(SettingsKey.DISABLE_MEMORY_LIMIT, value).apply()

var dynamicNotification: Boolean
get() = preferences.getBoolean(SettingsKey.DYNAMIC_NOTIFICATION, true)
set(value) =
preferences.edit().putBoolean(SettingsKey.DYNAMIC_NOTIFICATION, value).apply()

var systemProxyEnabled: Boolean
get() = preferences.getBoolean(SettingsKey.SYSTEM_PROXY_ENABLED, true)
set(value) =
Expand Down
65 changes: 35 additions & 30 deletions android/app/src/main/kotlin/com/hiddify/hiddify/bg/BoxService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import io.nekohasekai.libbox.BoxService
import io.nekohasekai.libbox.CommandServer
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
Expand All @@ -36,8 +35,8 @@ import kotlinx.coroutines.withContext
import java.io.File

class BoxService(
private val service: Service,
private val platformInterface: PlatformInterface
private val service: Service,
private val platformInterface: PlatformInterface
) : CommandServerHandler {

companion object {
Expand Down Expand Up @@ -72,8 +71,8 @@ class BoxService(
}
}

fun buildConfig(path: String, options: String):String {
return Mobile.buildConfig(path, options)
fun buildConfig(path: String, options: String): String {
return Mobile.buildConfig(path, options)
}

fun start() {
Expand All @@ -87,17 +86,17 @@ class BoxService(

fun stop() {
Application.application.sendBroadcast(
Intent(Action.SERVICE_CLOSE).setPackage(
Application.application.packageName
)
Intent(Action.SERVICE_CLOSE).setPackage(
Application.application.packageName
)
)
}

fun reload() {
Application.application.sendBroadcast(
Intent(Action.SERVICE_RELOAD).setPackage(
Application.application.packageName
)
Intent(Action.SERVICE_RELOAD).setPackage(
Application.application.packageName
)
)
}
}
Expand All @@ -106,10 +105,9 @@ class BoxService(

private val status = MutableLiveData(Status.Stopped)
private val binder = ServiceBinder(status)
private val notification = ServiceNotification(service)
private val notification = ServiceNotification(status, service)
private var boxService: BoxService? = null
private var commandServer: CommandServer? = null
private var pprofServer: PProfServer? = null
private var receiverRegistered = false
private val receiver = object : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
Expand All @@ -133,11 +131,12 @@ class BoxService(

private fun startCommandServer() {
val commandServer =
CommandServer(this, 300)
CommandServer(this, 300)
commandServer.start()
this.commandServer = commandServer
}

private var activeProfileName = ""
private suspend fun startService(delayStart: Boolean = false) {
try {
Log.d(TAG, "starting service")
Expand All @@ -148,6 +147,8 @@ class BoxService(
return
}

activeProfileName = Settings.activeProfileName

val configOptions = Settings.configOptions
if (configOptions.isBlank()) {
stopAndAlert(Alert.EmptyConfiguration)
Expand Down Expand Up @@ -191,30 +192,35 @@ class BoxService(
boxService = newService
commandServer?.setService(boxService)
status.postValue(Status.Started)

withContext(Dispatchers.Main) {
notification.show(activeProfileName)
}
} catch (e: Exception) {
stopAndAlert(Alert.StartService, e.message)
return
}
}

override fun serviceReload() {
notification.close()
status.postValue(Status.Starting)
runBlocking {
val pfd = fileDescriptor
if (pfd != null) {
pfd.close()
fileDescriptor = null
}
commandServer?.setService(null)
boxService?.apply {
runCatching {
close()
}.onFailure {
writeLog("service: error when closing: $it")
}
Seq.destroyRef(refnum)
val pfd = fileDescriptor
if (pfd != null) {
pfd.close()
fileDescriptor = null
}
commandServer?.setService(null)
boxService?.apply {
runCatching {
close()
}.onFailure {
writeLog("service: error when closing: $it")
}
boxService = null
Seq.destroyRef(refnum)
}
boxService = null
runBlocking {
startService(true)
}
}
Expand Down Expand Up @@ -311,7 +317,6 @@ class BoxService(
receiverRegistered = true
}

notification.show()
GlobalScope.launch(Dispatchers.IO) {
Settings.startedByUser = true
initialize()
Expand Down
Loading

0 comments on commit af64efe

Please sign in to comment.