1 package com.android.wallpaperpicker.tileinfo; 2 3 import android.content.Context; 4 import android.content.res.Resources; 5 import android.graphics.Bitmap; 6 import android.graphics.Matrix; 7 import android.graphics.Point; 8 import android.graphics.RectF; 9 import android.view.LayoutInflater; 10 import android.view.View; 11 import android.view.ViewGroup; 12 13 import com.android.gallery3d.common.Utils; 14 import com.android.wallpaperpicker.R; 15 import com.android.wallpaperpicker.WallpaperPickerActivity; 16 import com.android.wallpaperpicker.common.InputStreamProvider; 17 18 public abstract class WallpaperTileInfo { 19 20 protected View mView; 21 onClick(WallpaperPickerActivity a)22 public void onClick(WallpaperPickerActivity a) {} 23 onSave(WallpaperPickerActivity a)24 public void onSave(WallpaperPickerActivity a) {} 25 onDelete(WallpaperPickerActivity a)26 public void onDelete(WallpaperPickerActivity a) {} 27 isSelectable()28 public boolean isSelectable() { return false; } 29 isNamelessWallpaper()30 public boolean isNamelessWallpaper() { return false; } 31 onIndexUpdated(CharSequence label)32 public void onIndexUpdated(CharSequence label) { 33 if (isNamelessWallpaper()) { 34 mView.setContentDescription(label); 35 } 36 } 37 createView(Context context, LayoutInflater inflator, ViewGroup parent)38 public abstract View createView(Context context, LayoutInflater inflator, ViewGroup parent); 39 getDefaultThumbSize(Resources res)40 protected static Point getDefaultThumbSize(Resources res) { 41 return new Point(res.getDimensionPixelSize(R.dimen.wallpaperThumbnailWidth), 42 res.getDimensionPixelSize(R.dimen.wallpaperThumbnailHeight)); 43 44 } 45 createThumbnail(InputStreamProvider streamProvider, Context context, int rotation, boolean leftAligned)46 protected static Bitmap createThumbnail(InputStreamProvider streamProvider, Context context, 47 int rotation, boolean leftAligned) { 48 Point size = getDefaultThumbSize(context.getResources()); 49 int width = size.x; 50 int height = size.y; 51 Point bounds = streamProvider.getImageBounds(); 52 if (bounds == null) { 53 return null; 54 } 55 56 Matrix rotateMatrix = new Matrix(); 57 rotateMatrix.setRotate(rotation); 58 float[] rotatedBounds = new float[] { bounds.x, bounds.y }; 59 rotateMatrix.mapPoints(rotatedBounds); 60 rotatedBounds[0] = Math.abs(rotatedBounds[0]); 61 rotatedBounds[1] = Math.abs(rotatedBounds[1]); 62 63 RectF cropRect = Utils.getMaxCropRect( 64 (int) rotatedBounds[0], (int) rotatedBounds[1], width, height, leftAligned); 65 return streamProvider.readCroppedBitmap(cropRect, width, height, rotation); 66 } 67 }