Home | History | Annotate | Download | only in launcher3
      1 package com.android.launcher3;
      2 
      3 import android.appwidget.AppWidgetHostView;
      4 import android.appwidget.AppWidgetProviderInfo;
      5 import android.content.Context;
      6 import android.content.pm.PackageManager;
      7 import android.graphics.Point;
      8 import android.graphics.Rect;
      9 import android.os.Parcel;
     10 
     11 /**
     12  * This class is a thin wrapper around the framework AppWidgetProviderInfo class. This class affords
     13  * a common object for describing both framework provided AppWidgets as well as custom widgets
     14  * (who's implementation is owned by the launcher). This object represents a widget type / class,
     15  * as opposed to a widget instance, and so should not be confused with {@link LauncherAppWidgetInfo}
     16  */
     17 public class LauncherAppWidgetProviderInfo extends AppWidgetProviderInfo {
     18 
     19     public static final String CLS_CUSTOM_WIDGET_PREFIX = "#custom-widget-";
     20 
     21     public int spanX;
     22     public int spanY;
     23     public int minSpanX;
     24     public int minSpanY;
     25 
     26     public static LauncherAppWidgetProviderInfo fromProviderInfo(Context context,
     27             AppWidgetProviderInfo info) {
     28         final LauncherAppWidgetProviderInfo launcherInfo;
     29         if (info instanceof LauncherAppWidgetProviderInfo) {
     30             launcherInfo = (LauncherAppWidgetProviderInfo) info;
     31         } else {
     32 
     33             // In lieu of a public super copy constructor, we first write the AppWidgetProviderInfo
     34             // into a parcel, and then construct a new LauncherAppWidgetProvider info from the
     35             // associated super parcel constructor. This allows us to copy non-public members without
     36             // using reflection.
     37             Parcel p = Parcel.obtain();
     38             info.writeToParcel(p, 0);
     39             p.setDataPosition(0);
     40             launcherInfo = new LauncherAppWidgetProviderInfo(p);
     41             p.recycle();
     42         }
     43         launcherInfo.initSpans(context);
     44         return launcherInfo;
     45     }
     46 
     47     protected LauncherAppWidgetProviderInfo() {}
     48 
     49     protected LauncherAppWidgetProviderInfo(Parcel in) {
     50         super(in);
     51     }
     52 
     53     public void initSpans(Context context) {
     54         InvariantDeviceProfile idp = LauncherAppState.getIDP(context);
     55 
     56         Point paddingLand = idp.landscapeProfile.getTotalWorkspacePadding();
     57         Point paddingPort = idp.portraitProfile.getTotalWorkspacePadding();
     58 
     59         // Always assume we're working with the smallest span to make sure we
     60         // reserve enough space in both orientations.
     61         float smallestCellWidth = DeviceProfile.calculateCellWidth(Math.min(
     62                 idp.landscapeProfile.widthPx - paddingLand.x,
     63                 idp.portraitProfile.widthPx - paddingPort.x),
     64                 idp.numColumns);
     65         float smallestCellHeight = DeviceProfile.calculateCellWidth(Math.min(
     66                 idp.landscapeProfile.heightPx - paddingLand.y,
     67                 idp.portraitProfile.heightPx - paddingPort.y),
     68                 idp.numRows);
     69 
     70         // We want to account for the extra amount of padding that we are adding to the widget
     71         // to ensure that it gets the full amount of space that it has requested.
     72         Rect widgetPadding = AppWidgetHostView.getDefaultPaddingForWidget(
     73                 context, provider, null);
     74         spanX = Math.max(1, (int) Math.ceil(
     75                         (minWidth + widgetPadding.left + widgetPadding.right) / smallestCellWidth));
     76         spanY = Math.max(1, (int) Math.ceil(
     77                 (minHeight + widgetPadding.top + widgetPadding.bottom) / smallestCellHeight));
     78 
     79         minSpanX = Math.max(1, (int) Math.ceil(
     80                 (minResizeWidth + widgetPadding.left + widgetPadding.right) / smallestCellWidth));
     81         minSpanY = Math.max(1, (int) Math.ceil(
     82                 (minResizeHeight + widgetPadding.top + widgetPadding.bottom) / smallestCellHeight));
     83     }
     84 
     85     public String getLabel(PackageManager packageManager) {
     86         return super.loadLabel(packageManager);
     87     }
     88 
     89     public Point getMinSpans() {
     90         return new Point((resizeMode & RESIZE_HORIZONTAL) != 0 ? minSpanX : -1,
     91                 (resizeMode & RESIZE_VERTICAL) != 0 ? minSpanY : -1);
     92     }
     93 
     94     public boolean isCustomWidget() {
     95         return provider.getClassName().startsWith(CLS_CUSTOM_WIDGET_PREFIX);
     96     }
     97 
     98     public int getWidgetFeatures() {
     99         if (Utilities.ATLEAST_P) {
    100             return widgetFeatures;
    101         } else {
    102             return 0;
    103         }
    104     }
    105  }
    106