Creating a dumpmanager class to allow dumping state from singleton objects

Bug: 410927105
Flag: EXEMPT refactor
Test: atest DumpManagerTest
Change-Id: Idcd1c74b1edf7a79ec8e4fa91676e617afa04907
This commit is contained in:
Sunny Goyal committed 2025-04-17 11:05:20 -07:00
1 parent 612b58b344
commit 54e5ed0d36
11 files changed
+156 -33

No files matched your search

@@ -36,6 +36,7 @@ import org.mockito.Mockito.never
import org.mockito.Mockito.verify
import org.mockito.Mockito.verifyNoMoreInteractions
import org.mockito.MockitoAnnotations
import org.mockito.kotlin.mock
/** Unit tests for [QuickstepModelDelegate]. */
@RunWith(AndroidJUnit4::class)
@@ -65,7 +66,13 @@ class QuickstepModelDelegateTest {
underTest.mHotseatState.predictor = hotseatPredictor
underTest.mWidgetsRecommendationState.predictor = widgetRecommendationPredictor
underTest.mModel = LauncherAppState.getInstance(context).model
underTest.mDataModel = BgDataModel(WidgetsModel(context), { null })
underTest.mDataModel =
BgDataModel(
WidgetsModel(context),
/* homeDataProvider */ { null },
/* dumpManager */ mock(),
/* DaggerSingletonTracker */ mock(),
)
}
@Test
+2 -3
View File
@@ -181,6 +181,7 @@ import com.android.launcher3.celllayout.CellPosMapper.CellPos;
import com.android.launcher3.celllayout.CellPosMapper.TwoPanelCellPosMapper;
import com.android.launcher3.compat.AccessibilityManagerCompat;
import com.android.launcher3.config.FeatureFlags;
import com.android.launcher3.dagger.LauncherComponentProvider;
import com.android.launcher3.debug.TestEventEmitter;
import com.android.launcher3.debug.TestEventEmitter.TestEvent;
import com.android.launcher3.dragndrop.DragLayer;
@@ -235,7 +236,6 @@ import com.android.launcher3.util.ItemInflater;
import com.android.launcher3.util.KeyboardShortcutsDelegate;
import com.android.launcher3.util.LauncherBindableItemsContainer;
import com.android.launcher3.util.LockedUserState;
import com.android.launcher3.util.MSDLPlayerWrapper;
import com.android.launcher3.util.PackageUserKey;
import com.android.launcher3.util.PendingRequestArgs;
import com.android.launcher3.util.PluginManagerWrapper;
@@ -2630,10 +2630,9 @@ public class Launcher extends StatefulActivity<LauncherState>
// Ignore
}
mModel.dumpState(prefix, fd, writer, args);
mOverlayManager.dump(prefix, writer);
ACTIVITY_TRACKER.dump(prefix, writer);
MSDLPlayerWrapper.INSTANCE.get(getApplicationContext()).dump(prefix, writer);
LauncherComponentProvider.get(this).getDumpManager().dump(prefix, writer, args);
}
/**
+7 -7
View File
@@ -19,12 +19,13 @@ import android.content.Context
import android.content.Intent
import android.content.pm.ShortcutInfo
import android.os.UserHandle
import android.text.TextUtils
import androidx.annotation.WorkerThread
import com.android.launcher3.celllayout.CellPosMapper
import com.android.launcher3.dagger.ApplicationContext
import com.android.launcher3.dagger.LauncherAppSingleton
import com.android.launcher3.icons.IconCache
import com.android.launcher3.logging.DumpManager
import com.android.launcher3.logging.DumpManager.LauncherDumpable
import com.android.launcher3.model.AllAppsList
import com.android.launcher3.model.BaseLauncherBinder.BaseLauncherBinderFactory
import com.android.launcher3.model.BgDataModel
@@ -51,7 +52,6 @@ import com.android.launcher3.util.Executors.MAIN_EXECUTOR
import com.android.launcher3.util.Executors.MODEL_EXECUTOR
import com.android.launcher3.util.PackageUserKey
import com.android.launcher3.util.Preconditions
import java.io.FileDescriptor
import java.io.PrintWriter
import java.util.concurrent.CancellationException
import java.util.function.Consumer
@@ -82,7 +82,8 @@ constructor(
private val loaderFactory: LoaderTaskFactory,
private val binderFactory: BaseLauncherBinderFactory,
val modelDbController: ModelDbController,
) {
dumpManager: DumpManager,
) : LauncherDumpable {
private val mCallbacksList = ArrayList<BgDataModel.Callbacks>(1)
@@ -124,6 +125,7 @@ constructor(
}
lifecycle.addCloseable { destroy() }
modelDelegate.init(this, mBgAllAppsList, mBgDataModel)
lifecycle.addCloseable(dumpManager.register(this))
}
fun newModelCallbacks() = ModelLauncherCallbacks(this::enqueueModelUpdateTask)
@@ -444,8 +446,8 @@ constructor(
}
}
fun dumpState(prefix: String?, fd: FileDescriptor?, writer: PrintWriter, args: Array<String?>) {
if (args.isNotEmpty() && TextUtils.equals(args[0], "--all")) {
override fun dump(prefix: String, writer: PrintWriter, args: Array<String>?) {
if (args?.getOrNull(0) == "--all") {
writer.println(prefix + "All apps list: size=" + mBgAllAppsList.data.size)
for (info in mBgAllAppsList.data) {
writer.println(
@@ -454,8 +456,6 @@ constructor(
}
writer.println()
}
modelDelegate.dump(prefix, fd, writer, args)
mBgDataModel.dump(prefix, fd, writer, args)
}
/** Returns true if there are any callbacks attached to the model */
@@ -35,6 +35,7 @@ import android.util.Log;
import android.util.Pair;
import com.android.launcher3.LauncherSettings.Favorites;
import com.android.launcher3.dagger.LauncherComponentProvider;
import com.android.launcher3.model.ModelDbController;
import com.android.launcher3.util.LayoutImportExportHelper;
import com.android.launcher3.widget.LauncherWidgetHolder;
@@ -61,10 +62,7 @@ public class LauncherProvider extends ContentProvider {
*/
@Override
public void dump(FileDescriptor fd, PrintWriter writer, String[] args) {
LauncherModel model = LauncherAppState.INSTANCE.get(getContext()).getModel();
if (model.isModelLoaded()) {
model.dumpState("", fd, writer, args);
}
LauncherComponentProvider.get(getContext()).getDumpManager().dump("", writer, args);
}
@Override
@@ -34,6 +34,7 @@ import com.android.launcher3.pm.UserCache;
import com.android.launcher3.util.ApiWrapper;
import com.android.launcher3.util.DaggerSingletonTracker;
import com.android.launcher3.util.DisplayController;
import com.android.launcher3.logging.DumpManager;
import com.android.launcher3.util.DynamicResource;
import com.android.launcher3.util.LockedUserState;
import com.android.launcher3.util.MSDLPlayerWrapper;
@@ -91,6 +92,8 @@ public interface LauncherBaseAppComponent {
WidgetHolderFactory getWidgetHolderFactory();
RefreshRateTracker getFrameRateProvider();
DumpManager getDumpManager();
/** Builder for LauncherBaseAppComponent. */
interface Builder {
@BindsInstance Builder appContext(@ApplicationContext Context context);
@@ -0,0 +1,49 @@
/*
* Copyright (C) 2025 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.launcher3.logging
import com.android.launcher3.dagger.LauncherAppSingleton
import com.android.launcher3.util.SafeCloseable
import java.io.PrintWriter
import java.util.concurrent.CopyOnWriteArrayList
import javax.inject.Inject
/** Utility class for managing log dump for various objects */
@LauncherAppSingleton
class DumpManager @Inject constructor() {
private val dumpables = CopyOnWriteArrayList<LauncherDumpable>()
/** Registers a [LauncherDumpable], and returns a [SafeCloseable] to unregister it */
fun register(target: LauncherDumpable): SafeCloseable {
dumpables.add(target)
return SafeCloseable { dumpables.remove(target) }
}
/** Dumps all the registered dumpables */
fun dump(prefix: String, writer: PrintWriter, args: Array<String>?) {
dumpables.forEach { it.dump(prefix, writer, args) }
}
/**
* Interface to indicate a dumpable object. Prefixed with Launcher to avoid conflict with
* platform class
*/
fun interface LauncherDumpable {
fun dump(prefix: String, writer: PrintWriter, args: Array<String>?)
}
}
@@ -60,6 +60,9 @@ import com.android.launcher3.shortcuts.ShortcutKey;
import com.android.launcher3.shortcuts.ShortcutRequest;
import com.android.launcher3.shortcuts.ShortcutRequest.QueryResult;
import com.android.launcher3.util.ComponentKey;
import com.android.launcher3.util.DaggerSingletonTracker;
import com.android.launcher3.logging.DumpManager;
import com.android.launcher3.logging.DumpManager.LauncherDumpable;
import com.android.launcher3.util.IntArray;
import com.android.launcher3.util.IntSet;
import com.android.launcher3.util.IntSparseArrayMap;
@@ -68,7 +71,6 @@ import com.android.launcher3.util.PackageUserKey;
import com.android.launcher3.util.RunnableList;
import com.android.launcher3.widget.model.WidgetsListBaseEntry;
import java.io.FileDescriptor;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
@@ -94,7 +96,7 @@ import javax.inject.Provider;
* this object when accessing any data from this model.
*/
@LauncherAppSingleton
public class BgDataModel {
public class BgDataModel implements LauncherDumpable {
private static final String TAG = "BgDataModel";
@@ -138,9 +140,12 @@ public class BgDataModel {
public int lastLoadId = -1;
@Inject
public BgDataModel(WidgetsModel widgetsModel, Provider<HomeScreenRepository> homeDataProvider) {
public BgDataModel(WidgetsModel widgetsModel,
Provider<HomeScreenRepository> homeDataProvider,
DumpManager dumpManager, DaggerSingletonTracker lifeCycle) {
this.widgetsModel = widgetsModel;
mRepo = Flags.modelRepository() ? homeDataProvider.get() : null;
lifeCycle.addCloseable(dumpManager.register(this));
}
/**
@@ -170,8 +175,9 @@ public class BgDataModel {
return screenSet.getArray();
}
public synchronized void dump(String prefix, FileDescriptor fd, PrintWriter writer,
String[] args) {
@Override
public synchronized void dump(@NonNull String prefix, @NonNull PrintWriter writer,
@Nullable String[] args) {
writer.println(prefix + "Data Model:");
writer.println(prefix + " ---- items id map ");
for (int i = 0; i < itemsIdMap.size(); i++) {
@@ -182,7 +188,7 @@ public class BgDataModel {
writer.println(prefix + '\t' + extraItems.valueAt(i).toString());
}
if (args.length > 0 && TextUtils.equals(args[0], "--all")) {
if (args != null && args.length > 0 && TextUtils.equals(args[0], "--all")) {
writer.println(prefix + "shortcut counts ");
for (Integer count : deepShortcutMap.values()) {
writer.print(count + ", ");
@@ -27,8 +27,6 @@ import com.android.launcher3.LauncherModel;
import com.android.launcher3.dagger.ApplicationContext;
import com.android.launcher3.shortcuts.ShortcutKey;
import java.io.FileDescriptor;
import java.io.PrintWriter;
import java.util.Map;
import javax.inject.Inject;
@@ -117,11 +115,4 @@ public class ModelDelegate {
@WorkerThread
public void destroy() { }
/**
* Add data to a dumpsys request for Launcher (e.g. for bug reports).
*
* @see com.android.launcher3.Launcher#dump(java.lang.String, java.io.FileDescriptor,
* java.io.PrintWriter, java.lang.String[])
**/
public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) { }
}
@@ -19,9 +19,12 @@ package com.android.launcher3.util;
import android.content.Context;
import android.os.Vibrator;
import androidx.annotation.Nullable;
import com.android.launcher3.dagger.ApplicationContext;
import com.android.launcher3.dagger.LauncherAppSingleton;
import com.android.launcher3.dagger.LauncherBaseAppComponent;
import com.android.launcher3.logging.DumpManager;
import com.google.android.msdl.data.model.MSDLToken;
import com.google.android.msdl.domain.InteractionProperties;
@@ -46,11 +49,13 @@ public class MSDLPlayerWrapper {
private final MSDLPlayer mMSDLPlayer;
@Inject
public MSDLPlayerWrapper(@ApplicationContext Context context) {
public MSDLPlayerWrapper(@ApplicationContext Context context,
DumpManager dumpManager, DaggerSingletonTracker lifeCycle) {
Vibrator vibrator = context.getSystemService(Vibrator.class);
mMSDLPlayer = MSDLPlayer.Companion.createPlayer(vibrator,
java.util.concurrent.Executors.newSingleThreadExecutor(),
null /* useHapticFeedbackForToken */);
lifeCycle.addCloseable(dumpManager.register(this::dump));
}
/** Perform MSDL feedback for a token with interaction properties */
@@ -68,7 +73,7 @@ public class MSDLPlayerWrapper {
}
/** Print the latest history of MSDL tokens played */
public void dump(String prefix, PrintWriter writer) {
private void dump(String prefix, PrintWriter writer, @Nullable String[] args) {
writer.println(prefix + mMSDLPlayer.toString());
writer.println(prefix + "MSDLPlayerWrapper history of latest events:");
List<MSDLEvent> events = getHistory();
@@ -0,0 +1,65 @@
/*
* Copyright (C) 2025 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.launcher3.logging
import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.filters.SmallTest
import com.android.launcher3.logging.DumpManager.LauncherDumpable
import java.io.PrintWriter
import java.io.StringWriter
import org.junit.Test
import org.junit.runner.RunWith
import org.mockito.kotlin.any
import org.mockito.kotlin.anyOrNull
import org.mockito.kotlin.eq
import org.mockito.kotlin.mock
import org.mockito.kotlin.never
import org.mockito.kotlin.reset
import org.mockito.kotlin.times
import org.mockito.kotlin.verify
@SmallTest
@RunWith(AndroidJUnit4::class)
class DumpManagerTest {
private val dumpManager = DumpManager()
@Test
fun `dump called on dumpable`() {
val dumpable1 = mock<LauncherDumpable>()
val dumpable2 = mock<LauncherDumpable>()
dumpManager.register(dumpable1)
dumpManager.register(dumpable2)
dumpManager.dump("prefix", PrintWriter(StringWriter()), null)
verify(dumpable1, times(1)).dump(eq("prefix"), any(), anyOrNull())
verify(dumpable2, times(1)).dump(eq("prefix"), any(), anyOrNull())
}
@Test
fun `unregister doesnot call dump`() {
val dumpable = mock<LauncherDumpable>()
val closeAction = dumpManager.register(dumpable)
dumpManager.dump("prefix", PrintWriter(StringWriter()), null)
verify(dumpable, times(1)).dump(anyOrNull(), anyOrNull(), anyOrNull())
reset(dumpable)
closeAction.close()
dumpManager.dump("prefix", PrintWriter(StringWriter()), null)
verify(dumpable, never()).dump(anyOrNull(), anyOrNull(), anyOrNull())
}
}
@@ -71,7 +71,7 @@ class ShortcutsChangedTaskTest {
private val mockTaskController: ModelTaskController = mock()
private val mockAllApps: AllAppsList = mock()
private val mockIconCache: IconCache = mock()
private val bgDataModel = BgDataModel(mock(), mock())
private val bgDataModel = BgDataModel(mock(), mock(), mock(), mock())
private val expectedWai =
WorkspaceItemInfo().apply {