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