From 54bd36ef81f507ff9c2635d4a7b35786337baa4b Mon Sep 17 00:00:00 2001 From: Shamali P Date: Thu, 18 Jul 2024 14:22:12 +0000 Subject: [PATCH] Move out picker related methods from widgets model * The WidgetsBaseEntry and related types are specific to picker UI. So, moved them to a entry builder class. Bug: 353347512 Flag: EXEMPT BUGFIX Test: Unit test Change-Id: I42b3083b42ee03dc8d548e7464689ea270a36f22 --- .../launcher3/WidgetPickerActivity.java | 47 +++-- .../launcher3/model/BaseLauncherBinder.java | 5 +- .../launcher3/model/ModelTaskController.kt | 5 +- .../android/launcher3/model/WidgetsModel.java | 52 +---- .../model/WidgetsListBaseEntriesBuilder.kt | 51 +++++ .../WidgetsListBaseEntriesBuilderTest.kt | 178 ++++++++++++++++++ 6 files changed, 262 insertions(+), 76 deletions(-) create mode 100644 src/com/android/launcher3/widget/model/WidgetsListBaseEntriesBuilder.kt create mode 100644 tests/multivalentTests/src/com/android/launcher3/widget/model/WidgetsListBaseEntriesBuilderTest.kt diff --git a/quickstep/src/com/android/launcher3/WidgetPickerActivity.java b/quickstep/src/com/android/launcher3/WidgetPickerActivity.java index d0be2f3cd1..0b1863398e 100644 --- a/quickstep/src/com/android/launcher3/WidgetPickerActivity.java +++ b/quickstep/src/com/android/launcher3/WidgetPickerActivity.java @@ -47,11 +47,11 @@ import com.android.launcher3.model.WidgetItem; import com.android.launcher3.model.WidgetPredictionsRequester; import com.android.launcher3.model.WidgetsModel; import com.android.launcher3.model.data.ItemInfo; +import com.android.launcher3.model.data.PackageItemInfo; import com.android.launcher3.popup.PopupDataProvider; -import com.android.launcher3.util.ComponentKey; import com.android.launcher3.widget.WidgetCell; +import com.android.launcher3.widget.model.WidgetsListBaseEntriesBuilder; import com.android.launcher3.widget.model.WidgetsListBaseEntry; -import com.android.launcher3.widget.model.WidgetsListContentEntry; import com.android.launcher3.widget.picker.WidgetsFullSheet; import java.util.ArrayList; @@ -60,10 +60,8 @@ import java.util.List; import java.util.Locale; import java.util.Map; import java.util.Set; -import java.util.function.Function; import java.util.function.Predicate; import java.util.regex.Pattern; -import java.util.stream.Collectors; /** An Activity that can host Launcher's widget picker. */ public class WidgetPickerActivity extends BaseActivity { @@ -112,7 +110,8 @@ public class WidgetPickerActivity extends BaseActivity { private WidgetsModel mModel; private LauncherAppState mApp; private WidgetPredictionsRequester mWidgetPredictionsRequester; - private final PopupDataProvider mPopupDataProvider = new PopupDataProvider(i -> {}); + private final PopupDataProvider mPopupDataProvider = new PopupDataProvider(i -> { + }); private int mDesiredWidgetWidth; private int mDesiredWidgetHeight; @@ -287,45 +286,41 @@ public class WidgetPickerActivity extends BaseActivity { }; } - /** Updates the model with widgets, applies filters and launches the widgets sheet once - * widgets are available */ + /** + * Updates the model with widgets, applies filters and launches the widgets sheet once + * widgets are available + */ private void refreshAndBindWidgets() { MODEL_EXECUTOR.execute(() -> { LauncherAppState app = LauncherAppState.getInstance(this); Context context = app.getContext(); mModel.update(app, null); - final List allWidgets = - mModel.getFilteredWidgetsListForPicker(context, mWidgetsFilter); - final List defaultWidgets = - shouldShowDefaultWidgets() ? mModel.getFilteredWidgetsListForPicker(context, - mDefaultWidgetsFilter) : List.of(); - bindWidgets(allWidgets, defaultWidgets); + bindWidgets(mModel.getWidgetsByPackageItem()); // Open sheet once widgets are available, so that it doesn't interrupt the open // animation. openWidgetsSheet(); if (mUiSurface != null) { - Map allWidgetItems = allWidgets.stream() - .filter(entry -> entry instanceof WidgetsListContentEntry) - .flatMap(entry -> entry.mWidgets.stream()) - .distinct() - .collect(Collectors.toMap( - widget -> new ComponentKey(widget.componentName, widget.user), - Function.identity() - )); mWidgetPredictionsRequester = new WidgetPredictionsRequester(app.getContext(), - mUiSurface, allWidgetItems); + mUiSurface, mModel.getWidgetsByComponentKey()); mWidgetPredictionsRequester.request(mAddedWidgets, this::bindRecommendedWidgets); } }); } - private void bindWidgets(List allWidgets, - List defaultWidgets) { + private void bindWidgets(Map> widgets) { + WidgetsListBaseEntriesBuilder builder = new WidgetsListBaseEntriesBuilder( + mApp.getContext()); + + final List allWidgets = builder.build(widgets, mWidgetsFilter); + final List defaultWidgets = + shouldShowDefaultWidgets() ? builder.build(widgets, + mDefaultWidgetsFilter) : List.of(); + MAIN_EXECUTOR.execute(() -> mPopupDataProvider.setAllWidgets(allWidgets, defaultWidgets)); } - private void openWidgetsSheet() { + private void openWidgetsSheet() { MAIN_EXECUTOR.execute(() -> { mWidgetSheet = WidgetsFullSheet.show(this, true); mWidgetSheet.mayUpdateTitleAndDescription(mTitle, mDescription); @@ -392,7 +387,7 @@ public class WidgetPickerActivity extends BaseActivity { mActiveOnBackAnimationCallback.onBackCancelled(); mActiveOnBackAnimationCallback = null; } - }; + } private boolean shouldShowDefaultWidgets() { // If optional filters such as size filter are present, we display them as default widgets. diff --git a/src/com/android/launcher3/model/BaseLauncherBinder.java b/src/com/android/launcher3/model/BaseLauncherBinder.java index e6ade610f7..5faa2b83c3 100644 --- a/src/com/android/launcher3/model/BaseLauncherBinder.java +++ b/src/com/android/launcher3/model/BaseLauncherBinder.java @@ -52,6 +52,7 @@ import com.android.launcher3.util.LooperExecutor; import com.android.launcher3.util.LooperIdleLock; import com.android.launcher3.util.PackageUserKey; import com.android.launcher3.util.RunnableList; +import com.android.launcher3.widget.model.WidgetsListBaseEntriesBuilder; import com.android.launcher3.widget.model.WidgetsListBaseEntry; import java.util.ArrayList; @@ -195,8 +196,8 @@ public class BaseLauncherBinder { if (!WIDGETS_ENABLED) { return; } - final List widgets = - mBgDataModel.widgetsModel.getWidgetsListForPicker(mApp.getContext()); + List widgets = new WidgetsListBaseEntriesBuilder(mApp.getContext()) + .build(mBgDataModel.widgetsModel.getWidgetsByPackageItem()); executeCallbacksTask(c -> c.bindAllWidgets(widgets), mUiExecutor); } diff --git a/src/com/android/launcher3/model/ModelTaskController.kt b/src/com/android/launcher3/model/ModelTaskController.kt index 266ed0c67a..cf2cadc094 100644 --- a/src/com/android/launcher3/model/ModelTaskController.kt +++ b/src/com/android/launcher3/model/ModelTaskController.kt @@ -24,6 +24,7 @@ import com.android.launcher3.model.BgDataModel.FixedContainerItems import com.android.launcher3.model.data.ItemInfo import com.android.launcher3.model.data.WorkspaceItemInfo import com.android.launcher3.util.PackageUserKey +import com.android.launcher3.widget.model.WidgetsListBaseEntriesBuilder import java.util.Objects import java.util.concurrent.Executor import java.util.function.Predicate @@ -78,7 +79,9 @@ class ModelTaskController( } fun bindUpdatedWidgets(dataModel: BgDataModel) { - val widgets = dataModel.widgetsModel.getWidgetsListForPicker(app.context) + val widgets = + WidgetsListBaseEntriesBuilder(app.context) + .build(dataModel.widgetsModel.widgetsByPackageItem) scheduleCallbackTask { it.bindAllWidgets(widgets) } } diff --git a/src/com/android/launcher3/model/WidgetsModel.java b/src/com/android/launcher3/model/WidgetsModel.java index 58ebf0fa13..35064cf094 100644 --- a/src/com/android/launcher3/model/WidgetsModel.java +++ b/src/com/android/launcher3/model/WidgetsModel.java @@ -26,7 +26,6 @@ import com.android.launcher3.AppFilter; import com.android.launcher3.InvariantDeviceProfile; import com.android.launcher3.LauncherAppState; import com.android.launcher3.Utilities; -import com.android.launcher3.compat.AlphabeticIndexCompat; import com.android.launcher3.config.FeatureFlags; import com.android.launcher3.icons.ComponentWithLabelAndIcon; import com.android.launcher3.icons.IconCache; @@ -39,9 +38,6 @@ import com.android.launcher3.util.Preconditions; import com.android.launcher3.widget.LauncherAppWidgetProviderInfo; import com.android.launcher3.widget.WidgetManagerHelper; import com.android.launcher3.widget.WidgetSections; -import com.android.launcher3.widget.model.WidgetsListBaseEntry; -import com.android.launcher3.widget.model.WidgetsListContentEntry; -import com.android.launcher3.widget.model.WidgetsListHeaderEntry; import com.android.wm.shell.Flags; import java.util.ArrayList; @@ -75,6 +71,9 @@ public class WidgetsModel { * Returns all widgets keyed by their component key. */ public synchronized Map getWidgetsByComponentKey() { + if (!WIDGETS_ENABLED) { + return Collections.emptyMap(); + } return mWidgetsByPackageItem.values().stream() .flatMap(Collection::stream).distinct() .collect(Collectors.toMap( @@ -87,51 +86,10 @@ public class WidgetsModel { * Returns widgets grouped by the package item that they should belong to. */ public synchronized Map> getWidgetsByPackageItem() { - return mWidgetsByPackageItem; - } - - /** - * Returns a list of {@link WidgetsListBaseEntry} filtered using given widget item filter. All - * {@link WidgetItem}s in a single row are sorted (based on label and user), but the overall - * list of {@link WidgetsListBaseEntry}s is not sorted. - * - * @see com.android.launcher3.widget.picker.WidgetsListAdapter#setWidgets(List) - */ - public synchronized ArrayList getFilteredWidgetsListForPicker( - Context context, - Predicate widgetItemFilter) { if (!WIDGETS_ENABLED) { - return new ArrayList<>(); + return Collections.emptyMap(); } - ArrayList result = new ArrayList<>(); - AlphabeticIndexCompat indexer = new AlphabeticIndexCompat(context); - - for (Map.Entry> entry : - mWidgetsByPackageItem.entrySet()) { - PackageItemInfo pkgItem = entry.getKey(); - List widgetItems = entry.getValue() - .stream() - .filter(widgetItemFilter).toList(); - if (!widgetItems.isEmpty()) { - String sectionName = (pkgItem.title == null) ? "" : - indexer.computeSectionName(pkgItem.title); - result.add(WidgetsListHeaderEntry.create(pkgItem, sectionName, widgetItems)); - result.add(new WidgetsListContentEntry(pkgItem, sectionName, widgetItems)); - } - } - return result; - } - - /** - * Returns a list of {@link WidgetsListBaseEntry}. All {@link WidgetItem} in a single row - * are sorted (based on label and user), but the overall list of - * {@link WidgetsListBaseEntry}s is not sorted. - * - * @see com.android.launcher3.widget.picker.WidgetsListAdapter#setWidgets(List) - */ - public synchronized ArrayList getWidgetsListForPicker(Context context) { - // return all items - return getFilteredWidgetsListForPicker(context, /*widgetItemFilter=*/ item -> true); + return mWidgetsByPackageItem; } /** diff --git a/src/com/android/launcher3/widget/model/WidgetsListBaseEntriesBuilder.kt b/src/com/android/launcher3/widget/model/WidgetsListBaseEntriesBuilder.kt new file mode 100644 index 0000000000..1abe4da98b --- /dev/null +++ b/src/com/android/launcher3/widget/model/WidgetsListBaseEntriesBuilder.kt @@ -0,0 +1,51 @@ +/* + * Copyright (C) 2024 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.widget.model + +import android.content.Context +import com.android.launcher3.compat.AlphabeticIndexCompat +import com.android.launcher3.model.WidgetItem +import com.android.launcher3.model.data.PackageItemInfo +import java.util.function.Predicate + +/** + * A helper class that builds the list of [WidgetsListBaseEntry]s used by the UI to display widgets. + */ +class WidgetsListBaseEntriesBuilder(val context: Context) { + + /** Builds the widgets list entries in a format understandable by the widget picking UI. */ + @JvmOverloads + fun build( + widgetsByPackageItem: Map>, + widgetFilter: Predicate = Predicate { true }, + ): List { + val indexer = AlphabeticIndexCompat(context) + + return buildList { + for ((pkgItem, widgetItems) in widgetsByPackageItem.entries) { + val filteredWidgetItems = widgetItems.filter { widgetFilter.test(it) } + if (filteredWidgetItems.isNotEmpty()) { + // Enables fast scroll popup to show right characters in all locales. + val sectionName = pkgItem.title?.let { indexer.computeSectionName(it) } ?: "" + + add(WidgetsListHeaderEntry.create(pkgItem, sectionName, filteredWidgetItems)) + add(WidgetsListContentEntry(pkgItem, sectionName, filteredWidgetItems)) + } + } + } + } +} diff --git a/tests/multivalentTests/src/com/android/launcher3/widget/model/WidgetsListBaseEntriesBuilderTest.kt b/tests/multivalentTests/src/com/android/launcher3/widget/model/WidgetsListBaseEntriesBuilderTest.kt new file mode 100644 index 0000000000..5df7caa372 --- /dev/null +++ b/tests/multivalentTests/src/com/android/launcher3/widget/model/WidgetsListBaseEntriesBuilderTest.kt @@ -0,0 +1,178 @@ +/* + * Copyright (C) 2024 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.widget.model + +import android.content.ComponentName +import android.content.Context +import android.os.UserHandle +import android.platform.test.rule.AllowedDevices +import android.platform.test.rule.DeviceProduct +import android.platform.test.rule.LimitDevicesRule +import androidx.test.core.app.ApplicationProvider +import androidx.test.ext.junit.runners.AndroidJUnit4 +import com.android.launcher3.InvariantDeviceProfile +import com.android.launcher3.LauncherAppState +import com.android.launcher3.icons.ComponentWithLabel +import com.android.launcher3.icons.IconCache +import com.android.launcher3.model.WidgetItem +import com.android.launcher3.model.data.PackageItemInfo +import com.android.launcher3.util.ActivityContextWrapper +import com.android.launcher3.util.WidgetUtils +import com.android.launcher3.widget.LauncherAppWidgetProviderInfo +import com.google.common.truth.Truth.assertThat +import org.junit.Before +import org.junit.Rule +import org.junit.Test +import org.junit.runner.RunWith +import org.mockito.Mock +import org.mockito.invocation.InvocationOnMock +import org.mockito.junit.MockitoJUnit +import org.mockito.junit.MockitoRule +import org.mockito.kotlin.any +import org.mockito.kotlin.doAnswer + +@RunWith(AndroidJUnit4::class) +@AllowedDevices(allowed = [DeviceProduct.ROBOLECTRIC]) +class WidgetsListBaseEntriesBuilderTest { + @Rule @JvmField val limitDevicesRule = LimitDevicesRule() + @Rule @JvmField val mockitoRule: MockitoRule = MockitoJUnit.rule() + + @Mock private lateinit var iconCache: IconCache + + private lateinit var userHandle: UserHandle + private lateinit var context: Context + private lateinit var testInvariantProfile: InvariantDeviceProfile + private lateinit var allWidgets: Map> + private lateinit var underTest: WidgetsListBaseEntriesBuilder + + @Before + fun setUp() { + userHandle = UserHandle.CURRENT + context = ActivityContextWrapper(ApplicationProvider.getApplicationContext()) + testInvariantProfile = LauncherAppState.getIDP(context) + + doAnswer { invocation: InvocationOnMock -> + val componentWithLabel = invocation.getArgument(0) as ComponentWithLabel + componentWithLabel.getComponent().shortClassName + } + .`when`(iconCache) + .getTitleNoCache(any()) + underTest = WidgetsListBaseEntriesBuilder(context) + + allWidgets = + mapOf( + // app 1 + packageItemInfoWithTitle(APP_1_PACKAGE_NAME, APP_1_PACKAGE_TITLE) to + listOf( + createWidgetItem(APP_1_PACKAGE_NAME, APP_1_PROVIDER_1_CLASS_NAME), + createWidgetItem(APP_1_PACKAGE_NAME, APP_1_PROVIDER_2_CLASS_NAME) + ), + // app 2 + packageItemInfoWithTitle(APP_2_PACKAGE_NAME, APP_2_PACKAGE_TITLE) to + listOf(createWidgetItem(APP_2_PACKAGE_NAME, APP_2_PROVIDER_1_CLASS_NAME)), + // app 3 + packageItemInfoWithTitle(APP_3_PACKAGE_NAME, APP_3_PACKAGE_TITLE) to + listOf(createWidgetItem(APP_3_PACKAGE_NAME, APP_3_PROVIDER_1_CLASS_NAME)) + ) + } + + @Test + fun widgetsListBaseEntriesBuilder_addsHeaderAndContentEntries_withCorrectSectionName() { + val expectedWidgetsCountBySection = + listOf( + APP_1_EXPECTED_SECTION_NAME to 2, + APP_2_EXPECTED_SECTION_NAME to 1, + APP_3_EXPECTED_SECTION_NAME to 1 + ) + + val entries = underTest.build(allWidgets) + + assertThat(entries).hasSize(6) + val headerEntrySectionAndWidgetSizes = + entries.filterIsInstance().map { + it.mTitleSectionName to it.mWidgets.size + } + val contentEntrySectionAndWidgetSizes = + entries.filterIsInstance().map { + it.mTitleSectionName to it.mWidgets.size + } + assertThat(headerEntrySectionAndWidgetSizes) + .containsExactlyElementsIn(expectedWidgetsCountBySection) + assertThat(contentEntrySectionAndWidgetSizes) + .containsExactlyElementsIn(expectedWidgetsCountBySection) + } + + @Test + fun widgetsListBaseEntriesBuilder_withFilter_addsFilteredHeaderAndContentEntries() { + val allowList = listOf(APP_1_PROVIDER_1_CLASS_NAME, APP_3_PROVIDER_1_CLASS_NAME) + val expectedWidgetsCountBySection = + listOf( + APP_1_EXPECTED_SECTION_NAME to 1, // one widget filtered out + APP_3_EXPECTED_SECTION_NAME to 1 + ) + + val entries = + underTest.build(allWidgets) { w -> allowList.contains(w.componentName.shortClassName) } + + assertThat(entries).hasSize(4) // app 2 filtered out + val headerEntrySectionAndWidgetSizes = + entries.filterIsInstance().map { + it.mTitleSectionName to it.mWidgets.size + } + val contentEntrySectionAndWidgetSizes = + entries.filterIsInstance().map { + it.mTitleSectionName to it.mWidgets.size + } + assertThat(headerEntrySectionAndWidgetSizes) + .containsExactlyElementsIn(expectedWidgetsCountBySection) + assertThat(contentEntrySectionAndWidgetSizes) + .containsExactlyElementsIn(expectedWidgetsCountBySection) + } + + private fun packageItemInfoWithTitle(packageName: String, title: String): PackageItemInfo { + val packageItemInfo = PackageItemInfo(packageName, userHandle) + packageItemInfo.title = title + return packageItemInfo + } + + private fun createWidgetItem(packageName: String, widgetProviderName: String): WidgetItem { + val providerInfo = + WidgetUtils.createAppWidgetProviderInfo( + ComponentName.createRelative(packageName, widgetProviderName) + ) + val widgetInfo = LauncherAppWidgetProviderInfo.fromProviderInfo(context, providerInfo) + return WidgetItem(widgetInfo, testInvariantProfile, iconCache, context) + } + + companion object { + const val APP_1_PACKAGE_NAME = "com.example.app1" + const val APP_1_PACKAGE_TITLE = "App1" + const val APP_1_EXPECTED_SECTION_NAME = "A" // for fast popup + const val APP_1_PROVIDER_1_CLASS_NAME = "app1Provider1" + const val APP_1_PROVIDER_2_CLASS_NAME = "app1Provider2" + + const val APP_2_PACKAGE_NAME = "com.example.app2" + const val APP_2_PACKAGE_TITLE = "SomeApp2" + const val APP_2_EXPECTED_SECTION_NAME = "S" // for fast popup + const val APP_2_PROVIDER_1_CLASS_NAME = "app2Provider1" + + const val APP_3_PACKAGE_NAME = "com.example.app3" + const val APP_3_PACKAGE_TITLE = "OtherApp3" + const val APP_3_EXPECTED_SECTION_NAME = "O" // for fast popup + const val APP_3_PROVIDER_1_CLASS_NAME = "app3Provider1" + } +}