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.ComponentName;
      6 import android.content.Context;
      7 import android.content.pm.PackageManager;
      8 import android.graphics.Point;
      9 import android.graphics.Rect;
     10 import android.graphics.drawable.Drawable;
     11 import android.os.Parcel;
     12 import android.os.Process;
     13 import android.os.UserHandle;
     14 
     15 /**
     16  * This class is a thin wrapper around the framework AppWidgetProviderInfo class. This class affords
     17  * a common object for describing both framework provided AppWidgets as well as custom widgets
     18  * (who's implementation is owned by the launcher). This object represents a widget type / class,
     19  * as opposed to a widget instance, and so should not be confused with {@link LauncherAppWidgetInfo}
     20  */
     21 public class LauncherAppWidgetProviderInfo extends AppWidgetProviderInfo {
     22 
     23     public boolean isCustomWidget = false;
     24 
     25     public int spanX;
     26     public int spanY;
     27     public int minSpanX;
     28     public int minSpanY;
     29 
     30     public static LauncherAppWidgetProviderInfo fromProviderInfo(Context context,
     31             AppWidgetProviderInfo info) {
     32         final LauncherAppWidgetProviderInfo launcherInfo;
     33         if (info instanceof LauncherAppWidgetProviderInfo) {
     34             launcherInfo = (LauncherAppWidgetProviderInfo) info;
     35         } else {
     36 
     37             // In lieu of a public super copy constructor, we first write the AppWidgetProviderInfo
     38             // into a parcel, and then construct a new LauncherAppWidgetProvider info from the
     39             // associated super parcel constructor. This allows us to copy non-public members without
     40             // using reflection.
     41             Parcel p = Parcel.obtain();
     42             info.writeToParcel(p, 0);
     43             p.setDataPosition(0);
     44             launcherInfo = new LauncherAppWidgetProviderInfo(p);
     45             p.recycle();
     46         }
     47         launcherInfo.initSpans(context);
     48         return launcherInfo;
     49     }
     50 
     51     private LauncherAppWidgetProviderInfo(Parcel in) {
     52         super(in);
     53     }
     54 
     55     public LauncherAppWidgetProviderInfo(Context context, CustomAppWidget widget) {
     56         isCustomWidget = true;
     57 
     58         provider = new ComponentName(context, widget.getClass().getName());
     59         icon = widget.getIcon();
     60         label = widget.getLabel();
     61         previewImage = widget.getPreviewImage();
     62         initialLayout = widget.getWidgetLayout();
     63         resizeMode = widget.getResizeMode();
     64         initSpans(context);
     65     }
     66 
     67     public void initSpans(Context context) {
     68         InvariantDeviceProfile idp = LauncherAppState.getIDP(context);
     69 
     70         Point paddingLand = idp.landscapeProfile.getTotalWorkspacePadding();
     71         Point paddingPort = idp.portraitProfile.getTotalWorkspacePadding();
     72 
     73         // Always assume we're working with the smallest span to make sure we
     74         // reserve enough space in both orientations.
     75         float smallestCellWidth = DeviceProfile.calculateCellWidth(Math.min(
     76                 idp.landscapeProfile.widthPx - paddingLand.x,
     77                 idp.portraitProfile.widthPx - paddingPort.x),
     78                 idp.numColumns);
     79         float smallestCellHeight = DeviceProfile.calculateCellWidth(Math.min(
     80                 idp.landscapeProfile.heightPx - paddingLand.y,
     81                 idp.portraitProfile.heightPx - paddingPort.y),
     82                 idp.numRows);
     83 
     84         // We want to account for the extra amount of padding that we are adding to the widget
     85         // to ensure that it gets the full amount of space that it has requested.
     86         Rect widgetPadding = AppWidgetHostView.getDefaultPaddingForWidget(
     87                 context, provider, null);
     88         spanX = Math.max(1, (int) Math.ceil(
     89                         (minWidth + widgetPadding.left + widgetPadding.right) / smallestCellWidth));
     90         spanY = Math.max(1, (int) Math.ceil(
     91                 (minHeight + widgetPadding.top + widgetPadding.bottom) / smallestCellHeight));
     92 
     93         minSpanX = Math.max(1, (int) Math.ceil(
     94                 (minResizeWidth + widgetPadding.left + widgetPadding.right) / smallestCellWidth));
     95         minSpanY = Math.max(1, (int) Math.ceil(
     96                 (minResizeHeight + widgetPadding.top + widgetPadding.bottom) / smallestCellHeight));
     97     }
     98 
     99     public String getLabel(PackageManager packageManager) {
    100         if (isCustomWidget) {
    101             return Utilities.trim(label);
    102         }
    103         return super.loadLabel(packageManager);
    104     }
    105 
    106     public Drawable getIcon(Context context, IconCache cache) {
    107         if (isCustomWidget) {
    108             return cache.getFullResIcon(provider.getPackageName(), icon);
    109         }
    110         return super.loadIcon(context, LauncherAppState.getIDP(context).fillResIconDpi);
    111     }
    112 
    113     public String toString(PackageManager pm) {
    114         if (isCustomWidget) {
    115             return "WidgetProviderInfo(" + provider + ")";
    116         }
    117         return String.format("WidgetProviderInfo provider:%s package:%s short:%s label:%s",
    118                 provider.toString(), provider.getPackageName(), provider.getShortClassName(), getLabel(pm));
    119     }
    120 
    121     public Point getMinSpans(InvariantDeviceProfile idp, Context context) {
    122         return new Point(
    123                 (resizeMode & RESIZE_HORIZONTAL) != 0 ? minSpanX : -1,
    124                         (resizeMode & RESIZE_VERTICAL) != 0 ? minSpanY : -1);
    125     }
    126 
    127     public UserHandle getUser() {
    128         return isCustomWidget ? Process.myUserHandle() : getProfile();
    129     }
    130  }
    131