Skip to content

Commit

Permalink
Merge "Add a widget conversations category for grouping widgets in th…
Browse files Browse the repository at this point in the history
…e picker" into sc-dev
  • Loading branch information
stevenckngaa authored and Android (Google) Code Review committed Apr 12, 2021
2 parents ea29adf + 833a0b9 commit 1d8090e
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 5 deletions.
3 changes: 3 additions & 0 deletions res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,9 @@
<!-- Tab label. A user can tap this tab to access their work widgets. [CHAR_LIMIT=25] -->
<string name="widgets_full_sheet_work_tab">Work</string>

<!-- A widget category label for grouping widgets related to conversations. [CHAR_LIMIT=30] -->
<string name="widget_category_conversations">Conversations</string>

<!-- All Apps -->
<!-- Search bar text in the apps view. [CHAR_LIMIT=50] -->
<string name="all_apps_search_bar_hint">Search apps</string>
Expand Down
6 changes: 6 additions & 0 deletions src/com/android/launcher3/icons/IconCache.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@

import com.android.launcher3.InvariantDeviceProfile;
import com.android.launcher3.LauncherFiles;
import com.android.launcher3.R;
import com.android.launcher3.Utilities;
import com.android.launcher3.config.FeatureFlags;
import com.android.launcher3.icons.ComponentWithLabel.ComponentCachingLogic;
Expand Down Expand Up @@ -304,6 +305,11 @@ public synchronized void getTitleAndIconForApp(
CacheEntry entry = getEntryForPackageLocked(
infoInOut.packageName, infoInOut.user, useLowResIcon);
applyCacheEntry(entry, infoInOut);
if (infoInOut.category == PackageItemInfo.CONVERSATIONS) {
infoInOut.title = mContext.getString(R.string.widget_category_conversations);
infoInOut.contentDescription = mPackageManager.getUserBadgedLabel(
infoInOut.title, infoInOut.user);
}
}

protected void applyCacheEntry(CacheEntry entry, ItemInfoWithIcon info) {
Expand Down
22 changes: 21 additions & 1 deletion src/com/android/launcher3/model/data/PackageItemInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,27 +16,47 @@

package com.android.launcher3.model.data;

import androidx.annotation.IntDef;

import com.android.launcher3.LauncherSettings;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.util.Objects;

/**
* Represents a {@link Package} in the widget tray section.
*/
public class PackageItemInfo extends ItemInfoWithIcon {
@Retention(RetentionPolicy.SOURCE)
@IntDef({NO_CATEGORY, CONVERSATIONS})
public @interface Category{}
/** The package is not categorized in the widget tray. */
public static final int NO_CATEGORY = 0;
/** The package is categorized to conversations widget in the widget tray. */
public static final int CONVERSATIONS = 1;

/**
* Package name of the {@link PackageItemInfo}.
*/
public String packageName;
public final String packageName;

/** Represents a widget category shown in the widget tray section. */
@Category public final int category;

public PackageItemInfo(String packageName) {
this(packageName, NO_CATEGORY);
}

public PackageItemInfo(String packageName, @Category int category) {
this.packageName = packageName;
this.category = category;
this.itemType = LauncherSettings.Favorites.ITEM_TYPE_NON_ACTIONABLE;
}

public PackageItemInfo(PackageItemInfo copy) {
this.packageName = copy.packageName;
this.category = copy.category;
this.itemType = LauncherSettings.Favorites.ITEM_TYPE_NON_ACTIONABLE;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import com.android.launcher3.widget.picker.WidgetsDiffReporter;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand All @@ -58,6 +59,9 @@ public class WidgetsModel {
private static final String TAG = "WidgetsModel";
private static final boolean DEBUG = false;

private static final ComponentName CONVERSATION_WIDGET = ComponentName.createRelative(
"com.android.systemui", ".people.widget.PeopleSpaceWidgetProvider");

/* Map of widgets and shortcuts that are tracked per package. */
private final Map<PackageItemInfo, List<WidgetItem>> mWidgetsList = new HashMap<>();

Expand Down Expand Up @@ -156,7 +160,7 @@ private synchronized void setWidgetsAndShortcuts(ArrayList<WidgetItem> rawWidget

// Temporary list for {@link PackageItemInfos} to avoid having to go through
// {@link mPackageItemInfos} to locate the key to be used for {@link #mWidgetsList}
HashMap<PackageUserKey, PackageItemInfo> tmpPackageItemInfos = new HashMap<>();
HashMap<WidgetPackageOrCategoryKey, PackageItemInfo> tmpPackageItemInfos = new HashMap<>();

// Clear the lists only if this is an update on all widgets and shortcuts. If packageUser
// isn't null, only updates the shortcuts and widgets for the app represented in
Expand All @@ -168,11 +172,11 @@ private synchronized void setWidgetsAndShortcuts(ArrayList<WidgetItem> rawWidget
mWidgetsList.putAll(rawWidgetsShortcuts.stream()
.filter(new WidgetValidityCheck(app))
.collect(Collectors.groupingBy(item -> {
PackageUserKey packageUserKey = new PackageUserKey(
item.componentName.getPackageName(), item.user);
WidgetPackageOrCategoryKey packageUserKey = getWidgetPackageOrCategoryKey(item);
PackageItemInfo pInfo = tmpPackageItemInfos.get(packageUserKey);
if (pInfo == null) {
pInfo = new PackageItemInfo(packageUserKey.mPackageName);
pInfo = new PackageItemInfo(item.componentName.getPackageName(),
packageUserKey.mCategory);
pInfo.user = item.user;
tmpPackageItemInfos.put(packageUserKey, pInfo);
}
Expand Down Expand Up @@ -224,6 +228,13 @@ public WidgetItem getWidgetProviderInfoByProviderName(
return null;
}

private WidgetPackageOrCategoryKey getWidgetPackageOrCategoryKey(WidgetItem item) {
if (CONVERSATION_WIDGET.equals(item.componentName)) {
return new WidgetPackageOrCategoryKey(PackageItemInfo.CONVERSATIONS, item.user);
}
return new WidgetPackageOrCategoryKey(item.componentName.getPackageName(), item.user);
}

private static class WidgetValidityCheck implements Predicate<WidgetItem> {

private final InvariantDeviceProfile mIdp;
Expand Down Expand Up @@ -265,4 +276,40 @@ public boolean test(WidgetItem item) {
return true;
}
}

/** A hash key for grouping widgets by package name or category. */
private static class WidgetPackageOrCategoryKey {
/**
* The package name of the widget provider.
*
* <p>This shouldn't be empty if {@link #mCategory} has a value,
* {@link PackageItemInfo#NO_CATEGORY}.
*/
public final String mPackage;
/** A widget category. */
@PackageItemInfo.Category public final int mCategory;
public final UserHandle mUser;
private final int mHashCode;

WidgetPackageOrCategoryKey(String packageName, UserHandle user) {
this(packageName, PackageItemInfo.NO_CATEGORY, user);
}

WidgetPackageOrCategoryKey(@PackageItemInfo.Category int category, UserHandle user) {
this("", category, user);
}

private WidgetPackageOrCategoryKey(String packageName,
@PackageItemInfo.Category int category, UserHandle user) {
mPackage = packageName;
mCategory = category;
mUser = user;
mHashCode = Arrays.hashCode(new Object[]{mPackage, mCategory, mUser});
}

@Override
public int hashCode() {
return mHashCode;
}
}
}

0 comments on commit 1d8090e

Please sign in to comment.