Skip to content

Commit

Permalink
Minor fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
nekohasekai committed Jun 25, 2024
1 parent ad9e383 commit 3449863
Show file tree
Hide file tree
Showing 11 changed files with 101 additions and 115 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,4 @@ package io.nekohasekai.sfa.aidl;
interface IServiceCallback {
void onServiceStatusChanged(int status);
void onServiceAlert(int type, String message);
void onServiceWriteLog(String message);
void onServiceResetLogs(in List<String> messages);
}
33 changes: 6 additions & 27 deletions app/src/main/java/io/nekohasekai/sfa/bg/BoxService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ import io.nekohasekai.sfa.ktx.hasPermission
import kotlinx.coroutines.DelicateCoroutinesApi
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.withContext
Expand Down Expand Up @@ -74,14 +73,6 @@ class BoxService(
)
)
}

fun reload() {
Application.application.sendBroadcast(
Intent(Action.SERVICE_RELOAD).setPackage(
Application.application.packageName
)
)
}
}

var fileDescriptor: ParcelFileDescriptor? = null
Expand All @@ -99,9 +90,6 @@ class BoxService(
stopService()
}

Action.SERVICE_RELOAD -> {
serviceReload()
}

PowerManager.ACTION_DEVICE_IDLE_MODE_CHANGED -> {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
Expand All @@ -120,7 +108,7 @@ class BoxService(
}

private var lastProfileName = ""
private suspend fun startService(delayStart: Boolean = false) {
private suspend fun startService() {
try {
withContext(Dispatchers.Main) {
notification.show(lastProfileName, R.string.status_starting)
Expand All @@ -147,9 +135,6 @@ class BoxService(
lastProfileName = profile.name
withContext(Dispatchers.Main) {
notification.show(lastProfileName, R.string.status_starting)
binder.broadcast {
it.onServiceResetLogs(listOf())
}
}

DefaultNetworkMonitor.start()
Expand All @@ -163,10 +148,6 @@ class BoxService(
return
}

if (delayStart) {
delay(1000L)
}

newService.start()

if (newService.needWIFIState()) {
Expand Down Expand Up @@ -203,7 +184,6 @@ class BoxService(
pfd.close()
fileDescriptor = null
}
commandServer?.setService(null)
boxService?.apply {
runCatching {
close()
Expand All @@ -212,9 +192,11 @@ class BoxService(
}
Seq.destroyRef(refnum)
}
commandServer?.setService(null)
commandServer?.resetLog()
boxService = null
runBlocking {
startService(true)
startService()
}
}

Expand Down Expand Up @@ -259,7 +241,6 @@ class BoxService(
pfd.close()
fileDescriptor = null
}
commandServer?.setService(null)
boxService?.apply {
runCatching {
close()
Expand All @@ -268,6 +249,7 @@ class BoxService(
}
Seq.destroyRef(refnum)
}
commandServer?.setService(null)
boxService = null
Libbox.registerLocalDNSTransport(null)
DefaultNetworkMonitor.stop()
Expand Down Expand Up @@ -309,7 +291,6 @@ class BoxService(
if (!receiverRegistered) {
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)
}
Expand Down Expand Up @@ -344,9 +325,7 @@ class BoxService(
}

internal fun writeLog(message: String) {
binder.broadcast {
it.onServiceWriteLog(message)
}
commandServer?.writeMessage(message)
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,11 @@ interface PlatformInterfaceWrapper : PlatformInterface {

private class StringArray(private val iterator: Iterator<String>) : StringIterator {

override fun len(): Int {
// not used by core
return 0
}

override fun hasNext(): Boolean {
return iterator.hasNext()
}
Expand Down
7 changes: 0 additions & 7 deletions app/src/main/java/io/nekohasekai/sfa/bg/ServiceConnection.kt
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,6 @@ class ServiceConnection(
interface Callback {
fun onServiceStatusChanged(status: Status)
fun onServiceAlert(type: Alert, message: String?) {}
fun onServiceWriteLog(message: String?) {}
fun onServiceResetLogs(messages: MutableList<String>) {}
}

class ServiceCallback(private val callback: Callback) : IServiceCallback.Stub() {
Expand All @@ -106,10 +104,5 @@ class ServiceConnection(
override fun onServiceAlert(type: Int, message: String?) {
callback.onServiceAlert(Alert.values()[type], message)
}

override fun onServiceWriteLog(message: String?) = callback.onServiceWriteLog(message)

override fun onServiceResetLogs(messages: MutableList<String>) =
callback.onServiceResetLogs(messages)
}
}
1 change: 0 additions & 1 deletion app/src/main/java/io/nekohasekai/sfa/constant/Action.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,4 @@ package io.nekohasekai.sfa.constant
object Action {
const val SERVICE = "io.nekohasekai.sfa.SERVICE"
const val SERVICE_CLOSE = "io.nekohasekai.sfa.SERVICE_CLOSE"
const val SERVICE_RELOAD = "io.nekohasekai.sfa.SERVICE_RELOAD"
}
5 changes: 5 additions & 0 deletions app/src/main/java/io/nekohasekai/sfa/ktx/Wrappers.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ fun Iterable<String>.toStringIterator(): StringIterator {
return object : StringIterator {
val iterator = iterator()

override fun len(): Int {
// not used by core
return 0
}

override fun hasNext(): Boolean {
return iterator.hasNext()
}
Expand Down
37 changes: 0 additions & 37 deletions app/src/main/java/io/nekohasekai/sfa/ui/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import java.io.File
import java.util.Date
import java.util.LinkedList

class MainActivity : AbstractActivity<ActivityMainBinding>(),
PreferenceFragmentCompat.OnPreferenceStartFragmentCallback,
Expand All @@ -69,8 +68,6 @@ class MainActivity : AbstractActivity<ActivityMainBinding>(),

private val connection = ServiceConnection(this, this)

val logList = LinkedList<String>()
var logCallback: ((Boolean) -> Unit)? = null
val serviceStatus = MutableLiveData(Status.Stopped)

override fun onCreate(savedInstanceState: Bundle?) {
Expand Down Expand Up @@ -437,43 +434,9 @@ class MainActivity : AbstractActivity<ActivityMainBinding>(),
}
}


private var paused = false
override fun onPause() {
super.onPause()

paused = true
}

override fun onResume() {
super.onResume()

paused = false
logCallback?.invoke(true)
}

override fun onServiceWriteLog(message: String?) {
if (paused) {
if (logList.size > 300) {
logList.removeFirst()
}
}
logList.addLast(message)
if (!paused) {
logCallback?.invoke(false)
}
}

override fun onServiceResetLogs(messages: MutableList<String>) {
logList.clear()
logList.addAll(messages)
if (!paused) logCallback?.invoke(true)
}

override fun onDestroy() {
connection.disconnect()
super.onDestroy()
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -107,17 +107,27 @@ class OverviewFragment : Fragment() {
val status = Libbox.newStandaloneCommandClient().systemProxyStatus
withContext(Dispatchers.Main) {
binding.systemProxyCard.isVisible = status.available
binding.systemProxySwitch.setOnClickListener(null)
binding.systemProxySwitch.setOnCheckedChangeListener(null)
binding.systemProxySwitch.isChecked = status.enabled
binding.systemProxySwitch.isEnabled = true
var reloading = false
binding.systemProxySwitch.setOnCheckedChangeListener { buttonView, isChecked ->
binding.systemProxySwitch.isEnabled = false
lifecycleScope.launch(Dispatchers.IO) {
Settings.systemProxyEnabled = isChecked
runCatching {
Libbox.newStandaloneCommandClient().setSystemProxyEnabled(isChecked)
}.onFailure {
buttonView.context.errorDialogBuilder(it).show()
synchronized(this@OverviewFragment) {
if (reloading) return@setOnCheckedChangeListener
reloading = true
binding.systemProxySwitch.isEnabled = false
lifecycleScope.launch(Dispatchers.IO) {
Settings.systemProxyEnabled = isChecked
runCatching {
Libbox.newStandaloneCommandClient().setSystemProxyEnabled(isChecked)
}.onFailure {
withContext(Dispatchers.Main) {
buttonView.context.errorDialogBuilder(it).show()
}
}
withContext(Dispatchers.Main) {
delay(1000L)
binding.systemProxySwitch.isEnabled = true
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ class DashboardFragment : Fragment(R.layout.fragment_dashboard) {
enablePager()
binding.fab.setImageResource(R.drawable.ic_stop_24)
binding.fab.show()
binding.fab.isEnabled = true
}

Status.Stopping -> {
Expand All @@ -65,6 +66,7 @@ class DashboardFragment : Fragment(R.layout.fragment_dashboard) {
binding.fab.setOnClickListener {
when (activity.serviceStatus.value) {
Status.Stopped -> {
it.isEnabled = false
activity.startService()
}

Expand Down
Loading

0 comments on commit 3449863

Please sign in to comment.