2016-03-10 12:02:29 -08:00
|
|
|
package com.android.launcher3.model;
|
|
|
|
|
|
2023-11-07 22:31:46 +00:00
|
|
|
import static android.appwidget.AppWidgetProviderInfo.WIDGET_CATEGORY_HOME_SCREEN;
|
|
|
|
|
import static android.appwidget.AppWidgetProviderInfo.WIDGET_CATEGORY_KEYGUARD;
|
|
|
|
|
import static android.appwidget.AppWidgetProviderInfo.WIDGET_CATEGORY_SEARCHBOX;
|
|
|
|
|
|
2021-06-14 14:55:07 +00:00
|
|
|
import static com.android.launcher3.Utilities.ATLEAST_S;
|
|
|
|
|
|
|
|
|
|
import android.annotation.SuppressLint;
|
2023-05-05 10:31:17 -07:00
|
|
|
import android.content.Context;
|
2016-03-10 12:02:29 -08:00
|
|
|
import android.content.pm.ActivityInfo;
|
|
|
|
|
import android.content.pm.PackageManager;
|
2021-06-14 14:55:07 +00:00
|
|
|
import android.content.res.Resources;
|
2023-11-07 22:31:46 +00:00
|
|
|
import android.util.SparseArray;
|
|
|
|
|
import android.widget.RemoteViews;
|
|
|
|
|
|
|
|
|
|
import androidx.core.os.BuildCompat;
|
2016-03-10 12:02:29 -08:00
|
|
|
|
2023-11-07 22:31:46 +00:00
|
|
|
import com.android.launcher3.Flags;
|
2016-03-10 12:02:29 -08:00
|
|
|
import com.android.launcher3.InvariantDeviceProfile;
|
|
|
|
|
import com.android.launcher3.Utilities;
|
2024-02-12 11:04:20 -08:00
|
|
|
import com.android.launcher3.icons.BitmapInfo;
|
2018-09-26 12:00:30 -07:00
|
|
|
import com.android.launcher3.icons.IconCache;
|
2019-10-02 16:13:34 -07:00
|
|
|
import com.android.launcher3.pm.ShortcutConfigActivityInfo;
|
2016-03-10 12:02:29 -08:00
|
|
|
import com.android.launcher3.util.ComponentKey;
|
2021-02-22 14:03:44 +00:00
|
|
|
import com.android.launcher3.widget.LauncherAppWidgetProviderInfo;
|
2023-11-07 22:31:46 +00:00
|
|
|
import com.android.launcher3.widget.WidgetManagerHelper;
|
2016-03-10 12:02:29 -08:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* An wrapper over various items displayed in a widget picker,
|
|
|
|
|
* {@link LauncherAppWidgetProviderInfo} & {@link ActivityInfo}. This provides easier access to
|
|
|
|
|
* common attributes like spanX and spanY.
|
|
|
|
|
*/
|
2019-12-09 14:55:56 -08:00
|
|
|
public class WidgetItem extends ComponentKey {
|
2016-03-10 12:02:29 -08:00
|
|
|
|
|
|
|
|
public final LauncherAppWidgetProviderInfo widgetInfo;
|
2017-01-19 10:27:54 -08:00
|
|
|
public final ShortcutConfigActivityInfo activityInfo;
|
2016-03-10 12:02:29 -08:00
|
|
|
|
2024-02-12 11:04:20 -08:00
|
|
|
public BitmapInfo bitmap = BitmapInfo.LOW_RES_INFO;
|
2016-03-10 12:02:29 -08:00
|
|
|
public final String label;
|
2023-05-05 10:31:17 -07:00
|
|
|
public final CharSequence description;
|
2016-03-10 12:02:29 -08:00
|
|
|
public final int spanX, spanY;
|
2023-11-07 22:31:46 +00:00
|
|
|
public final SparseArray<RemoteViews> generatedPreviews;
|
2016-03-10 12:02:29 -08:00
|
|
|
|
2018-09-26 12:00:30 -07:00
|
|
|
public WidgetItem(LauncherAppWidgetProviderInfo info,
|
2023-11-07 22:31:46 +00:00
|
|
|
InvariantDeviceProfile idp, IconCache iconCache, Context context,
|
|
|
|
|
WidgetManagerHelper helper) {
|
2016-12-16 15:04:51 -08:00
|
|
|
super(info.provider, info.getProfile());
|
2016-03-10 12:02:29 -08:00
|
|
|
|
2018-09-26 12:00:30 -07:00
|
|
|
label = iconCache.getTitleNoCache(info);
|
2023-05-05 10:31:17 -07:00
|
|
|
description = ATLEAST_S ? info.loadDescription(context) : null;
|
2016-03-10 12:02:29 -08:00
|
|
|
widgetInfo = info;
|
|
|
|
|
activityInfo = null;
|
|
|
|
|
|
2017-01-11 10:48:34 -08:00
|
|
|
spanX = Math.min(info.spanX, idp.numColumns);
|
|
|
|
|
spanY = Math.min(info.spanY, idp.numRows);
|
2023-11-07 22:31:46 +00:00
|
|
|
|
|
|
|
|
if (BuildCompat.isAtLeastV() && Flags.enableGeneratedPreviews()) {
|
|
|
|
|
generatedPreviews = new SparseArray<>(3);
|
|
|
|
|
for (int widgetCategory : new int[] {
|
|
|
|
|
WIDGET_CATEGORY_HOME_SCREEN,
|
|
|
|
|
WIDGET_CATEGORY_KEYGUARD,
|
|
|
|
|
WIDGET_CATEGORY_SEARCHBOX,
|
|
|
|
|
}) {
|
|
|
|
|
if ((widgetCategory & widgetInfo.generatedPreviewCategories) != 0) {
|
|
|
|
|
generatedPreviews.put(widgetCategory,
|
|
|
|
|
helper.loadGeneratedPreview(widgetInfo, widgetCategory));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
generatedPreviews = null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public WidgetItem(LauncherAppWidgetProviderInfo info,
|
|
|
|
|
InvariantDeviceProfile idp, IconCache iconCache, Context context) {
|
|
|
|
|
this(info, idp, iconCache, context, new WidgetManagerHelper(context));
|
2016-03-10 12:02:29 -08:00
|
|
|
}
|
|
|
|
|
|
2018-09-26 12:00:30 -07:00
|
|
|
public WidgetItem(ShortcutConfigActivityInfo info, IconCache iconCache, PackageManager pm) {
|
2017-01-19 10:27:54 -08:00
|
|
|
super(info.getComponent(), info.getUser());
|
2018-09-26 12:00:30 -07:00
|
|
|
label = info.isPersistable() ? iconCache.getTitleNoCache(info) :
|
|
|
|
|
Utilities.trim(info.getLabel(pm));
|
2023-05-05 10:31:17 -07:00
|
|
|
description = null;
|
2016-03-10 12:02:29 -08:00
|
|
|
widgetInfo = null;
|
2017-01-19 10:27:54 -08:00
|
|
|
activityInfo = info;
|
2016-03-10 12:02:29 -08:00
|
|
|
spanX = spanY = 1;
|
2023-11-07 22:31:46 +00:00
|
|
|
generatedPreviews = null;
|
2016-03-10 12:02:29 -08:00
|
|
|
}
|
2021-02-19 21:29:18 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Returns {@code true} if this {@link WidgetItem} has the same type as the given
|
|
|
|
|
* {@code otherItem}.
|
|
|
|
|
*
|
|
|
|
|
* For example, both items are widgets or both items are shortcuts.
|
|
|
|
|
*/
|
|
|
|
|
public boolean hasSameType(WidgetItem otherItem) {
|
|
|
|
|
if (widgetInfo != null && otherItem.widgetInfo != null) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
if (activityInfo != null && otherItem.activityInfo != null) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2021-06-14 14:55:07 +00:00
|
|
|
|
|
|
|
|
/** Returns whether this {@link WidgetItem} has a preview layout that can be used. */
|
|
|
|
|
@SuppressLint("NewApi") // Already added API check.
|
|
|
|
|
public boolean hasPreviewLayout() {
|
|
|
|
|
return ATLEAST_S && widgetInfo != null && widgetInfo.previewLayout != Resources.ID_NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** Returns whether this {@link WidgetItem} is for a shortcut rather than an app widget. */
|
|
|
|
|
public boolean isShortcut() {
|
|
|
|
|
return activityInfo != null;
|
|
|
|
|
}
|
2023-11-07 22:31:46 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Returns whether this {@link WidgetItem} has a generated preview for the given widget
|
|
|
|
|
* category.
|
|
|
|
|
*/
|
|
|
|
|
public boolean hasGeneratedPreview(int widgetCategory) {
|
|
|
|
|
if (!Flags.enableGeneratedPreviews() || generatedPreviews == null) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2024-03-08 22:03:00 +00:00
|
|
|
return generatedPreviews.contains(widgetCategory)
|
|
|
|
|
&& generatedPreviews.get(widgetCategory) != null;
|
2023-11-07 22:31:46 +00:00
|
|
|
}
|
2016-03-10 12:02:29 -08:00
|
|
|
}
|