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