1 package com.android.launcher3.model; 2 3 import android.content.Context; 4 import android.content.pm.ActivityInfo; 5 6 import com.android.launcher3.InvariantDeviceProfile; 7 import com.android.launcher3.Utilities; 8 import com.android.launcher3.icons.BitmapInfo; 9 import com.android.launcher3.icons.IconCache; 10 import com.android.launcher3.pm.ShortcutConfigActivityInfo; 11 import com.android.launcher3.util.ComponentKey; 12 import com.android.launcher3.widget.LauncherAppWidgetProviderInfo; 13 14 /** 15 * An wrapper over various items displayed in a widget picker, 16 * {@link LauncherAppWidgetProviderInfo} & {@link ActivityInfo}. This provides easier access to 17 * common attributes like spanX and spanY. 18 */ 19 public class WidgetItem extends ComponentKey { 20 21 public final LauncherAppWidgetProviderInfo widgetInfo; 22 public final ShortcutConfigActivityInfo activityInfo; 23 24 public BitmapInfo bitmap = BitmapInfo.LOW_RES_INFO; 25 public final String label; 26 public final CharSequence description; 27 public final int spanX, spanY; 28 WidgetItem(LauncherAppWidgetProviderInfo info, InvariantDeviceProfile idp, IconCache iconCache, Context context)29 public WidgetItem(LauncherAppWidgetProviderInfo info, 30 InvariantDeviceProfile idp, IconCache iconCache, Context context) { 31 super(info.provider, info.getProfile()); 32 33 label = iconCache.getTitleNoCache(info); 34 description = info.loadDescription(context); 35 widgetInfo = info; 36 activityInfo = null; 37 38 spanX = Math.min(info.spanX, idp.numColumns); 39 spanY = Math.min(info.spanY, idp.numRows); 40 } 41 WidgetItem(ShortcutConfigActivityInfo info, IconCache iconCache)42 public WidgetItem(ShortcutConfigActivityInfo info, IconCache iconCache) { 43 super(info.getComponent(), info.getUser()); 44 label = info.isPersistable() ? iconCache.getTitleNoCache(info) : 45 Utilities.trim(info.getLabel()); 46 description = null; 47 widgetInfo = null; 48 activityInfo = info; 49 spanX = spanY = 1; 50 } 51 52 /** 53 * Returns {@code true} if this {@link WidgetItem} has the same type as the given 54 * {@code otherItem}. 55 * 56 * For example, both items are widgets or both items are shortcuts. 57 */ hasSameType(WidgetItem otherItem)58 public boolean hasSameType(WidgetItem otherItem) { 59 if (widgetInfo != null && otherItem.widgetInfo != null) { 60 return true; 61 } 62 if (activityInfo != null && otherItem.activityInfo != null) { 63 return true; 64 } 65 return false; 66 } 67 68 /** Returns whether this {@link WidgetItem} is for a shortcut rather than an app widget. */ isShortcut()69 public boolean isShortcut() { 70 return activityInfo != null; 71 } 72 } 73