diff options
Diffstat (limited to 'src/com/android/launcher2/WidgetListAdapter.java')
-rw-r--r-- | src/com/android/launcher2/WidgetListAdapter.java | 74 |
1 files changed, 56 insertions, 18 deletions
diff --git a/src/com/android/launcher2/WidgetListAdapter.java b/src/com/android/launcher2/WidgetListAdapter.java index 5d5d86a67..597ecf947 100644 --- a/src/com/android/launcher2/WidgetListAdapter.java +++ b/src/com/android/launcher2/WidgetListAdapter.java @@ -22,29 +22,36 @@ import android.appwidget.AppWidgetManager; import android.appwidget.AppWidgetProviderInfo; import android.content.Context; import android.content.pm.PackageManager; +import android.content.res.Resources; +import android.graphics.Bitmap; +import android.graphics.Bitmap.Config; +import android.graphics.Canvas; +import android.graphics.Rect; +import android.graphics.Region.Op; +import android.graphics.drawable.BitmapDrawable; import android.graphics.drawable.Drawable; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; -import android.widget.ImageView; -import android.widget.LinearLayout; import android.widget.TextView; import java.util.List; public class WidgetListAdapter extends BaseAdapter { - private LayoutInflater mLayoutInflater; - private PackageManager mPackageManager; + private final Launcher mLauncher; private List<AppWidgetProviderInfo> mWidgets; + private final Canvas mCanvas = new Canvas(); + + private final int[] mTempSize = new int[2]; + private final Rect mTempRect = new Rect(); + private static final String TAG = "Launcher.WidgetGalleryAdapter"; - WidgetListAdapter(Context context) { - mLayoutInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); - AppWidgetManager widgetManager = AppWidgetManager.getInstance(context); - mWidgets = widgetManager.getInstalledProviders(); - mPackageManager = context.getPackageManager(); + WidgetListAdapter(Launcher launcher) { + mLauncher = launcher; + mWidgets = AppWidgetManager.getInstance(launcher).getInstalledProviders(); } public int getCount() { @@ -63,27 +70,58 @@ public class WidgetListAdapter extends BaseAdapter { TextView textView; if (convertView == null) { - textView = (TextView) mLayoutInflater.inflate( + LayoutInflater inflater = + (LayoutInflater)mLauncher.getSystemService(Context.LAYOUT_INFLATER_SERVICE); + textView = (TextView) inflater.inflate( R.layout.home_customization_drawer_item, parent, false); } else { textView = (TextView) convertView; } AppWidgetProviderInfo info = mWidgets.get(position); - Drawable image = null; + PackageManager packageManager = mLauncher.getPackageManager(); + String packageName = info.provider.getPackageName(); + Drawable drawable = null; if (info.previewImage != 0) { - image = mPackageManager.getDrawable( - info.provider.getPackageName(), info.previewImage, null); - if (image == null) { + drawable = packageManager.getDrawable(packageName, info.previewImage, null); + if (drawable == null) { Log.w(TAG, "Can't load icon drawable 0x" + Integer.toHexString(info.icon) + " for provider: " + info.provider); } } - if (image == null) { - image = mPackageManager.getDrawable(info.provider.getPackageName(), info.icon, null); + // If we don't have a preview image, create a default one + if (drawable == null) { + Resources resources = mLauncher.getResources(); + + // Determine the size the widget will take in the layout + mLauncher.getWorkspace().estimateChildSize(info.minWidth, info.minHeight, mTempSize); + + // Create a new bitmap to hold the widget preview + final int width = mTempSize[0]; + final int height = mTempSize[1]; + Bitmap bitmap = Bitmap.createBitmap(width, height, Config.ARGB_8888); + mCanvas.setBitmap(bitmap); + // For some reason, we must re-set the clip rect here, otherwise it will be wrong + mCanvas.clipRect(0, 0, width, height, Op.REPLACE); + + Drawable background = resources.getDrawable(R.drawable.default_widget_preview); + background.setBounds(0, 0, width, height); + background.draw(mCanvas); + + // Draw the icon vertically centered, flush left + Drawable icon = packageManager.getDrawable(packageName, info.icon, null); + background.getPadding(mTempRect); + + final int left = mTempRect.left; + final int top = (height - icon.getIntrinsicHeight()) / 2; + icon.setBounds( + left, top, left + icon.getIntrinsicWidth(), top + icon.getIntrinsicHeight()); + icon.draw(mCanvas); + + drawable = new BitmapDrawable(resources, bitmap); } - image.setBounds(0, 0, image.getIntrinsicWidth(), image.getIntrinsicHeight()); - textView.setCompoundDrawables(null, image, null, null); + drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight()); + textView.setCompoundDrawables(null, drawable, null, null); textView.setText(info.label); // Store the widget info on the associated view so we can easily fetch it later textView.setTag(info); |