Update labels for the categories to match the UX writer provided strings

Also fix issue that user was not passed when getting application info.
(error noticed by Zak)
http://screen/8fiiTSHvo2SfYdE

Bug: 318410881
Flag: N/A
Test: Unit test
Change-Id: Id85feec0d95d3d4d183a673f7eed40906b0043e2
This commit is contained in:
Shamali P
2024-03-05 16:01:05 +00:00
committed by Shamali Patwa
parent 0f0c1dd30b
commit 20a5f4e8d1
4 changed files with 79 additions and 46 deletions
@@ -15,6 +15,8 @@
*/
package com.android.launcher3.model;
import static android.content.pm.ApplicationInfo.CATEGORY_PRODUCTIVITY;
import static android.content.pm.ApplicationInfo.FLAG_INSTALLED;
import static android.os.Process.myUserHandle;
import static com.android.launcher3.LauncherSettings.Favorites.CONTAINER_WIDGETS_PREDICTION;
@@ -37,6 +39,8 @@ import android.app.prediction.AppTargetId;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProviderInfo;
import android.content.ComponentName;
import android.content.pm.ApplicationInfo;
import android.content.pm.LauncherApps;
import android.os.UserHandle;
import android.platform.test.flag.junit.SetFlagsRule;
import android.text.TextUtils;
@@ -81,6 +85,8 @@ public final class WidgetsPredicationUpdateTaskTest {
private FakeBgDataModelCallback mCallback = new FakeBgDataModelCallback();
private LauncherModelHelper mModelHelper;
private UserHandle mUserHandle;
private LauncherApps mLauncherApps;
@Before
public void setup() throws Exception {
@@ -103,12 +109,18 @@ public final class WidgetsPredicationUpdateTaskTest {
allWidgets = Arrays.asList(mApp1Provider1, mApp1Provider2, mApp2Provider1,
mApp4Provider1, mApp4Provider2, mApp5Provider1);
mLauncherApps = mModelHelper.sandboxContext.spyService(LauncherApps.class);
doAnswer(i -> {
String pkg = i.getArgument(0);
return ApplicationInfoBuilder.newBuilder().setPackageName(pkg).setName(
"App " + pkg).build();
}).when(mModelHelper.sandboxContext.getPackageManager())
.getApplicationInfo(anyString(), anyInt());
ApplicationInfo applicationInfo = ApplicationInfoBuilder.newBuilder()
.setPackageName(pkg)
.setName("App " + pkg)
.build();
applicationInfo.category = CATEGORY_PRODUCTIVITY;
applicationInfo.flags = FLAG_INSTALLED;
return applicationInfo;
}).when(mLauncherApps).getApplicationInfo(anyString(), anyInt(), any());
AppWidgetManager manager = mModelHelper.sandboxContext.spyService(AppWidgetManager.class);
doReturn(allWidgets).when(manager).getInstalledProviders();
doReturn(allWidgets).when(manager).getInstalledProvidersForProfile(eq(myUserHandle()));
+7 -5
View File
@@ -75,12 +75,14 @@
<!-- Widget suggestions header title in the full widgets picker for large screen devices
in landscape mode. [CHAR_LIMIT=50] -->
<string name="suggested_widgets_header_title">Suggestions</string>
<string name="productivity_widget_recommendation_category_label">Your Daily Essentials</string>
<string name="news_widget_recommendation_category_label">News For You</string>
<string name="productivity_widget_recommendation_category_label">Essentials</string>
<string name="news_widget_recommendation_category_label">News &amp; magazines</string>
<string name="social_and_entertainment_widget_recommendation_category_label">Your Chill Zone</string>
<string name="fitness_widget_recommendation_category_label">Reach Your Fitness Goals</string>
<string name="weather_widget_recommendation_category_label">Stay Ahead of the Weather</string>
<string name="others_widget_recommendation_category_label">You Might Also Like</string>
<string name="entertainment_widget_recommendation_category_label">Entertainment</string>
<string name="social_widget_recommendation_category_label">Social</string>
<string name="fitness_widget_recommendation_category_label">Health &amp; fitness</string>
<string name="weather_widget_recommendation_category_label">Weather</string>
<string name="others_widget_recommendation_category_label">Suggested for you</string>
<!-- accessibilityPaneTitle for the right pane when showing suggested widgets. -->
<string name="widget_picker_right_pane_accessibility_title"><xliff:g id="selected_header" example="Calendar">%1$s</xliff:g> widgets on right, search and options on left</string>
<!-- Label for showing the number of widgets an app has in the full widgets picker.
@@ -18,13 +18,13 @@ package com.android.launcher3.widget.picker;
import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.util.Log;
import androidx.annotation.Nullable;
import androidx.annotation.WorkerThread;
import com.android.launcher3.R;
import com.android.launcher3.model.WidgetItem;
import com.android.launcher3.util.PackageManagerHelper;
import com.android.launcher3.util.Preconditions;
import com.android.launcher3.util.ResourceBasedOverride;
@@ -54,6 +54,7 @@ public class WidgetRecommendationCategoryProvider implements ResourceBasedOverri
* to display the recommendation grouped by categories.
*/
@WorkerThread
@Nullable
public WidgetRecommendationCategory getWidgetRecommendationCategory(Context context,
WidgetItem item) {
// This is a default implementation that uses application category to derive the category to
@@ -61,17 +62,16 @@ public class WidgetRecommendationCategoryProvider implements ResourceBasedOverri
// via the overridden WidgetRecommendationCategoryProvider resource.
Preconditions.assertWorkerThread();
PackageManager pm = context.getPackageManager();
PackageManagerHelper pmHelper = new PackageManagerHelper(context);
if (item.widgetInfo != null && item.widgetInfo.getComponent() != null) {
String widgetComponentName = item.widgetInfo.getComponent().getClassName();
try {
int predictionCategory = pm.getApplicationInfo(
item.widgetInfo.getComponent().getPackageName(), 0 /* flags */).category;
ApplicationInfo applicationInfo = pmHelper.getApplicationInfo(
item.widgetInfo.getComponent().getPackageName(), item.widgetInfo.getUser(),
0 /* flags */);
if (applicationInfo != null) {
int predictionCategory = applicationInfo.category;
return getCategoryFromApplicationCategory(context, predictionCategory,
widgetComponentName);
} catch (PackageManager.NameNotFoundException e) {
Log.e(TAG, "Failed to retrieve application category when determining the "
+ "widget category for " + widgetComponentName, e);
}
}
return null;
@@ -112,17 +112,22 @@ public class WidgetRecommendationCategoryProvider implements ResourceBasedOverri
R.string.news_widget_recommendation_category_label, /*order=*/1);
}
if (applicationCategory == ApplicationInfo.CATEGORY_SOCIAL
|| applicationCategory == ApplicationInfo.CATEGORY_AUDIO
if (applicationCategory == ApplicationInfo.CATEGORY_SOCIAL) {
return new WidgetRecommendationCategory(
R.string.social_widget_recommendation_category_label,
/*order=*/5);
}
if (applicationCategory == ApplicationInfo.CATEGORY_AUDIO
|| applicationCategory == ApplicationInfo.CATEGORY_VIDEO
|| applicationCategory == ApplicationInfo.CATEGORY_IMAGE) {
return new WidgetRecommendationCategory(
R.string.social_and_entertainment_widget_recommendation_category_label,
/*order=*/4);
R.string.entertainment_widget_recommendation_category_label,
/*order=*/6);
}
return new WidgetRecommendationCategory(
R.string.others_widget_recommendation_category_label, /*order=*/5);
R.string.others_widget_recommendation_category_label, /*order=*/4);
}
}
@@ -23,6 +23,7 @@ import static android.content.pm.ApplicationInfo.CATEGORY_PRODUCTIVITY;
import static android.content.pm.ApplicationInfo.CATEGORY_SOCIAL;
import static android.content.pm.ApplicationInfo.CATEGORY_UNDEFINED;
import static android.content.pm.ApplicationInfo.CATEGORY_VIDEO;
import static android.content.pm.ApplicationInfo.FLAG_INSTALLED;
import static androidx.test.core.app.ApplicationProvider.getApplicationContext;
import static androidx.test.platform.app.InstrumentationRegistry.getInstrumentation;
@@ -30,8 +31,7 @@ import static androidx.test.platform.app.InstrumentationRegistry.getInstrumentat
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.when;
@@ -41,7 +41,7 @@ import android.content.ComponentName;
import android.content.Context;
import android.content.ContextWrapper;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.content.pm.LauncherApps;
import android.os.Process;
import androidx.test.core.content.pm.ApplicationInfoBuilder;
@@ -70,10 +70,25 @@ import java.util.Map;
public class WidgetRecommendationCategoryProviderTest {
private static final String TEST_PACKAGE = "com.foo.test";
private static final String TEST_APP_NAME = "foo";
public static final WidgetRecommendationCategory SOCIAL_AND_ENTERTAINMENT_CATEGORY =
private static final WidgetRecommendationCategory PRODUCTIVITY =
new WidgetRecommendationCategory(
R.string.social_and_entertainment_widget_recommendation_category_label,
/*order=*/4);
R.string.productivity_widget_recommendation_category_label,
/*order=*/0);
private static final WidgetRecommendationCategory NEWS =
new WidgetRecommendationCategory(
R.string.news_widget_recommendation_category_label, /*order=*/1);
private static final WidgetRecommendationCategory SUGGESTED_FOR_YOU =
new WidgetRecommendationCategory(
R.string.others_widget_recommendation_category_label, /*order=*/4);
private static final WidgetRecommendationCategory SOCIAL =
new WidgetRecommendationCategory(
R.string.social_widget_recommendation_category_label,
/*order=*/5);
private static final WidgetRecommendationCategory ENTERTAINMENT =
new WidgetRecommendationCategory(
R.string.entertainment_widget_recommendation_category_label,
/*order=*/6);
private final ApplicationInfo mTestAppInfo = ApplicationInfoBuilder.newBuilder().setPackageName(
TEST_PACKAGE).setName(TEST_APP_NAME).build();
private Context mContext;
@@ -82,7 +97,7 @@ public class WidgetRecommendationCategoryProviderTest {
private WidgetItem mTestWidgetItem;
@Mock
private PackageManager mPackageManager;
private LauncherApps mLauncherApps;
private InvariantDeviceProfile mTestProfile;
@Before
@@ -90,10 +105,12 @@ public class WidgetRecommendationCategoryProviderTest {
MockitoAnnotations.initMocks(this);
mContext = new ContextWrapper(getInstrumentation().getTargetContext()) {
@Override
public PackageManager getPackageManager() {
return mPackageManager;
public Object getSystemService(String name) {
return LAUNCHER_APPS_SERVICE.equals(name) ? mLauncherApps : super.getSystemService(
name);
}
};
mTestAppInfo.flags = FLAG_INSTALLED;
mTestProfile = new InvariantDeviceProfile();
mTestProfile.numRows = 5;
mTestProfile.numColumns = 5;
@@ -103,25 +120,22 @@ public class WidgetRecommendationCategoryProviderTest {
@Test
public void getWidgetRecommendationCategory_returnsMappedCategory() throws Exception {
ImmutableMap<Integer, WidgetRecommendationCategory> testCategories = ImmutableMap.of(
CATEGORY_PRODUCTIVITY, new WidgetRecommendationCategory(
R.string.productivity_widget_recommendation_category_label,
/*order=*/
0),
CATEGORY_NEWS, new WidgetRecommendationCategory(
R.string.news_widget_recommendation_category_label, /*order=*/1),
CATEGORY_SOCIAL, SOCIAL_AND_ENTERTAINMENT_CATEGORY,
CATEGORY_AUDIO, SOCIAL_AND_ENTERTAINMENT_CATEGORY,
CATEGORY_IMAGE, SOCIAL_AND_ENTERTAINMENT_CATEGORY,
CATEGORY_VIDEO, SOCIAL_AND_ENTERTAINMENT_CATEGORY,
CATEGORY_UNDEFINED, new WidgetRecommendationCategory(
R.string.others_widget_recommendation_category_label, /*order=*/5));
CATEGORY_PRODUCTIVITY, PRODUCTIVITY,
CATEGORY_NEWS, NEWS,
CATEGORY_SOCIAL, SOCIAL,
CATEGORY_AUDIO, ENTERTAINMENT,
CATEGORY_IMAGE, ENTERTAINMENT,
CATEGORY_VIDEO, ENTERTAINMENT,
CATEGORY_UNDEFINED, SUGGESTED_FOR_YOU);
for (Map.Entry<Integer, WidgetRecommendationCategory> testCategory :
testCategories.entrySet()) {
mTestAppInfo.category = testCategory.getKey();
when(mPackageManager.getApplicationInfo(anyString(), anyInt())).thenReturn(
mTestAppInfo);
when(mLauncherApps.getApplicationInfo(/*packageName=*/ eq(TEST_PACKAGE),
/*flags=*/ eq(0),
/*user=*/ eq(Process.myUserHandle())))
.thenReturn(mTestAppInfo);
WidgetRecommendationCategory category = Executors.MODEL_EXECUTOR.submit(() ->
new WidgetRecommendationCategoryProvider().getWidgetRecommendationCategory(