From d512b2a484569d85f04cc7e3a7fa4ed45d774e9f Mon Sep 17 00:00:00 2001 From: Pun Butrach Date: Sat, 16 May 2026 19:13:22 +0700 Subject: [PATCH] fix: Memory leak reported by LeakCanary (#6752) Signed-off-by: Pun Butrach --- .../app/lawnchair/SearchBarStateHandler.kt | 6 ++-- .../lawnchair/allapps/AllAppsSearchInput.kt | 11 ++++++-- .../allapps/LawnchairAlphabeticalAppsList.kt | 9 +++++- .../nexuslauncher/OverlayCallbackImpl.kt | 1 - .../src/app/lawnchair/qsb/LawnQsbLayout.kt | 14 ++++------ .../launcherclient/LauncherClient.java | 28 ++++++++++++++++--- 6 files changed, 47 insertions(+), 22 deletions(-) diff --git a/lawnchair/src/app/lawnchair/SearchBarStateHandler.kt b/lawnchair/src/app/lawnchair/SearchBarStateHandler.kt index 0c36ab5c56..1d6eaee1ae 100644 --- a/lawnchair/src/app/lawnchair/SearchBarStateHandler.kt +++ b/lawnchair/src/app/lawnchair/SearchBarStateHandler.kt @@ -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 { - 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 } } diff --git a/lawnchair/src/app/lawnchair/allapps/AllAppsSearchInput.kt b/lawnchair/src/app/lawnchair/allapps/AllAppsSearchInput.kt index 35a5c2fc84..4b39fe957d 100644 --- a/lawnchair/src/app/lawnchair/allapps/AllAppsSearchInput.kt +++ b/lawnchair/src/app/lawnchair/allapps/AllAppsSearchInput.kt @@ -111,7 +111,6 @@ class AllAppsSearchInput(context: Context, attrs: AttributeSet?) : val wrapper = ViewCompat.requireViewById(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) } diff --git a/lawnchair/src/app/lawnchair/allapps/LawnchairAlphabeticalAppsList.kt b/lawnchair/src/app/lawnchair/allapps/LawnchairAlphabeticalAppsList.kt index d7db44fedb..e8926ef405 100644 --- a/lawnchair/src/app/lawnchair/allapps/LawnchairAlphabeticalAppsList.kt +++ b/lawnchair/src/app/lawnchair/allapps/LawnchairAlphabeticalAppsList.kt @@ -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( workProfileManager: WorkProfileManager?, privateProfileManager: PrivateProfileManager?, ) : AlphabeticalAppsList(context, appsStore, workProfileManager, privateProfileManager), - OnIDPChangeListener + OnIDPChangeListener, + DefaultLifecycleObserver where T : Context, T : ActivityContext { private var hiddenApps: Set = setOf() @@ -48,6 +50,7 @@ class LawnchairAlphabeticalAppsList( 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( observeFolders() } + override fun onDestroy(owner: LifecycleOwner) { + context.launcher.deviceProfile.inv.removeOnChangeListener(this) + } + private fun observeFolders() { viewModel.foldersLiveData.observe(context as LifecycleOwner) { folders -> folderList = folders diff --git a/lawnchair/src/app/lawnchair/nexuslauncher/OverlayCallbackImpl.kt b/lawnchair/src/app/lawnchair/nexuslauncher/OverlayCallbackImpl.kt index 5fb7674b1c..616eb582c6 100644 --- a/lawnchair/src/app/lawnchair/nexuslauncher/OverlayCallbackImpl.kt +++ b/lawnchair/src/app/lawnchair/nexuslauncher/OverlayCallbackImpl.kt @@ -100,7 +100,6 @@ class OverlayCallbackImpl(private val mLauncher: LawnchairLauncher) : override fun onActivityDestroyed() { mClient.onDestroy() - mClient.mDestroyed = true } override fun onOverlayScrollChanged(progress: Float) { diff --git a/lawnchair/src/app/lawnchair/qsb/LawnQsbLayout.kt b/lawnchair/src/app/lawnchair/qsb/LawnQsbLayout.kt index 7ab1fa1699..1cf690d058 100644 --- a/lawnchair/src/app/lawnchair/qsb/LawnQsbLayout.kt +++ b/lawnchair/src/app/lawnchair/qsb/LawnQsbLayout.kt @@ -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) { diff --git a/lawnchair/src/com/google/android/libraries/launcherclient/LauncherClient.java b/lawnchair/src/com/google/android/libraries/launcherclient/LauncherClient.java index 360dbc4caf..902403088b 100644 --- a/lawnchair/src/com/google/android/libraries/launcherclient/LauncherClient.java +++ b/lawnchair/src/com/google/android/libraries/launcherclient/LauncherClient.java @@ -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);