Home | History | Annotate | Download | only in launcher3
      1 package com.android.launcher3;
      2 
      3 import android.annotation.TargetApi;
      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.drawable.Drawable;
      9 import android.os.Build;
     10 import android.os.Parcel;
     11 
     12 /**
     13  * This class is a thin wrapper around the framework AppWidgetProviderInfo class. This class affords
     14  * a common object for describing both framework provided AppWidgets as well as custom widgets
     15  * (who's implementation is owned by the launcher). This object represents a widget type / class,
     16  * as opposed to a widget instance, and so should not be confused with {@link LauncherAppWidgetInfo}
     17  */
     18 public class LauncherAppWidgetProviderInfo extends AppWidgetProviderInfo {
     19 
     20     public boolean isCustomWidget = false;
     21 
     22     private int mSpanX = -1;
     23     private int mSpanY = -1;
     24     private int mMinSpanX = -1;
     25     private int mMinSpanY = -1;
     26 
     27     public static LauncherAppWidgetProviderInfo fromProviderInfo(Context context,
     28             AppWidgetProviderInfo info) {
     29 
     30         // In lieu of a public super copy constructor, we first write the AppWidgetProviderInfo
     31         // into a parcel, and then construct a new LauncherAppWidgetProvider info from the
     32         // associated super parcel constructor. This allows us to copy non-public members without
     33         // using reflection.
     34         Parcel p = Parcel.obtain();
     35         info.writeToParcel(p, 0);
     36         p.setDataPosition(0);
     37         LauncherAppWidgetProviderInfo lawpi = new LauncherAppWidgetProviderInfo(p);
     38         p.recycle();
     39         return lawpi;
     40     }
     41 
     42     public LauncherAppWidgetProviderInfo(Parcel in) {
     43         super(in);
     44     }
     45 
     46     public LauncherAppWidgetProviderInfo(Context context, CustomAppWidget widget) {
     47         isCustomWidget = true;
     48 
     49         provider = new ComponentName(context, widget.getClass().getName());
     50         icon = widget.getIcon();
     51         label = widget.getLabel();
     52         previewImage = widget.getPreviewImage();
     53         initialLayout = widget.getWidgetLayout();
     54         resizeMode = widget.getResizeMode();
     55     }
     56 
     57     @TargetApi(Build.VERSION_CODES.LOLLIPOP)
     58     public String getLabel(PackageManager packageManager) {
     59         if (isCustomWidget) {
     60             return Utilities.trim(label);
     61         }
     62         return super.loadLabel(packageManager);
     63     }
     64 
     65     @TargetApi(Build.VERSION_CODES.LOLLIPOP)
     66     public Drawable getIcon(Context context, IconCache cache) {
     67         if (isCustomWidget) {
     68             return cache.getFullResIcon(provider.getPackageName(), icon);
     69         }
     70         return super.loadIcon(context,
     71                 LauncherAppState.getInstance().getInvariantDeviceProfile().fillResIconDpi);
     72     }
     73 
     74     public String toString(PackageManager pm) {
     75         if (isCustomWidget) {
     76             return "WidgetProviderInfo(" + provider + ")";
     77         }
     78         return String.format("WidgetProviderInfo provider:%s package:%s short:%s label:%s",
     79                 provider.toString(), provider.getPackageName(), provider.getShortClassName(), getLabel(pm));
     80     }
     81 
     82     public int getSpanX(Launcher launcher) {
     83         lazyLoadSpans(launcher);
     84         return mSpanX;
     85     }
     86 
     87     public int getSpanY(Launcher launcher) {
     88         lazyLoadSpans(launcher);
     89         return mSpanY;
     90     }
     91 
     92     public int getMinSpanX(Launcher launcher) {
     93         lazyLoadSpans(launcher);
     94         return mMinSpanX;
     95     }
     96 
     97     public int getMinSpanY(Launcher launcher) {
     98         lazyLoadSpans(launcher);
     99         return mMinSpanY;
    100     }
    101 
    102     private void lazyLoadSpans(Launcher launcher) {
    103         if (mSpanX < 0 || mSpanY < 0 || mMinSpanX < 0 || mMinSpanY < 0) {
    104             int[] minResizeSpan = launcher.getMinSpanForWidget(this);
    105             int[] span = launcher.getSpanForWidget(this);
    106 
    107             mSpanX = span[0];
    108             mSpanY = span[1];
    109             mMinSpanX = minResizeSpan[0];
    110             mMinSpanY = minResizeSpan[1];
    111         }
    112     }
    113  }
    114