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;
|
2016-03-03 16:58:55 -08:00
|
|
|
import android.os.DeadObjectException;
|
|
|
|
|
import android.os.TransactionTooLargeException;
|
2015-04-08 19:01:34 -07:00
|
|
|
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.ItemInfo;
|
|
|
|
|
import com.android.launcher3.LauncherAppState;
|
2015-04-08 19:01:34 -07:00
|
|
|
import com.android.launcher3.LauncherAppWidgetProviderInfo;
|
2015-05-21 13:04:53 -07:00
|
|
|
import com.android.launcher3.Utilities;
|
2015-06-04 11:37:46 -07:00
|
|
|
import com.android.launcher3.compat.AlphabeticIndexCompat;
|
2015-07-16 17:24:30 -07:00
|
|
|
import com.android.launcher3.compat.AppWidgetManagerCompat;
|
2016-03-03 16:58:55 -08:00
|
|
|
import com.android.launcher3.config.ProviderConfig;
|
2015-04-08 19:01:34 -07:00
|
|
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
|
import java.util.Collections;
|
|
|
|
|
import java.util.Comparator;
|
|
|
|
|
import java.util.HashMap;
|
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 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;
|
|
|
|
|
|
|
|
|
|
/* List of packages that is tracked by this model. */
|
2015-09-22 16:56:55 -07:00
|
|
|
private final ArrayList<PackageItemInfo> mPackageItemInfos;
|
2015-04-08 19:01:34 -07:00
|
|
|
|
|
|
|
|
/* Map of widgets and shortcuts that are tracked per package. */
|
2016-03-10 12:02:29 -08:00
|
|
|
private final HashMap<PackageItemInfo, ArrayList<WidgetItem>> mWidgetsList;
|
2015-04-08 19:01:34 -07:00
|
|
|
|
2015-07-16 17:24:30 -07:00
|
|
|
private final AppWidgetManagerCompat mAppWidgetMgr;
|
2015-08-03 13:05:01 -07:00
|
|
|
private final Comparator<ItemInfo> mAppNameComparator;
|
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-09-22 16:56:55 -07:00
|
|
|
private final AlphabeticIndexCompat mIndexer;
|
|
|
|
|
|
2016-03-10 12:02:29 -08:00
|
|
|
private ArrayList<WidgetItem> mRawList;
|
2015-04-08 19:01:34 -07:00
|
|
|
|
2015-06-17 21:12:44 -07:00
|
|
|
public WidgetsModel(Context context, IconCache iconCache, AppFilter appFilter) {
|
2015-07-16 17:24:30 -07:00
|
|
|
mAppWidgetMgr = AppWidgetManagerCompat.getInstance(context);
|
2015-05-19 13:39:44 -07:00
|
|
|
mAppNameComparator = (new AppNameComparator(context)).getAppInfoComparator();
|
2015-06-17 21:12:44 -07:00
|
|
|
mIconCache = iconCache;
|
|
|
|
|
mAppFilter = appFilter;
|
2015-06-04 11:37:46 -07:00
|
|
|
mIndexer = new AlphabeticIndexCompat(context);
|
2015-09-22 16:56:55 -07:00
|
|
|
mPackageItemInfos = new ArrayList<>();
|
|
|
|
|
mWidgetsList = new HashMap<>();
|
2016-02-23 11:36:21 -08:00
|
|
|
|
2015-09-22 16:56:55 -07:00
|
|
|
mRawList = new ArrayList<>();
|
2015-05-22 14:49:23 -07:00
|
|
|
}
|
|
|
|
|
|
2015-08-03 13:05:01 -07:00
|
|
|
@SuppressWarnings("unchecked")
|
2015-05-22 14:49:23 -07:00
|
|
|
private WidgetsModel(WidgetsModel model) {
|
2015-07-16 17:24:30 -07:00
|
|
|
mAppWidgetMgr = model.mAppWidgetMgr;
|
2015-05-22 14:49:23 -07:00
|
|
|
mPackageItemInfos = (ArrayList<PackageItemInfo>) model.mPackageItemInfos.clone();
|
2016-03-10 12:02:29 -08:00
|
|
|
mWidgetsList = (HashMap<PackageItemInfo, ArrayList<WidgetItem>>) model.mWidgetsList.clone();
|
2015-05-22 14:49:23 -07:00
|
|
|
mAppNameComparator = model.mAppNameComparator;
|
|
|
|
|
mIconCache = model.mIconCache;
|
2015-06-17 21:12:44 -07:00
|
|
|
mAppFilter = model.mAppFilter;
|
2015-09-22 16:56:55 -07:00
|
|
|
mIndexer = model.mIndexer;
|
2016-03-10 12:02:29 -08:00
|
|
|
mRawList = (ArrayList<WidgetItem>) model.mRawList.clone();
|
2015-04-08 19:01:34 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Access methods that may be deleted if the private fields are made package-private.
|
|
|
|
|
public int getPackageSize() {
|
2015-04-11 15:44:32 -07:00
|
|
|
return mPackageItemInfos.size();
|
2015-04-08 19:01:34 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Access methods that may be deleted if the private fields are made package-private.
|
2015-04-11 15:44:32 -07:00
|
|
|
public PackageItemInfo getPackageItemInfo(int pos) {
|
2015-06-04 11:37:46 -07:00
|
|
|
if (pos >= mPackageItemInfos.size() || pos < 0) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
2015-04-11 15:44:32 -07:00
|
|
|
return mPackageItemInfos.get(pos);
|
2015-04-08 19:01:34 -07:00
|
|
|
}
|
|
|
|
|
|
2016-03-10 12:02:29 -08:00
|
|
|
public List<WidgetItem> getSortedWidgets(int pos) {
|
2015-04-11 15:44:32 -07:00
|
|
|
return mWidgetsList.get(mPackageItemInfos.get(pos));
|
2015-04-08 19:01:34 -07:00
|
|
|
}
|
|
|
|
|
|
2016-03-10 12:02:29 -08:00
|
|
|
public ArrayList<WidgetItem> getRawList() {
|
2015-05-21 13:04:53 -07:00
|
|
|
return mRawList;
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-03 16:58:55 -08:00
|
|
|
public boolean isEmpty() {
|
|
|
|
|
return mRawList.isEmpty();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public WidgetsModel updateAndClone(Context context) {
|
2015-05-21 13:04:53 -07:00
|
|
|
Utilities.assertWorkerThread();
|
2016-03-03 16:58:55 -08:00
|
|
|
|
|
|
|
|
try {
|
2016-03-10 12:02:29 -08:00
|
|
|
final ArrayList<WidgetItem> widgetsAndShortcuts = new ArrayList<>();
|
2016-03-03 16:58:55 -08:00
|
|
|
// 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) {
|
|
|
|
|
if (!ProviderConfig.IS_DOGFOOD_BUILD &&
|
|
|
|
|
(e.getCause() instanceof TransactionTooLargeException ||
|
|
|
|
|
e.getCause() instanceof DeadObjectException)) {
|
|
|
|
|
// 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;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return clone();
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-10 12:02:29 -08:00
|
|
|
private void setWidgetsAndShortcuts(ArrayList<WidgetItem> rawWidgetsShortcuts) {
|
2015-05-21 13:04:53 -07:00
|
|
|
mRawList = 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-04-14 11:50:44 -07:00
|
|
|
mPackageItemInfos.clear();
|
2015-04-08 19:01:34 -07:00
|
|
|
|
2015-08-03 13:05:01 -07:00
|
|
|
InvariantDeviceProfile idp = LauncherAppState.getInstance().getInvariantDeviceProfile();
|
|
|
|
|
|
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-03-10 12:02:29 -08:00
|
|
|
if (mAppFilter != null && !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-03-10 12:02:29 -08:00
|
|
|
ArrayList<WidgetItem> widgetsShortcutsList = mWidgetsList.get(pInfo);
|
|
|
|
|
|
|
|
|
|
if (widgetsShortcutsList == null) {
|
2015-07-27 17:50:13 -07:00
|
|
|
widgetsShortcutsList = new ArrayList<>();
|
2016-03-10 12:02:29 -08:00
|
|
|
|
2015-04-11 15:44:32 -07:00
|
|
|
pInfo = new PackageItemInfo(packageName);
|
|
|
|
|
tmpPackageItemInfos.put(packageName, pInfo);
|
2016-03-10 12:02:29 -08:00
|
|
|
|
2015-04-11 15:44:32 -07:00
|
|
|
mPackageItemInfos.add(pInfo);
|
2016-03-10 12:02:29 -08:00
|
|
|
mWidgetsList.put(pInfo, widgetsShortcutsList);
|
2015-04-08 19:01:34 -07:00
|
|
|
}
|
2016-03-10 12:02:29 -08:00
|
|
|
|
|
|
|
|
widgetsShortcutsList.add(item);
|
2015-04-08 19:01:34 -07:00
|
|
|
}
|
|
|
|
|
|
2016-03-10 12:02:29 -08:00
|
|
|
// Update each package entry
|
|
|
|
|
for (PackageItemInfo p : mPackageItemInfos) {
|
|
|
|
|
ArrayList<WidgetItem> widgetsShortcutsList = mWidgetsList.get(p);
|
|
|
|
|
Collections.sort(widgetsShortcutsList);
|
|
|
|
|
|
|
|
|
|
// Update the package entry based on the first item.
|
|
|
|
|
p.user = widgetsShortcutsList.get(0).user;
|
|
|
|
|
mIconCache.getTitleAndIconForApp(p, true /* userLowResIcon */);
|
|
|
|
|
p.titleSectionName = mIndexer.computeSectionName(p.title);
|
2015-04-08 19:01:34 -07:00
|
|
|
}
|
2016-03-10 12:02:29 -08:00
|
|
|
|
|
|
|
|
// sort the package entries.
|
|
|
|
|
Collections.sort(mPackageItemInfos, mAppNameComparator);
|
2015-04-08 19:01:34 -07:00
|
|
|
}
|
2015-05-22 14:49:23 -07:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Create a snapshot of the widgets model.
|
|
|
|
|
* <p>
|
|
|
|
|
* Usage case: view binding without being modified from package updates.
|
|
|
|
|
*/
|
|
|
|
|
@Override
|
|
|
|
|
public WidgetsModel clone(){
|
|
|
|
|
return new WidgetsModel(this);
|
|
|
|
|
}
|
2015-04-11 15:44:32 -07:00
|
|
|
}
|