use of com.android.launcher3.model.WidgetItem in project android_packages_apps_Launcher3 by crdroidandroid.
the class WidgetsPredictionUpdateTask method execute.
/**
* Uses the app predication result to infer widgets that the user may want to use.
*
* <p>The algorithm uses the app prediction ranking to create a widgets ranking which only
* includes one widget per app and excludes widgets that have already been added to the
* workspace.
*/
@Override
public void execute(LauncherAppState appState, BgDataModel dataModel, AllAppsList apps) {
Set<ComponentKey> widgetsInWorkspace = dataModel.appWidgets.stream().map(widget -> new ComponentKey(widget.providerName, widget.user)).collect(Collectors.toSet());
Map<PackageUserKey, List<WidgetItem>> allWidgets = dataModel.widgetsModel.getAllWidgetsWithoutShortcuts();
FixedContainerItems fixedContainerItems = mPredictorState.items;
fixedContainerItems.items.clear();
if (FeatureFlags.ENABLE_LOCAL_RECOMMENDED_WIDGETS_FILTER.get()) {
for (AppTarget app : mTargets) {
PackageUserKey packageUserKey = new PackageUserKey(app.getPackageName(), app.getUser());
if (allWidgets.containsKey(packageUserKey)) {
List<WidgetItem> notAddedWidgets = allWidgets.get(packageUserKey).stream().filter(item -> !widgetsInWorkspace.contains(new ComponentKey(item.componentName, item.user))).collect(Collectors.toList());
if (notAddedWidgets.size() > 0) {
// Even an apps have more than one widgets, we only include one widget.
fixedContainerItems.items.add(new PendingAddWidgetInfo(notAddedWidgets.get(0).widgetInfo, CONTAINER_WIDGETS_PREDICTION));
}
}
}
} else {
Map<ComponentKey, WidgetItem> widgetItems = allWidgets.values().stream().flatMap(List::stream).collect(Collectors.toMap(widget -> (ComponentKey) widget, widget -> widget));
for (AppTarget app : mTargets) {
if (TextUtils.isEmpty(app.getClassName())) {
continue;
}
ComponentKey targetWidget = new ComponentKey(new ComponentName(app.getPackageName(), app.getClassName()), app.getUser());
if (widgetItems.containsKey(targetWidget)) {
fixedContainerItems.items.add(new PendingAddWidgetInfo(widgetItems.get(targetWidget).widgetInfo, CONTAINER_WIDGETS_PREDICTION));
}
}
}
bindExtraContainerItems(fixedContainerItems);
// Don't store widgets prediction to disk because it is not used frequently.
}
use of com.android.launcher3.model.WidgetItem in project android_packages_apps_Launcher3 by crdroidandroid.
the class WidgetsListAdapter method ensureAllPreviewsReady.
/**
* Checks that all preview images are loaded and starts loading for those that aren't ready.
*
* @return true if all previews are ready and the data can be updated, false otherwise.
*/
private boolean ensureAllPreviewsReady() {
boolean allReady = true;
BaseActivity activity = BaseActivity.fromContext(mContext);
for (WidgetsListBaseEntry entry : mAllEntries) {
if (!(entry instanceof WidgetsListContentEntry))
continue;
WidgetsListContentEntry contentEntry = (WidgetsListContentEntry) entry;
if (!matchesKey(entry, mWidgetsContentVisiblePackageUserKey)) {
// If the entry isn't visible, clear any loaded previews.
mCachingPreviewLoader.clearPreviews(contentEntry.mWidgets);
continue;
}
for (int i = 0; i < entry.mWidgets.size(); i++) {
WidgetItem widgetItem = entry.mWidgets.get(i);
DeviceProfile deviceProfile = activity.getDeviceProfile();
Size widgetSize = WidgetSizes.getWidgetItemSizePx(mContext, deviceProfile, widgetItem);
if (widgetItem.isShortcut()) {
widgetSize = new Size(widgetSize.getWidth() + mShortcutPreviewPadding, widgetSize.getHeight() + mShortcutPreviewPadding);
}
if (widgetItem.hasPreviewLayout() || mCachingPreviewLoader.isPreviewLoaded(widgetItem, widgetSize)) {
// preview bitmap is in the cache.
continue;
}
// If we've reached this point, we should load the preview for the widget.
allReady = false;
mCachingPreviewLoader.loadPreview(activity, widgetItem, widgetSize, mPreviewLoadedCallback);
}
}
return allReady;
}
use of com.android.launcher3.model.WidgetItem in project android_packages_apps_Launcher3 by crdroidandroid.
the class WidgetsModel method setWidgetsAndShortcuts.
private synchronized void setWidgetsAndShortcuts(ArrayList<WidgetItem> rawWidgetsShortcuts, LauncherAppState app, @Nullable PackageUserKey packageUser) {
if (DEBUG) {
Log.d(TAG, "addWidgetsAndShortcuts, widgetsShortcuts#=" + rawWidgetsShortcuts.size());
}
// Temporary cache for {@link PackageItemInfos} to avoid having to go through
// {@link mPackageItemInfos} to locate the key to be used for {@link #mWidgetsList}
PackageItemInfoCache packageItemInfoCache = new PackageItemInfoCache();
if (packageUser == null) {
// Clear the list if this is an update on all widgets and shortcuts.
mWidgetsList.clear();
} else {
// Otherwise, only clear the widgets and shortcuts for the changed package.
mWidgetsList.remove(packageItemInfoCache.getOrCreate(new WidgetPackageOrCategoryKey(packageUser)));
}
// add and update.
mWidgetsList.putAll(rawWidgetsShortcuts.stream().filter(new WidgetValidityCheck(app)).collect(Collectors.groupingBy(item -> packageItemInfoCache.getOrCreate(getWidgetPackageOrCategoryKey(item)))));
// Update each package entry
IconCache iconCache = app.getIconCache();
for (PackageItemInfo p : packageItemInfoCache.values()) {
iconCache.getTitleAndIconForApp(p, true);
}
}
use of com.android.launcher3.model.WidgetItem in project android_packages_apps_Launcher3 by crdroidandroid.
the class WidgetsModel method getAllWidgetsWithoutShortcuts.
/**
* Returns a mapping of packages to their widgets without static shortcuts.
*/
public synchronized Map<PackageUserKey, List<WidgetItem>> getAllWidgetsWithoutShortcuts() {
Map<PackageUserKey, List<WidgetItem>> packagesToWidgets = new HashMap<>();
mWidgetsList.forEach((packageItemInfo, widgetsAndShortcuts) -> {
List<WidgetItem> widgets = widgetsAndShortcuts.stream().filter(item -> item.widgetInfo != null).collect(toList());
if (widgets.size() > 0) {
packagesToWidgets.put(new PackageUserKey(packageItemInfo.packageName, packageItemInfo.user), widgets);
}
});
return packagesToWidgets;
}
use of com.android.launcher3.model.WidgetItem in project android_packages_apps_Launcher3 by crdroidandroid.
the class WidgetsModel method getWidgetsListForPicker.
/**
* 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. This list is sorted at the UI when using
* {@link WidgetsDiffReporter}
*
* @see com.android.launcher3.widget.picker.WidgetsListAdapter#setWidgets(List)
*/
public synchronized ArrayList<WidgetsListBaseEntry> getWidgetsListForPicker(Context context) {
ArrayList<WidgetsListBaseEntry> result = new ArrayList<>();
AlphabeticIndexCompat indexer = new AlphabeticIndexCompat(context);
for (Map.Entry<PackageItemInfo, List<WidgetItem>> entry : mWidgetsList.entrySet()) {
PackageItemInfo pkgItem = entry.getKey();
List<WidgetItem> widgetItems = entry.getValue();
String sectionName = (pkgItem.title == null) ? "" : indexer.computeSectionName(pkgItem.title);
result.add(new WidgetsListHeaderEntry(pkgItem, sectionName, widgetItems));
result.add(new WidgetsListContentEntry(pkgItem, sectionName, widgetItems));
}
return result;
}
Aggregations