Commit 24bd70a4 authored by Mygod's avatar Mygod

Refine IntentFilters

parent 32aa8007
...@@ -43,7 +43,6 @@ import androidx.work.WorkManager ...@@ -43,7 +43,6 @@ import androidx.work.WorkManager
import com.crashlytics.android.Crashlytics import com.crashlytics.android.Crashlytics
import com.github.shadowsocks.acl.Acl import com.github.shadowsocks.acl.Acl
import com.github.shadowsocks.aidl.ShadowsocksConnection import com.github.shadowsocks.aidl.ShadowsocksConnection
import com.github.shadowsocks.bg.BaseService
import com.github.shadowsocks.core.R import com.github.shadowsocks.core.R
import com.github.shadowsocks.database.Profile import com.github.shadowsocks.database.Profile
import com.github.shadowsocks.database.ProfileManager import com.github.shadowsocks.database.ProfileManager
...@@ -145,18 +144,19 @@ object Core { ...@@ -145,18 +144,19 @@ object Core {
fun reloadService() = app.sendBroadcast(Intent(Action.RELOAD)) fun reloadService() = app.sendBroadcast(Intent(Action.RELOAD))
fun stopService() = app.sendBroadcast(Intent(Action.CLOSE)) fun stopService() = app.sendBroadcast(Intent(Action.CLOSE))
fun listenForPackageChanges(onetime: Boolean = true, callback: () -> Unit): BroadcastReceiver { fun listenForPackageChanges(onetime: Boolean = true, callback: () -> Unit) = object : BroadcastReceiver() {
val filter = IntentFilter(Intent.ACTION_PACKAGE_ADDED) init {
filter.addAction(Intent.ACTION_PACKAGE_REMOVED) app.registerReceiver(this, IntentFilter().apply {
filter.addDataScheme("package") addAction(Intent.ACTION_PACKAGE_ADDED)
val result = object : BroadcastReceiver() { addAction(Intent.ACTION_PACKAGE_REMOVED)
override fun onReceive(context: Context, intent: Intent) { addDataScheme("package")
if (intent.getBooleanExtra(Intent.EXTRA_REPLACING, false)) return })
callback() }
if (onetime) app.unregisterReceiver(this)
} override fun onReceive(context: Context, intent: Intent) {
if (intent.getBooleanExtra(Intent.EXTRA_REPLACING, false)) return
callback()
if (onetime) app.unregisterReceiver(this)
} }
app.registerReceiver(result, filter)
return result
} }
} }
...@@ -36,8 +36,10 @@ import com.github.shadowsocks.aidl.IShadowsocksServiceCallback ...@@ -36,8 +36,10 @@ import com.github.shadowsocks.aidl.IShadowsocksServiceCallback
import com.github.shadowsocks.aidl.TrafficStats import com.github.shadowsocks.aidl.TrafficStats
import com.github.shadowsocks.core.R import com.github.shadowsocks.core.R
import com.github.shadowsocks.plugin.PluginManager import com.github.shadowsocks.plugin.PluginManager
import com.github.shadowsocks.preference.DataStore import com.github.shadowsocks.utils.Action
import com.github.shadowsocks.utils.* import com.github.shadowsocks.utils.broadcastReceiver
import com.github.shadowsocks.utils.printLog
import com.github.shadowsocks.utils.thread
import com.google.firebase.analytics.FirebaseAnalytics import com.google.firebase.analytics.FirebaseAnalytics
import java.io.File import java.io.File
import java.net.UnknownHostException import java.net.UnknownHostException
...@@ -280,12 +282,11 @@ object BaseService { ...@@ -280,12 +282,11 @@ object BaseService {
if (fallback != null) data.udpFallback = ProxyInstance(fallback, profile.route) if (fallback != null) data.udpFallback = ProxyInstance(fallback, profile.route)
if (!data.closeReceiverRegistered) { if (!data.closeReceiverRegistered) {
// register close receiver registerReceiver(data.closeReceiver, IntentFilter().apply {
val filter = IntentFilter() addAction(Action.RELOAD)
filter.addAction(Action.RELOAD) addAction(Intent.ACTION_SHUTDOWN)
filter.addAction(Intent.ACTION_SHUTDOWN) addAction(Action.CLOSE)
filter.addAction(Action.CLOSE) })
registerReceiver(data.closeReceiver, filter)
data.closeReceiverRegistered = true data.closeReceiverRegistered = true
} }
......
...@@ -86,11 +86,11 @@ class ServiceNotification(private val service: BaseService.Interface, profileNam ...@@ -86,11 +86,11 @@ class ServiceNotification(private val service: BaseService.Interface, profileNam
service.getString(R.string.stop), PendingIntent.getBroadcast(service, 0, Intent(Action.CLOSE), 0)) service.getString(R.string.stop), PendingIntent.getBroadcast(service, 0, Intent(Action.CLOSE), 0))
update(if (service.getSystemService<PowerManager>()?.isInteractive != false) update(if (service.getSystemService<PowerManager>()?.isInteractive != false)
Intent.ACTION_SCREEN_ON else Intent.ACTION_SCREEN_OFF, true) Intent.ACTION_SCREEN_ON else Intent.ACTION_SCREEN_OFF, true)
val screenFilter = IntentFilter() service.registerReceiver(lockReceiver, IntentFilter().apply {
screenFilter.addAction(Intent.ACTION_SCREEN_ON) addAction(Intent.ACTION_SCREEN_ON)
screenFilter.addAction(Intent.ACTION_SCREEN_OFF) addAction(Intent.ACTION_SCREEN_OFF)
if (visible && Build.VERSION.SDK_INT < 26) screenFilter.addAction(Intent.ACTION_USER_PRESENT) if (visible && Build.VERSION.SDK_INT < 26) addAction(Intent.ACTION_USER_PRESENT)
service.registerReceiver(lockReceiver, screenFilter) })
} }
private fun update(action: String?, forceShow: Boolean = false) { private fun update(action: String?, forceShow: Boolean = false) {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment