Fix receiver leaking
This commit is contained in:
@@ -174,12 +174,16 @@ class App : Application() {
|
||||
}
|
||||
}
|
||||
|
||||
fun listenForPackageChanges(callback: () -> Unit): BroadcastReceiver {
|
||||
fun listenForPackageChanges(onetime: Boolean = true, callback: () -> Unit): BroadcastReceiver {
|
||||
val filter = IntentFilter(Intent.ACTION_PACKAGE_ADDED)
|
||||
filter.addAction(Intent.ACTION_PACKAGE_REMOVED)
|
||||
filter.addDataScheme("package")
|
||||
val result = broadcastReceiver { _, intent ->
|
||||
if (!intent.getBooleanExtra(Intent.EXTRA_REPLACING, false)) callback()
|
||||
val result = object : BroadcastReceiver() {
|
||||
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
|
||||
|
||||
@@ -63,11 +63,14 @@ class AppManager : AppCompatActivity(), Toolbar.OnMenuItemClickListener {
|
||||
private var receiver: BroadcastReceiver? = null
|
||||
private var cachedApps: List<PackageInfo>? = null
|
||||
private fun getApps(pm: PackageManager): List<ProxiedApp> {
|
||||
if (receiver == null) receiver = app.listenForPackageChanges {
|
||||
synchronized(AppManager) { cachedApps = null }
|
||||
AppManager.instance?.reloadApps()
|
||||
}
|
||||
return synchronized(AppManager) {
|
||||
if (receiver == null) receiver = app.listenForPackageChanges {
|
||||
synchronized(AppManager) {
|
||||
receiver = null
|
||||
cachedApps = null
|
||||
}
|
||||
AppManager.instance?.reloadApps()
|
||||
}
|
||||
// Labels and icons can change on configuration (locale, etc.) changes, therefore they are not cached.
|
||||
val cachedApps = cachedApps ?: pm.getInstalledPackages(PackageManager.GET_PERMISSIONS)
|
||||
.filter { it.packageName != app.packageName &&
|
||||
|
||||
@@ -21,6 +21,7 @@
|
||||
package com.github.shadowsocks
|
||||
|
||||
import android.app.Activity
|
||||
import android.content.BroadcastReceiver
|
||||
import android.content.Intent
|
||||
import android.os.Build
|
||||
import android.os.Bundle
|
||||
@@ -60,6 +61,7 @@ class ProfileConfigFragment : PreferenceFragmentCompatDividers(), Toolbar.OnMenu
|
||||
private lateinit var plugin: IconListPreference
|
||||
private lateinit var pluginConfigure: EditTextPreference
|
||||
private lateinit var pluginConfiguration: PluginConfiguration
|
||||
private lateinit var receiver: BroadcastReceiver
|
||||
|
||||
override fun onCreatePreferencesFix(savedInstanceState: Bundle?, rootKey: String?) {
|
||||
preferenceManager.preferenceDataStore = DataStore.privateStore
|
||||
@@ -102,7 +104,7 @@ class ProfileConfigFragment : PreferenceFragmentCompatDividers(), Toolbar.OnMenu
|
||||
}
|
||||
pluginConfigure.onPreferenceChangeListener = this
|
||||
initPlugins()
|
||||
app.listenForPackageChanges { initPlugins() }
|
||||
receiver = app.listenForPackageChanges(false) { initPlugins() }
|
||||
DataStore.privateStore.registerChangeListener(this)
|
||||
}
|
||||
|
||||
@@ -202,6 +204,7 @@ class ProfileConfigFragment : PreferenceFragmentCompatDividers(), Toolbar.OnMenu
|
||||
|
||||
override fun onDestroy() {
|
||||
DataStore.privateStore.unregisterChangeListener(this)
|
||||
app.unregisterReceiver(receiver)
|
||||
super.onDestroy()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -21,6 +21,7 @@
|
||||
package com.github.shadowsocks.plugin
|
||||
|
||||
import android.annotation.SuppressLint
|
||||
import android.content.BroadcastReceiver
|
||||
import android.content.ContentResolver
|
||||
import android.content.Intent
|
||||
import android.content.pm.PackageManager
|
||||
@@ -84,11 +85,16 @@ object PluginManager {
|
||||
""", Base64.DEFAULT))
|
||||
}
|
||||
|
||||
private val receiver by lazy { app.listenForPackageChanges { synchronized(this) { cachedPlugins = null } } }
|
||||
private var receiver: BroadcastReceiver? = null
|
||||
private var cachedPlugins: Map<String, Plugin>? = null
|
||||
fun fetchPlugins(): Map<String, Plugin> {
|
||||
receiver
|
||||
return synchronized(this) {
|
||||
if (receiver == null) receiver = app.listenForPackageChanges {
|
||||
synchronized(this) {
|
||||
receiver = null
|
||||
cachedPlugins = null
|
||||
}
|
||||
}
|
||||
if (cachedPlugins == null) {
|
||||
val pm = app.packageManager
|
||||
cachedPlugins = (pm.queryIntentContentProviders(Intent(PluginContract.ACTION_NATIVE_PLUGIN),
|
||||
|
||||
Reference in New Issue
Block a user