fix: Memory leak reported by LeakCanary (#6752)

Signed-off-by: Pun Butrach <pun.butrach@gmail.com>
This commit is contained in:
Pun Butrach authored and GitHub committed 2026-05-16 19:13:22 +07:00
1 parent 95c0fec90e
commit d512b2a484
6 files changed
+47 -22

No files matched your search

@@ -4,6 +4,7 @@ import android.os.CancellationSignal
import android.view.WindowInsets
import android.view.animation.Interpolator
import androidx.core.view.WindowInsetsCompat
import androidx.lifecycle.lifecycleScope
import app.lawnchair.preferences2.PreferenceManager2
import com.android.app.animation.Interpolators
import com.android.launcher3.LauncherState
@@ -15,17 +16,14 @@ import com.android.launcher3.anim.PendingAnimation
import com.android.launcher3.statemanager.StateManager
import com.android.launcher3.states.StateAnimationConfig
import com.patrykmichalik.opto.core.onEach
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
class SearchBarStateHandler(private val launcher: LawnchairLauncher) : StateManager.StateHandler<LauncherState> {
private val coroutineScope = CoroutineScope(Dispatchers.IO)
private val preferenceManager2 = PreferenceManager2.getInstance(launcher)
private var autoShowKeyboard = false
init {
preferenceManager2.autoShowKeyboardInDrawer.onEach(launchIn = coroutineScope) {
preferenceManager2.autoShowKeyboardInDrawer.onEach(launchIn = launcher.lifecycleScope) {
autoShowKeyboard = it
}
}
@@ -111,7 +111,6 @@ class AllAppsSearchInput(context: Context, attrs: AttributeSet?) :
val wrapper = ViewCompat.requireViewById<View>(this, R.id.search_wrapper)
wrapper.background = bg
setupPadding()
launcher.deviceProfile.inv.addOnChangeListener(this)
bgAlphaAnimator.addUpdateListener { updateBgAlpha() }
hint = ViewCompat.requireViewById(this, R.id.hint)
@@ -324,13 +323,19 @@ class AllAppsSearchInput(context: Context, attrs: AttributeSet?) :
override fun onAttachedToWindow() {
super.onAttachedToWindow()
appsView.appsStore?.addUpdateListener(this)
launcher.deviceProfile.inv.addOnChangeListener(this)
if (::appsView.isInitialized) {
appsView.appsStore?.addUpdateListener(this)
}
input.viewTreeObserver.addOnGlobalLayoutListener(this)
}
override fun onDetachedFromWindow() {
super.onDetachedFromWindow()
appsView.appsStore?.removeUpdateListener(this)
launcher.deviceProfile.inv.removeOnChangeListener(this)
if (::appsView.isInitialized) {
appsView.appsStore?.removeUpdateListener(this)
}
input.viewTreeObserver.removeOnGlobalLayoutListener(this)
}
@@ -3,6 +3,7 @@ package app.lawnchair.allapps
import android.content.Context
import android.util.Log
import androidx.activity.ComponentActivity
import androidx.lifecycle.DefaultLifecycleObserver
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.lifecycleScope
import app.lawnchair.data.folder.model.FolderOrderUtils
@@ -31,7 +32,8 @@ class LawnchairAlphabeticalAppsList<T>(
workProfileManager: WorkProfileManager?,
privateProfileManager: PrivateProfileManager?,
) : AlphabeticalAppsList<T>(context, appsStore, workProfileManager, privateProfileManager),
OnIDPChangeListener
OnIDPChangeListener,
DefaultLifecycleObserver
where T : Context, T : ActivityContext {
private var hiddenApps: Set<String> = setOf()
@@ -48,6 +50,7 @@ class LawnchairAlphabeticalAppsList<T>(
init {
context.launcher.deviceProfile.inv.addOnChangeListener(this)
(context as? LifecycleOwner)?.lifecycle?.addObserver(this)
try {
prefs2.hiddenApps.onEach(launchIn = context.launcher.lifecycleScope) {
hiddenApps = it
@@ -59,6 +62,10 @@ class LawnchairAlphabeticalAppsList<T>(
observeFolders()
}
override fun onDestroy(owner: LifecycleOwner) {
context.launcher.deviceProfile.inv.removeOnChangeListener(this)
}
private fun observeFolders() {
viewModel.foldersLiveData.observe(context as LifecycleOwner) { folders ->
folderList = folders
@@ -100,7 +100,6 @@ class OverlayCallbackImpl(private val mLauncher: LawnchairLauncher) :
override fun onActivityDestroyed() {
mClient.onDestroy()
mClient.mDestroyed = true
}
override fun onOverlayScrollChanged(progress: Float) {
@@ -40,9 +40,6 @@ import com.android.launcher3.qsb.QsbContainerView
import com.android.launcher3.util.Themes
import com.android.launcher3.views.ActivityContext
import com.patrykmichalik.opto.core.firstBlocking
import com.patrykmichalik.opto.core.onEach
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.flow.flatMapLatest
@@ -59,7 +56,6 @@ class LawnQsbLayout(context: Context, attrs: AttributeSet?) : FrameLayout(contex
private lateinit var preferenceManager: PreferenceManager
private lateinit var preferenceManager2: PreferenceManager2
private var searchPendingIntent: PendingIntent? = null
private val coroutineScope = CoroutineScope(Dispatchers.Default)
private var strokeColor: ColorOption? = null
@@ -74,19 +70,19 @@ class LawnQsbLayout(context: Context, attrs: AttributeSet?) : FrameLayout(contex
preferenceManager = PreferenceManager.getInstance(context)
preferenceManager2 = PreferenceManager2.getInstance(context)
preferenceManager2.strokeColorStyle.onEach(launchIn = coroutineScope) {
val attachedScope = viewAttachedScope
preferenceManager2.strokeColorStyle.subscribeBlocking(scope = attachedScope) {
strokeColor = it
setUpBackground()
}
setUpBackground()
clipIconRipples()
val searchProvider = getSearchProvider(context, preferenceManager2)
val isGoogle = searchProvider == Google || searchProvider == GoogleGo || searchProvider == PixelSearch
val supportsLens = searchProvider == Google || searchProvider == PixelSearch
preferenceManager2.themedHotseatQsb.subscribeBlocking(scope = viewAttachedScope) { themed ->
preferenceManager2.themedHotseatQsb.subscribeBlocking(scope = attachedScope) { themed ->
setUpBackground(themed)
val iconRes = if (themed) searchProvider.themedIcon else searchProvider.icon
@@ -129,8 +125,8 @@ class LawnQsbLayout(context: Context, attrs: AttributeSet?) : FrameLayout(contex
subscribeGoogleSearchWidget()
}
preferenceManager.hotseatQsbAlpha.subscribeChanges(this::setUpBackground)
preferenceManager.hotseatQsbStrokeWidth.subscribeChanges(this::setUpBackground)
preferenceManager.hotseatQsbAlpha.subscribeChanges(this, ::setUpBackground)
preferenceManager.hotseatQsbStrokeWidth.subscribeChanges(this, ::setUpBackground)
}
override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
@@ -44,7 +44,7 @@ public class LauncherClient {
};
private int mActivityState = 0;
private int mServiceState = 0;
int mServiceState = 0;
public int mFlags;
public LayoutParams mLayoutParams;
@@ -54,7 +54,7 @@ public class LauncherClient {
public boolean mDestroyed = false;
private Bundle mLayoutBundle;
public class OverlayCallback extends ILauncherOverlayCallback.Stub implements Callback {
public static class OverlayCallback extends ILauncherOverlayCallback.Stub implements Callback {
public LauncherClient mClient;
private final Handler mUIHandler = new Handler(Looper.getMainLooper(), this);
public Window mWindow;
@@ -62,6 +62,13 @@ public class LauncherClient {
public WindowManager mWindowManager;
int mWindowShift;
public void destroy() {
mClient = null;
mWindow = null;
mWindowManager = null;
mUIHandler.removeCallbacksAndMessages(null);
}
@Override
public final void overlayScrollChanged(float f) {
mUIHandler.removeMessages(2);
@@ -222,7 +229,20 @@ public class LauncherClient {
}
public void onDestroy() {
mActivity.unregisterReceiver(googleInstallListener);
mDestroyed = true;
try {
mActivity.unregisterReceiver(googleInstallListener);
} catch (Exception ignored) {
// LC-Ignored
}
if (mOverlayCallback != null) {
mOverlayCallback.destroy();
mOverlayCallback = null;
}
if (mLauncherService.mClient != null && mLauncherService.mClient.get() == this) {
mLauncherService.mClient = null;
}
mBaseService.disconnect();
}
private void connect() {
@@ -391,7 +411,7 @@ public class LauncherClient {
}
}
private void setServiceState(int serviceState) {
void setServiceState(int serviceState) {
if (mServiceState != serviceState) {
mServiceState = serviceState;
mScrollCallback.onServiceStateChanged((serviceState & 1) != 0);