import android.content.ComponentName;
import android.content.Context;
+import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.util.Log;
import com.android.launcher3.compat.AlphabeticIndexCompat;
import com.android.launcher3.compat.AppWidgetManagerCompat;
import com.android.launcher3.compat.UserHandleCompat;
+import com.android.launcher3.util.ComponentKey;
import java.util.ArrayList;
import java.util.Collections;
/* Map of widgets and shortcuts that are tracked per package. */
private final HashMap<PackageItemInfo, ArrayList<Object>> mWidgetsList;
+ /* Labels of widgets */
+ private final HashMap<ComponentKey, CharSequence> mLabels;
+
private final AppWidgetManagerCompat mAppWidgetMgr;
+ private final PackageManager mPackageMgr;
private final WidgetsAndShortcutNameComparator mWidgetAndShortcutNameComparator;
private final Comparator<ItemInfo> mAppNameComparator;
private final IconCache mIconCache;
public WidgetsModel(Context context, IconCache iconCache, AppFilter appFilter) {
mAppWidgetMgr = AppWidgetManagerCompat.getInstance(context);
+ mPackageMgr = context.getPackageManager();
mWidgetAndShortcutNameComparator = new WidgetsAndShortcutNameComparator(context);
mAppNameComparator = (new AppNameComparator(context)).getAppInfoComparator();
mIconCache = iconCache;
mIndexer = new AlphabeticIndexCompat(context);
mPackageItemInfos = new ArrayList<>();
mWidgetsList = new HashMap<>();
-
mRawList = new ArrayList<>();
+ mLabels = new HashMap<>();
}
@SuppressWarnings("unchecked")
private WidgetsModel(WidgetsModel model) {
mAppWidgetMgr = model.mAppWidgetMgr;
+ mPackageMgr = model.mPackageMgr;
mPackageItemInfos = (ArrayList<PackageItemInfo>) model.mPackageItemInfos.clone();
mWidgetsList = (HashMap<PackageItemInfo, ArrayList<Object>>) model.mWidgetsList.clone();
mWidgetAndShortcutNameComparator = model.mWidgetAndShortcutNameComparator;
mAppFilter = model.mAppFilter;
mIndexer = model.mIndexer;
mRawList = (ArrayList<Object>) model.mRawList.clone();
+ mLabels = (HashMap<ComponentKey, CharSequence>) model.mLabels.clone();
}
// Access methods that may be deleted if the private fields are made package-private.
return mPackageItemInfos.get(pos);
}
+ 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;
+ }
+
public List<Object> getSortedWidgets(int pos) {
return mWidgetsList.get(mPackageItemInfos.get(pos));
}
// clear the lists.
mWidgetsList.clear();
mPackageItemInfos.clear();
+ mLabels.clear();
mWidgetAndShortcutNameComparator.reset();
InvariantDeviceProfile idp = LauncherAppState.getInstance().getInvariantDeviceProfile();
String packageName = "";
UserHandleCompat userHandle = null;
ComponentName componentName = null;
+ ComponentKey key = null;
if (o instanceof LauncherAppWidgetProviderInfo) {
LauncherAppWidgetProviderInfo widgetInfo = (LauncherAppWidgetProviderInfo) o;
}
continue;
}
+ key = new ComponentKey(componentName, userHandle);
+ mLabels.put(key, mAppWidgetMgr.loadLabel(widgetInfo));
} else if (o instanceof ResolveInfo) {
ResolveInfo resolveInfo = (ResolveInfo) o;
componentName = new ComponentName(resolveInfo.activityInfo.packageName,
resolveInfo.activityInfo.name);
packageName = resolveInfo.activityInfo.packageName;
userHandle = UserHandleCompat.myUserHandle();
+ key = new ComponentKey(componentName, userHandle);
+ mLabels.put(key, resolveInfo.loadLabel(mPackageMgr));
}
if (componentName == null || userHandle == null) {
}
/**
- * Apply the widget provider info to the view.
+ * Apply the widget provider info or the resolve info to the view.
*/
- public void applyFromAppWidgetProviderInfo(LauncherAppWidgetProviderInfo info,
- WidgetPreviewLoader loader) {
-
- InvariantDeviceProfile profile =
- LauncherAppState.getInstance().getInvariantDeviceProfile();
+ public void applyFromInfo(Object info, CharSequence label,
+ WidgetPreviewLoader loader) {
mInfo = info;
- // TODO(hyunyoungs): setup a cache for these labels.
- mWidgetName.setText(AppWidgetManagerCompat.getInstance(getContext()).loadLabel(info));
- int hSpan = Math.min(info.spanX, profile.numColumns);
- int vSpan = Math.min(info.spanY, profile.numRows);
- mWidgetDims.setText(String.format(mDimensionsFormatString, hSpan, vSpan));
- mWidgetPreviewLoader = loader;
- }
-
- /**
- * Apply the resolve info to the view.
- */
- public void applyFromResolveInfo(
- PackageManager pm, ResolveInfo info, WidgetPreviewLoader loader) {
- mInfo = info;
- CharSequence label = info.loadLabel(pm);
mWidgetName.setText(label);
- mWidgetDims.setText(String.format(mDimensionsFormatString, 1, 1));
mWidgetPreviewLoader = loader;
+ if (info instanceof LauncherAppWidgetProviderInfo) {
+ InvariantDeviceProfile profile =
+ LauncherAppState.getInstance().getInvariantDeviceProfile();
+ int hSpan = Math.min(((LauncherAppWidgetProviderInfo)info).spanX, profile.numColumns);
+ int vSpan = Math.min(((LauncherAppWidgetProviderInfo)info).spanY, profile.numRows);
+ mWidgetDims.setText(String.format(mDimensionsFormatString, hSpan, vSpan));
+ }
+ if (info instanceof ResolveInfo) {
+ mWidgetDims.setText(String.format(mDimensionsFormatString, 1, 1));
+ }
}
public int[] getPreviewSize() {
package com.android.launcher3.widget;
import android.annotation.TargetApi;
-import android.content.Context;
import android.content.pm.ResolveInfo;
-import android.content.res.Resources;
import android.os.Build;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.RecyclerView.Adapter;
import android.widget.LinearLayout;
import com.android.launcher3.BubbleTextView;
-import com.android.launcher3.DeviceProfile;
import com.android.launcher3.Launcher;
import com.android.launcher3.LauncherAppState;
import com.android.launcher3.LauncherAppWidgetProviderInfo;
}
for (int i=0; i < infoList.size(); i++) {
WidgetCell widget = (WidgetCell) row.getChildAt(i);
- if (infoList.get(i) instanceof LauncherAppWidgetProviderInfo) {
- LauncherAppWidgetProviderInfo info = (LauncherAppWidgetProviderInfo) infoList.get(i);
- PendingAddWidgetInfo pawi = new PendingAddWidgetInfo(mLauncher, info, null);
- widget.setTag(pawi);
- widget.applyFromAppWidgetProviderInfo(info, mWidgetPreviewLoader);
- } else if (infoList.get(i) instanceof ResolveInfo) {
- ResolveInfo info = (ResolveInfo) infoList.get(i);
- PendingAddShortcutInfo pasi = new PendingAddShortcutInfo(info.activityInfo);
- widget.setTag(pasi);
- widget.applyFromResolveInfo(mLauncher.getPackageManager(), info, mWidgetPreviewLoader);
+ Object info = infoList.get(i);
+ if (info instanceof LauncherAppWidgetProviderInfo) {
+ widget.setTag(new PendingAddWidgetInfo(mLauncher, (LauncherAppWidgetProviderInfo)info, null));
+ } else if (info instanceof ResolveInfo) {
+ widget.setTag(new PendingAddShortcutInfo(((ResolveInfo) info).activityInfo));
}
+ widget.applyFromInfo(info, mWidgetsModel.getLabel(info), mWidgetPreviewLoader);
widget.ensurePreview();
widget.setVisibility(View.VISIBLE);
}