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
|
|
|
|
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-02-22 16:36:15 -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.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-02-22 16:36:15 -08:00
|
|
|
import com.android.launcher3.util.ComponentKey;
|
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
|
|
|
|
2016-02-22 16:36:15 -08:00
|
|
|
/* Labels of widgets */
|
|
|
|
|
private final HashMap<ComponentKey, CharSequence> mLabels;
|
|
|
|
|
|
2015-07-16 17:24:30 -07:00
|
|
|
private final AppWidgetManagerCompat mAppWidgetMgr;
|
2016-02-22 16:36:15 -08:00
|
|
|
private final PackageManager mPackageMgr;
|
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);
|
2016-02-22 16:36:15 -08:00
|
|
|
mPackageMgr = context.getPackageManager();
|
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<>();
|
|
|
|
|
mRawList = new ArrayList<>();
|
2016-02-22 16:36:15 -08:00
|
|
|
mLabels = new HashMap<>();
|
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;
|
2016-02-22 16:36:15 -08:00
|
|
|
mPackageMgr = model.mPackageMgr;
|
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();
|
2016-02-22 16:36:15 -08:00
|
|
|
mLabels = (HashMap<ComponentKey, CharSequence>) model.mLabels.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-02-22 16:36:15 -08:00
|
|
|
public CharSequence getLabel(Object info) {
|
|
|
|
|
ComponentKey key;
|
|
|
|
|
if (info instanceof LauncherAppWidgetProviderInfo) {
|
|
|
|
|
key = new ComponentKey(((LauncherAppWidgetProviderInfo) info).provider,
|
|
|
|
|
mAppWidgetMgr.getUser(((LauncherAppWidgetProviderInfo) info)));
|
|
|
|
|
return mLabels.get(key);
|
|
|
|
|
} else if (info instanceof ResolveInfo) {
|
|
|
|
|
ResolveInfo ri = (ResolveInfo) info;
|
|
|
|
|
ComponentName componentName = new ComponentName(ri.activityInfo.packageName,
|
|
|
|
|
ri.activityInfo.name);
|
|
|
|
|
key = new ComponentKey(componentName, UserHandleCompat.myUserHandle());
|
|
|
|
|
return mLabels.get(key);
|
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2015-06-17 21:12:44 -07:00
|
|
|
public void setWidgetsAndShortcuts(ArrayList<Object> rawWidgetsShortcuts) {
|
2015-05-21 13:04:53 -07:00
|
|
|
Utilities.assertWorkerThread();
|
|
|
|
|
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();
|
2016-02-22 16:36:15 -08:00
|
|
|
mLabels.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;
|
2016-02-22 16:36:15 -08:00
|
|
|
ComponentKey key = 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;
|
|
|
|
|
}
|
2016-02-22 16:36:15 -08:00
|
|
|
key = new ComponentKey(componentName, userHandle);
|
|
|
|
|
mLabels.put(key, mAppWidgetMgr.loadLabel(widgetInfo));
|
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();
|
2016-02-22 16:36:15 -08:00
|
|
|
key = new ComponentKey(componentName, userHandle);
|
|
|
|
|
mLabels.put(key, resolveInfo.loadLabel(mPackageMgr));
|
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
|
|
|
}
|