WallpaperTileInfo.java revision 748dfe025b128ea88ba626c8bfb7da315d5af29c
1package com.android.wallpaperpicker.tileinfo;
2
3import android.content.Context;
4import android.content.res.Resources;
5import android.graphics.Bitmap;
6import android.graphics.Matrix;
7import android.graphics.Point;
8import android.graphics.RectF;
9import android.net.Uri;
10import android.view.LayoutInflater;
11import android.view.View;
12import android.view.ViewGroup;
13
14import com.android.gallery3d.common.BitmapCropTask;
15import com.android.gallery3d.common.Utils;
16import com.android.wallpaperpicker.R;
17import com.android.wallpaperpicker.WallpaperPickerActivity;
18
19public abstract class WallpaperTileInfo {
20
21    protected View mView;
22
23    public void onClick(WallpaperPickerActivity a) {}
24
25    public void onSave(WallpaperPickerActivity a) {}
26
27    public void onDelete(WallpaperPickerActivity a) {}
28
29    public boolean isSelectable() { return false; }
30
31    public boolean isNamelessWallpaper() { return false; }
32
33    public void onIndexUpdated(CharSequence label) {
34        if (isNamelessWallpaper()) {
35            mView.setContentDescription(label);
36        }
37    }
38
39    public abstract View createView(Context context, LayoutInflater inflator, ViewGroup parent);
40
41    protected static Point getDefaultThumbSize(Resources res) {
42        return new Point(res.getDimensionPixelSize(R.dimen.wallpaperThumbnailWidth),
43                res.getDimensionPixelSize(R.dimen.wallpaperThumbnailHeight));
44
45    }
46
47    protected static Bitmap createThumbnail(Context context, Uri uri, byte[] imageBytes,
48            Resources res, int resId, int rotation, boolean leftAligned) {
49        Point size = getDefaultThumbSize(context.getResources());
50        int width = size.x;
51        int height = size.y;
52
53        BitmapCropTask cropTask;
54        if (uri != null) {
55            cropTask = new BitmapCropTask(
56                    context, uri, null, rotation, width, height, false, true, null);
57        } else if (imageBytes != null) {
58            cropTask = new BitmapCropTask(
59                    imageBytes, null, rotation, width, height, false, true, null);
60        }  else {
61            cropTask = new BitmapCropTask(
62                    context, res, resId, null, rotation, width, height, false, true, null);
63        }
64        Point bounds = cropTask.getImageBounds();
65        if (bounds == null || bounds.x == 0 || bounds.y == 0) {
66            return null;
67        }
68
69        Matrix rotateMatrix = new Matrix();
70        rotateMatrix.setRotate(rotation);
71        float[] rotatedBounds = new float[] { bounds.x, bounds.y };
72        rotateMatrix.mapPoints(rotatedBounds);
73        rotatedBounds[0] = Math.abs(rotatedBounds[0]);
74        rotatedBounds[1] = Math.abs(rotatedBounds[1]);
75
76        RectF cropRect = Utils.getMaxCropRect(
77                (int) rotatedBounds[0], (int) rotatedBounds[1], width, height, leftAligned);
78        cropTask.setCropBounds(cropRect);
79
80        if (cropTask.cropBitmap()) {
81            return cropTask.getCroppedBitmap();
82        } else {
83            return null;
84        }
85    }
86}