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-06-17 21:12:44 -07:00
|
|
|
import android.content.ComponentName;
|
2015-04-08 19:01:34 -07:00
|
|
|
import android.content.Context;
|
2016-03-03 16:58:55 -08:00
|
|
|
import android.content.Intent;
|
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;
|
2015-04-08 19:01:34 -07:00
|
|
|
import com.android.launcher3.compat.UserHandleCompat;
|
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. */
|
2015-09-22 16:56:55 -07:00
|
|
|
private final HashMap<PackageItemInfo, ArrayList<Object>> mWidgetsList;
|
2015-04-08 19:01:34 -07:00
|
|
|
|
2015-07-16 17:24:30 -07:00
|
|
|
private final AppWidgetManagerCompat mAppWidgetMgr;
|
2015-07-27 17:50:13 -07:00
|
|
|
private final WidgetsAndShortcutNameComparator mWidgetAndShortcutNameComparator;
|
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;
|
|
|
|
|
|
|
|
|
|
private ArrayList<Object> 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
|
|
|
mWidgetAndShortcutNameComparator = new WidgetsAndShortcutNameComparator(context);
|
|
|
|
|
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();
|
|
|
|
|
mWidgetsList = (HashMap<PackageItemInfo, ArrayList<Object>>) model.mWidgetsList.clone();
|
|
|
|
|
mWidgetAndShortcutNameComparator = model.mWidgetAndShortcutNameComparator;
|
|
|
|
|
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;
|
|
|
|
|
mRawList = (ArrayList<Object>) 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
|
|
|
}
|
|
|
|
|
|
2015-04-11 15:44:32 -07:00
|
|
|
public List<Object> getSortedWidgets(int pos) {
|
|
|
|
|
return mWidgetsList.get(mPackageItemInfos.get(pos));
|
2015-04-08 19:01:34 -07:00
|
|
|
}
|
|
|
|
|
|
2015-05-21 13:04:53 -07:00
|
|
|
public ArrayList<Object> getRawList() {
|
|
|
|
|
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 {
|
|
|
|
|
final ArrayList<Object> widgetsAndShortcuts = new ArrayList<>();
|
|
|
|
|
// Widgets
|
|
|
|
|
for (AppWidgetProviderInfo widgetInfo :
|
|
|
|
|
AppWidgetManagerCompat.getInstance(context).getAllProviders()) {
|
|
|
|
|
widgetsAndShortcuts.add(LauncherAppWidgetProviderInfo
|
|
|
|
|
.fromProviderInfo(context, widgetInfo));
|
|
|
|
|
}
|
|
|
|
|
// Shortcuts
|
|
|
|
|
widgetsAndShortcuts.addAll(context.getPackageManager().queryIntentActivities(
|
|
|
|
|
new Intent(Intent.ACTION_CREATE_SHORTCUT), 0));
|
|
|
|
|
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();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void setWidgetsAndShortcuts(ArrayList<Object> 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-07-27 17:50:13 -07:00
|
|
|
mWidgetAndShortcutNameComparator.reset();
|
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.
|
2015-05-21 13:04:53 -07:00
|
|
|
for (Object o: rawWidgetsShortcuts) {
|
2015-04-08 19:01:34 -07:00
|
|
|
String packageName = "";
|
2015-07-16 17:24:30 -07:00
|
|
|
UserHandleCompat userHandle = null;
|
2015-06-17 21:12:44 -07:00
|
|
|
ComponentName componentName = null;
|
2015-04-08 19:01:34 -07:00
|
|
|
if (o instanceof LauncherAppWidgetProviderInfo) {
|
|
|
|
|
LauncherAppWidgetProviderInfo widgetInfo = (LauncherAppWidgetProviderInfo) o;
|
2015-08-03 13:05:01 -07:00
|
|
|
|
|
|
|
|
// Ensure that all widgets we show can be added on a workspace of this size
|
|
|
|
|
int minSpanX = Math.min(widgetInfo.spanX, widgetInfo.minSpanX);
|
|
|
|
|
int minSpanY = Math.min(widgetInfo.spanY, widgetInfo.minSpanY);
|
|
|
|
|
if (minSpanX <= (int) idp.numColumns &&
|
|
|
|
|
minSpanY <= (int) idp.numRows) {
|
|
|
|
|
componentName = widgetInfo.provider;
|
|
|
|
|
packageName = widgetInfo.provider.getPackageName();
|
|
|
|
|
userHandle = mAppWidgetMgr.getUser(widgetInfo);
|
|
|
|
|
} else {
|
|
|
|
|
if (DEBUG) {
|
|
|
|
|
Log.d(TAG, String.format(
|
|
|
|
|
"Widget %s : (%d X %d) can't fit on this device",
|
|
|
|
|
widgetInfo.provider, minSpanX, minSpanY));
|
|
|
|
|
}
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2015-04-08 19:01:34 -07:00
|
|
|
} else if (o instanceof ResolveInfo) {
|
|
|
|
|
ResolveInfo resolveInfo = (ResolveInfo) o;
|
2015-06-17 21:12:44 -07:00
|
|
|
componentName = new ComponentName(resolveInfo.activityInfo.packageName,
|
|
|
|
|
resolveInfo.activityInfo.name);
|
2015-04-08 19:01:34 -07:00
|
|
|
packageName = resolveInfo.activityInfo.packageName;
|
2015-07-16 17:24:30 -07:00
|
|
|
userHandle = UserHandleCompat.myUserHandle();
|
2015-06-17 21:12:44 -07:00
|
|
|
}
|
|
|
|
|
|
2015-07-16 17:24:30 -07:00
|
|
|
if (componentName == null || userHandle == null) {
|
|
|
|
|
Log.e(TAG, String.format("Widget cannot be set for %s.", o.getClass().toString()));
|
2015-06-17 21:12:44 -07:00
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
if (mAppFilter != null && !mAppFilter.shouldShowApp(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.",
|
2015-06-17 21:12:44 -07:00
|
|
|
packageName));
|
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
|
|
|
}
|
|
|
|
|
|
2015-04-11 15:44:32 -07:00
|
|
|
PackageItemInfo pInfo = tmpPackageItemInfos.get(packageName);
|
|
|
|
|
ArrayList<Object> widgetsShortcutsList = mWidgetsList.get(pInfo);
|
2015-04-08 19:01:34 -07:00
|
|
|
if (widgetsShortcutsList != null) {
|
|
|
|
|
widgetsShortcutsList.add(o);
|
|
|
|
|
} else {
|
2015-07-27 17:50:13 -07:00
|
|
|
widgetsShortcutsList = new ArrayList<>();
|
2015-04-08 19:01:34 -07:00
|
|
|
widgetsShortcutsList.add(o);
|
2015-04-11 15:44:32 -07:00
|
|
|
pInfo = new PackageItemInfo(packageName);
|
2015-07-16 17:24:30 -07:00
|
|
|
mIconCache.getTitleAndIconForApp(packageName, userHandle,
|
2015-05-21 13:04:53 -07:00
|
|
|
true /* userLowResIcon */, pInfo);
|
2015-06-04 11:37:46 -07:00
|
|
|
pInfo.titleSectionName = mIndexer.computeSectionName(pInfo.title);
|
2015-04-11 15:44:32 -07:00
|
|
|
mWidgetsList.put(pInfo, widgetsShortcutsList);
|
|
|
|
|
tmpPackageItemInfos.put(packageName, pInfo);
|
|
|
|
|
mPackageItemInfos.add(pInfo);
|
2015-04-08 19:01:34 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// sort.
|
2015-05-19 13:39:44 -07:00
|
|
|
Collections.sort(mPackageItemInfos, mAppNameComparator);
|
2015-04-11 15:44:32 -07:00
|
|
|
for (PackageItemInfo p: mPackageItemInfos) {
|
|
|
|
|
Collections.sort(mWidgetsList.get(p), mWidgetAndShortcutNameComparator);
|
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
|
|
|
}
|