mirror of
https://github.com/LawnchairLauncher/lawnchair.git
synced 2026-02-28 07:46:55 +00:00
Add support for getting widgets/shortucts for a particular package/user
Bug: 34940468 Bug: 33553066 Change-Id: I5d0131df206c6a13d4227ad28c5b094bbf1343df
This commit is contained in:
@@ -3,11 +3,10 @@ package com.android.launcher3.model;
|
||||
|
||||
import android.appwidget.AppWidgetProviderInfo;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.pm.PackageManager;
|
||||
import android.content.pm.ResolveInfo;
|
||||
import android.os.Process;
|
||||
import android.os.UserHandle;
|
||||
import android.support.annotation.Nullable;
|
||||
import android.util.Log;
|
||||
|
||||
import com.android.launcher3.AppFilter;
|
||||
@@ -19,13 +18,14 @@ import com.android.launcher3.Utilities;
|
||||
import com.android.launcher3.compat.AppWidgetManagerCompat;
|
||||
import com.android.launcher3.compat.LauncherAppsCompat;
|
||||
import com.android.launcher3.compat.ShortcutConfigActivityInfo;
|
||||
import com.android.launcher3.compat.UserManagerCompat;
|
||||
import com.android.launcher3.config.ProviderConfig;
|
||||
import com.android.launcher3.util.MultiHashMap;
|
||||
import com.android.launcher3.util.PackageUserKey;
|
||||
import com.android.launcher3.util.Preconditions;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.Iterator;
|
||||
|
||||
/**
|
||||
* Widgets data model that is used by the adapters of the widget views and controllers.
|
||||
@@ -57,7 +57,11 @@ public class WidgetsModel {
|
||||
return mWidgetsList.isEmpty();
|
||||
}
|
||||
|
||||
public ArrayList<WidgetItem> update(Context context) {
|
||||
/**
|
||||
* @param packageUser If null, all widgets and shortcuts are updated and returned, otherwise
|
||||
* only widgets and shortcuts associated with the package/user are.
|
||||
*/
|
||||
public ArrayList<WidgetItem> update(Context context, @Nullable PackageUserKey packageUser) {
|
||||
Preconditions.assertWorkerThread();
|
||||
|
||||
final ArrayList<WidgetItem> widgetsAndShortcuts = new ArrayList<>();
|
||||
@@ -66,18 +70,18 @@ public class WidgetsModel {
|
||||
InvariantDeviceProfile idp = LauncherAppState.getIDP(context);
|
||||
|
||||
// Widgets
|
||||
for (AppWidgetProviderInfo widgetInfo :
|
||||
AppWidgetManagerCompat.getInstance(context).getAllProviders()) {
|
||||
AppWidgetManagerCompat widgetManager = AppWidgetManagerCompat.getInstance(context);
|
||||
for (AppWidgetProviderInfo widgetInfo : widgetManager.getAllProviders(packageUser)) {
|
||||
widgetsAndShortcuts.add(new WidgetItem(LauncherAppWidgetProviderInfo
|
||||
.fromProviderInfo(context, widgetInfo), pm, idp));
|
||||
}
|
||||
|
||||
// Shortcuts
|
||||
for (ShortcutConfigActivityInfo info : LauncherAppsCompat.getInstance(context)
|
||||
.getCustomShortcutActivityList()) {
|
||||
.getCustomShortcutActivityList(packageUser)) {
|
||||
widgetsAndShortcuts.add(new WidgetItem(info));
|
||||
}
|
||||
setWidgetsAndShortcuts(widgetsAndShortcuts, context);
|
||||
setWidgetsAndShortcuts(widgetsAndShortcuts, context, packageUser);
|
||||
} catch (Exception e) {
|
||||
if (!ProviderConfig.IS_DOGFOOD_BUILD && Utilities.isBinderSizeError(e)) {
|
||||
// the returned value may be incomplete and will not be refreshed until the next
|
||||
@@ -92,7 +96,7 @@ public class WidgetsModel {
|
||||
}
|
||||
|
||||
private void setWidgetsAndShortcuts(ArrayList<WidgetItem> rawWidgetsShortcuts,
|
||||
Context context) {
|
||||
Context context, @Nullable PackageUserKey packageUser) {
|
||||
if (DEBUG) {
|
||||
Log.d(TAG, "addWidgetsAndShortcuts, widgetsShortcuts#=" + rawWidgetsShortcuts.size());
|
||||
}
|
||||
@@ -102,13 +106,38 @@ public class WidgetsModel {
|
||||
HashMap<String, PackageItemInfo> tmpPackageItemInfos = new HashMap<>();
|
||||
|
||||
// clear the lists.
|
||||
mWidgetsList.clear();
|
||||
if (packageUser == null) {
|
||||
mWidgetsList.clear();
|
||||
} else {
|
||||
// Only clear the widgets for the given package/user.
|
||||
PackageItemInfo packageItem = null;
|
||||
for (PackageItemInfo item : mWidgetsList.keySet()) {
|
||||
if (item.packageName.equals(packageUser.mPackageName)) {
|
||||
packageItem = item;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (packageItem != null) {
|
||||
// We want to preserve the user that was on the packageItem previously,
|
||||
// so add it to tmpPackageItemInfos here to avoid creating a new entry.
|
||||
tmpPackageItemInfos.put(packageItem.packageName, packageItem);
|
||||
|
||||
Iterator<WidgetItem> widgetItemIterator = mWidgetsList.get(packageItem).iterator();
|
||||
while (widgetItemIterator.hasNext()) {
|
||||
WidgetItem nextWidget = widgetItemIterator.next();
|
||||
if (nextWidget.componentName.getPackageName().equals(packageUser.mPackageName)
|
||||
&& nextWidget.user.equals(packageUser.mUser)) {
|
||||
widgetItemIterator.remove();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
InvariantDeviceProfile idp = LauncherAppState.getIDP(context);
|
||||
UserHandle myUser = Process.myUserHandle();
|
||||
|
||||
// add and update.
|
||||
for (WidgetItem item: rawWidgetsShortcuts) {
|
||||
for (WidgetItem item : rawWidgetsShortcuts) {
|
||||
if (item.widgetInfo != null) {
|
||||
// Ensure that all widgets we show can be added on a workspace of this size
|
||||
int minSpanX = Math.min(item.widgetInfo.spanX, item.widgetInfo.minSpanX);
|
||||
|
||||
Reference in New Issue
Block a user