2014-03-05 18:07:04 -08:00
|
|
|
package com.android.launcher3;
|
|
|
|
|
|
2015-08-03 13:05:01 -07:00
|
|
|
import android.appwidget.AppWidgetHostView;
|
2014-03-05 18:07:04 -08:00
|
|
|
import android.appwidget.AppWidgetProviderInfo;
|
|
|
|
|
import android.content.Context;
|
|
|
|
|
import android.content.pm.PackageManager;
|
2015-07-27 14:36:07 -07:00
|
|
|
import android.graphics.Point;
|
2015-08-03 13:05:01 -07:00
|
|
|
import android.graphics.Rect;
|
2014-03-05 18:07:04 -08:00
|
|
|
import android.os.Parcel;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* This class is a thin wrapper around the framework AppWidgetProviderInfo class. This class affords
|
|
|
|
|
* a common object for describing both framework provided AppWidgets as well as custom widgets
|
|
|
|
|
* (who's implementation is owned by the launcher). This object represents a widget type / class,
|
|
|
|
|
* as opposed to a widget instance, and so should not be confused with {@link LauncherAppWidgetInfo}
|
|
|
|
|
*/
|
|
|
|
|
public class LauncherAppWidgetProviderInfo extends AppWidgetProviderInfo {
|
|
|
|
|
|
2017-08-16 04:59:08 -07:00
|
|
|
public static final String CLS_CUSTOM_WIDGET_PREFIX = "#custom-widget-";
|
2015-05-06 11:42:25 -07:00
|
|
|
|
2015-08-03 13:05:01 -07:00
|
|
|
public int spanX;
|
|
|
|
|
public int spanY;
|
|
|
|
|
public int minSpanX;
|
|
|
|
|
public int minSpanY;
|
2014-03-05 18:07:04 -08:00
|
|
|
|
|
|
|
|
public static LauncherAppWidgetProviderInfo fromProviderInfo(Context context,
|
|
|
|
|
AppWidgetProviderInfo info) {
|
2016-12-29 13:31:43 -08:00
|
|
|
final LauncherAppWidgetProviderInfo launcherInfo;
|
|
|
|
|
if (info instanceof LauncherAppWidgetProviderInfo) {
|
|
|
|
|
launcherInfo = (LauncherAppWidgetProviderInfo) info;
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
|
|
// In lieu of a public super copy constructor, we first write the AppWidgetProviderInfo
|
|
|
|
|
// into a parcel, and then construct a new LauncherAppWidgetProvider info from the
|
|
|
|
|
// associated super parcel constructor. This allows us to copy non-public members without
|
|
|
|
|
// using reflection.
|
|
|
|
|
Parcel p = Parcel.obtain();
|
|
|
|
|
info.writeToParcel(p, 0);
|
|
|
|
|
p.setDataPosition(0);
|
|
|
|
|
launcherInfo = new LauncherAppWidgetProviderInfo(p);
|
|
|
|
|
p.recycle();
|
|
|
|
|
}
|
|
|
|
|
launcherInfo.initSpans(context);
|
|
|
|
|
return launcherInfo;
|
2014-03-05 18:07:04 -08:00
|
|
|
}
|
|
|
|
|
|
2017-08-16 04:59:08 -07:00
|
|
|
protected LauncherAppWidgetProviderInfo() {}
|
2014-03-05 18:07:04 -08:00
|
|
|
|
2017-08-16 04:59:08 -07:00
|
|
|
protected LauncherAppWidgetProviderInfo(Parcel in) {
|
|
|
|
|
super(in);
|
2015-08-03 13:05:01 -07:00
|
|
|
}
|
|
|
|
|
|
2016-12-29 13:31:43 -08:00
|
|
|
public void initSpans(Context context) {
|
2017-01-11 10:48:34 -08:00
|
|
|
InvariantDeviceProfile idp = LauncherAppState.getIDP(context);
|
2015-08-03 13:05:01 -07:00
|
|
|
|
2016-07-06 09:47:56 -07:00
|
|
|
Point paddingLand = idp.landscapeProfile.getTotalWorkspacePadding();
|
|
|
|
|
Point paddingPort = idp.portraitProfile.getTotalWorkspacePadding();
|
2015-08-03 13:05:01 -07:00
|
|
|
|
|
|
|
|
// Always assume we're working with the smallest span to make sure we
|
|
|
|
|
// reserve enough space in both orientations.
|
|
|
|
|
float smallestCellWidth = DeviceProfile.calculateCellWidth(Math.min(
|
2016-07-06 09:47:56 -07:00
|
|
|
idp.landscapeProfile.widthPx - paddingLand.x,
|
|
|
|
|
idp.portraitProfile.widthPx - paddingPort.x),
|
2015-08-03 13:05:01 -07:00
|
|
|
idp.numColumns);
|
|
|
|
|
float smallestCellHeight = DeviceProfile.calculateCellWidth(Math.min(
|
2016-07-06 09:47:56 -07:00
|
|
|
idp.landscapeProfile.heightPx - paddingLand.y,
|
|
|
|
|
idp.portraitProfile.heightPx - paddingPort.y),
|
2015-08-03 13:05:01 -07:00
|
|
|
idp.numRows);
|
|
|
|
|
|
|
|
|
|
// We want to account for the extra amount of padding that we are adding to the widget
|
|
|
|
|
// to ensure that it gets the full amount of space that it has requested.
|
|
|
|
|
Rect widgetPadding = AppWidgetHostView.getDefaultPaddingForWidget(
|
2016-12-29 13:31:43 -08:00
|
|
|
context, provider, null);
|
2015-08-03 13:05:01 -07:00
|
|
|
spanX = Math.max(1, (int) Math.ceil(
|
|
|
|
|
(minWidth + widgetPadding.left + widgetPadding.right) / smallestCellWidth));
|
|
|
|
|
spanY = Math.max(1, (int) Math.ceil(
|
|
|
|
|
(minHeight + widgetPadding.top + widgetPadding.bottom) / smallestCellHeight));
|
|
|
|
|
|
|
|
|
|
minSpanX = Math.max(1, (int) Math.ceil(
|
|
|
|
|
(minResizeWidth + widgetPadding.left + widgetPadding.right) / smallestCellWidth));
|
|
|
|
|
minSpanY = Math.max(1, (int) Math.ceil(
|
|
|
|
|
(minResizeHeight + widgetPadding.top + widgetPadding.bottom) / smallestCellHeight));
|
2014-03-05 18:07:04 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public String getLabel(PackageManager packageManager) {
|
|
|
|
|
return super.loadLabel(packageManager);
|
|
|
|
|
}
|
|
|
|
|
|
2017-08-16 04:59:08 -07:00
|
|
|
public Point getMinSpans() {
|
|
|
|
|
return new Point((resizeMode & RESIZE_HORIZONTAL) != 0 ? minSpanX : -1,
|
|
|
|
|
(resizeMode & RESIZE_VERTICAL) != 0 ? minSpanY : -1);
|
2015-07-27 14:36:07 -07:00
|
|
|
}
|
2016-12-16 15:04:51 -08:00
|
|
|
|
2017-08-16 04:59:08 -07:00
|
|
|
public boolean isCustomWidget() {
|
|
|
|
|
return provider.getClassName().startsWith(CLS_CUSTOM_WIDGET_PREFIX);
|
2016-12-16 15:04:51 -08:00
|
|
|
}
|
2018-03-05 19:39:21 +00:00
|
|
|
|
|
|
|
|
public int getWidgetFeatures() {
|
|
|
|
|
if (Utilities.ATLEAST_P) {
|
|
|
|
|
return widgetFeatures;
|
|
|
|
|
} else {
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
2014-03-05 18:07:04 -08:00
|
|
|
}
|