2015-04-08 19:01:34 -07:00
|
|
|
|
2015-05-21 13:04:53 -07:00
|
|
|
package com.android.launcher3.model;
|
2015-04-08 19:01:34 -07:00
|
|
|
|
2016-03-03 16:58:55 -08:00
|
|
|
import android.appwidget.AppWidgetProviderInfo;
|
2015-04-08 19:01:34 -07:00
|
|
|
import android.content.Context;
|
2016-03-03 16:58:55 -08:00
|
|
|
import android.content.Intent;
|
2016-03-10 12:02:29 -08:00
|
|
|
import android.content.pm.PackageManager;
|
2015-04-08 19:01:34 -07:00
|
|
|
import android.content.pm.ResolveInfo;
|
|
|
|
|
import android.util.Log;
|
|
|
|
|
|
2015-06-17 21:12:44 -07:00
|
|
|
import com.android.launcher3.AppFilter;
|
2015-04-08 19:01:34 -07:00
|
|
|
import com.android.launcher3.IconCache;
|
2015-08-03 13:05:01 -07:00
|
|
|
import com.android.launcher3.InvariantDeviceProfile;
|
|
|
|
|
import com.android.launcher3.LauncherAppState;
|
2015-04-08 19:01:34 -07:00
|
|
|
import com.android.launcher3.LauncherAppWidgetProviderInfo;
|
2016-11-04 10:19:58 -07:00
|
|
|
import com.android.launcher3.Utilities;
|
2015-07-16 17:24:30 -07:00
|
|
|
import com.android.launcher3.compat.AppWidgetManagerCompat;
|
2016-10-12 20:49:31 -07:00
|
|
|
import com.android.launcher3.compat.UserHandleCompat;
|
2016-03-03 16:58:55 -08:00
|
|
|
import com.android.launcher3.config.ProviderConfig;
|
2016-10-12 20:49:31 -07:00
|
|
|
import com.android.launcher3.util.MultiHashMap;
|
2016-04-15 18:03:27 -07:00
|
|
|
import com.android.launcher3.util.Preconditions;
|
2015-04-08 19:01:34 -07:00
|
|
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
|
import java.util.HashMap;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Widgets data model that is used by the adapters of the widget views and controllers.
|
|
|
|
|
*
|
|
|
|
|
* <p> The widgets and shortcuts are organized using package name as its index.
|
|
|
|
|
*/
|
|
|
|
|
public class WidgetsModel {
|
|
|
|
|
|
|
|
|
|
private static final String TAG = "WidgetsModel";
|
|
|
|
|
private static final boolean DEBUG = false;
|
|
|
|
|
|
|
|
|
|
/* Map of widgets and shortcuts that are tracked per package. */
|
2016-10-12 20:49:31 -07:00
|
|
|
private final MultiHashMap<PackageItemInfo, WidgetItem> mWidgetsList;
|
2015-04-08 19:01:34 -07:00
|
|
|
|
2015-05-21 13:04:53 -07:00
|
|
|
private final IconCache mIconCache;
|
2015-06-17 21:12:44 -07:00
|
|
|
private final AppFilter mAppFilter;
|
2015-04-08 19:01:34 -07:00
|
|
|
|
2016-10-12 20:49:31 -07:00
|
|
|
public WidgetsModel(IconCache iconCache, AppFilter appFilter) {
|
2015-06-17 21:12:44 -07:00
|
|
|
mIconCache = iconCache;
|
|
|
|
|
mAppFilter = appFilter;
|
2016-10-12 20:49:31 -07:00
|
|
|
mWidgetsList = new MultiHashMap<>();
|
2015-04-08 19:01:34 -07:00
|
|
|
}
|
|
|
|
|
|
2016-10-12 20:49:31 -07:00
|
|
|
public MultiHashMap<PackageItemInfo, WidgetItem> getWidgetsMap() {
|
|
|
|
|
return mWidgetsList;
|
2015-05-21 13:04:53 -07:00
|
|
|
}
|
|
|
|
|
|
2016-03-03 16:58:55 -08:00
|
|
|
public boolean isEmpty() {
|
2016-10-12 20:49:31 -07:00
|
|
|
return mWidgetsList.isEmpty();
|
2016-03-03 16:58:55 -08:00
|
|
|
}
|
|
|
|
|
|
2016-10-12 20:49:31 -07:00
|
|
|
public ArrayList<WidgetItem> update(Context context) {
|
2016-04-15 18:03:27 -07:00
|
|
|
Preconditions.assertWorkerThread();
|
2016-03-03 16:58:55 -08:00
|
|
|
|
2016-10-12 20:49:31 -07:00
|
|
|
final ArrayList<WidgetItem> widgetsAndShortcuts = new ArrayList<>();
|
2016-03-03 16:58:55 -08:00
|
|
|
try {
|
|
|
|
|
// Widgets
|
2016-03-10 12:02:29 -08:00
|
|
|
AppWidgetManagerCompat widgetManager = AppWidgetManagerCompat.getInstance(context);
|
|
|
|
|
for (AppWidgetProviderInfo widgetInfo : widgetManager.getAllProviders()) {
|
|
|
|
|
widgetsAndShortcuts.add(new WidgetItem(
|
|
|
|
|
LauncherAppWidgetProviderInfo.fromProviderInfo(context, widgetInfo),
|
|
|
|
|
widgetManager));
|
2016-03-03 16:58:55 -08:00
|
|
|
}
|
2016-03-10 12:02:29 -08:00
|
|
|
|
2016-03-03 16:58:55 -08:00
|
|
|
// Shortcuts
|
2016-03-10 12:02:29 -08:00
|
|
|
PackageManager pm = context.getPackageManager();
|
|
|
|
|
for (ResolveInfo info :
|
|
|
|
|
pm.queryIntentActivities(new Intent(Intent.ACTION_CREATE_SHORTCUT), 0)) {
|
|
|
|
|
widgetsAndShortcuts.add(new WidgetItem(info, pm));
|
|
|
|
|
}
|
2016-03-03 16:58:55 -08:00
|
|
|
setWidgetsAndShortcuts(widgetsAndShortcuts);
|
|
|
|
|
} catch (Exception e) {
|
2016-11-04 10:19:58 -07:00
|
|
|
if (!ProviderConfig.IS_DOGFOOD_BUILD && Utilities.isBinderSizeError(e)) {
|
2016-03-03 16:58:55 -08:00
|
|
|
// the returned value may be incomplete and will not be refreshed until the next
|
|
|
|
|
// time Launcher starts.
|
|
|
|
|
// TODO: after figuring out a repro step, introduce a dirty bit to check when
|
|
|
|
|
// onResume is called to refresh the widget provider list.
|
|
|
|
|
} else {
|
|
|
|
|
throw e;
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-10-12 20:49:31 -07:00
|
|
|
return widgetsAndShortcuts;
|
2016-03-03 16:58:55 -08:00
|
|
|
}
|
|
|
|
|
|
2016-03-10 12:02:29 -08:00
|
|
|
private void setWidgetsAndShortcuts(ArrayList<WidgetItem> rawWidgetsShortcuts) {
|
2015-04-08 19:01:34 -07:00
|
|
|
if (DEBUG) {
|
2015-05-21 13:04:53 -07:00
|
|
|
Log.d(TAG, "addWidgetsAndShortcuts, widgetsShortcuts#=" + rawWidgetsShortcuts.size());
|
2015-04-08 19:01:34 -07:00
|
|
|
}
|
|
|
|
|
|
2015-04-11 15:44:32 -07:00
|
|
|
// Temporary list for {@link PackageItemInfos} to avoid having to go through
|
|
|
|
|
// {@link mPackageItemInfos} to locate the key to be used for {@link #mWidgetsList}
|
|
|
|
|
HashMap<String, PackageItemInfo> tmpPackageItemInfos = new HashMap<>();
|
2015-04-14 11:50:44 -07:00
|
|
|
|
2015-04-08 19:01:34 -07:00
|
|
|
// clear the lists.
|
|
|
|
|
mWidgetsList.clear();
|
|
|
|
|
|
2015-08-03 13:05:01 -07:00
|
|
|
InvariantDeviceProfile idp = LauncherAppState.getInstance().getInvariantDeviceProfile();
|
2016-10-12 20:49:31 -07:00
|
|
|
UserHandleCompat myUser = UserHandleCompat.myUserHandle();
|
2015-08-03 13:05:01 -07:00
|
|
|
|
2015-04-08 19:01:34 -07:00
|
|
|
// add and update.
|
2016-03-10 12:02:29 -08:00
|
|
|
for (WidgetItem item: rawWidgetsShortcuts) {
|
|
|
|
|
if (item.widgetInfo != null) {
|
2015-08-03 13:05:01 -07:00
|
|
|
// Ensure that all widgets we show can be added on a workspace of this size
|
2016-03-10 12:02:29 -08:00
|
|
|
int minSpanX = Math.min(item.widgetInfo.spanX, item.widgetInfo.minSpanX);
|
|
|
|
|
int minSpanY = Math.min(item.widgetInfo.spanY, item.widgetInfo.minSpanY);
|
|
|
|
|
if (minSpanX > idp.numColumns || minSpanY > idp.numRows) {
|
2015-08-03 13:05:01 -07:00
|
|
|
if (DEBUG) {
|
|
|
|
|
Log.d(TAG, String.format(
|
|
|
|
|
"Widget %s : (%d X %d) can't fit on this device",
|
2016-03-10 12:02:29 -08:00
|
|
|
item.componentName, minSpanX, minSpanY));
|
2015-08-03 13:05:01 -07:00
|
|
|
}
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2015-06-17 21:12:44 -07:00
|
|
|
}
|
|
|
|
|
|
2016-12-02 19:29:43 +05:30
|
|
|
if (!mAppFilter.shouldShowApp(item.componentName)) {
|
2015-07-13 10:26:22 -07:00
|
|
|
if (DEBUG) {
|
|
|
|
|
Log.d(TAG, String.format("%s is filtered and not added to the widget tray.",
|
2016-03-10 12:02:29 -08:00
|
|
|
item.componentName));
|
2015-07-13 10:26:22 -07:00
|
|
|
}
|
2015-06-17 21:12:44 -07:00
|
|
|
continue;
|
2015-04-08 19:01:34 -07:00
|
|
|
}
|
|
|
|
|
|
2016-03-10 12:02:29 -08:00
|
|
|
String packageName = item.componentName.getPackageName();
|
2015-04-11 15:44:32 -07:00
|
|
|
PackageItemInfo pInfo = tmpPackageItemInfos.get(packageName);
|
2016-10-12 20:49:31 -07:00
|
|
|
if (pInfo == null) {
|
2015-04-11 15:44:32 -07:00
|
|
|
pInfo = new PackageItemInfo(packageName);
|
2016-10-12 20:49:31 -07:00
|
|
|
pInfo.user = item.user;
|
2015-04-11 15:44:32 -07:00
|
|
|
tmpPackageItemInfos.put(packageName, pInfo);
|
2016-10-12 20:49:31 -07:00
|
|
|
} else if (!myUser.equals(pInfo.user)) {
|
|
|
|
|
// Keep updating the user, until we get the primary user.
|
|
|
|
|
pInfo.user = item.user;
|
2015-04-08 19:01:34 -07:00
|
|
|
}
|
2016-10-12 20:49:31 -07:00
|
|
|
mWidgetsList.addToList(pInfo, item);
|
2015-04-08 19:01:34 -07:00
|
|
|
}
|
|
|
|
|
|
2016-03-10 12:02:29 -08:00
|
|
|
// Update each package entry
|
2016-10-12 20:49:31 -07:00
|
|
|
for (PackageItemInfo p : tmpPackageItemInfos.values()) {
|
2016-03-10 12:02:29 -08:00
|
|
|
mIconCache.getTitleAndIconForApp(p, true /* userLowResIcon */);
|
2015-04-08 19:01:34 -07:00
|
|
|
}
|
2015-05-22 14:49:23 -07:00
|
|
|
}
|
2015-04-11 15:44:32 -07:00
|
|
|
}
|