Action.java revision 55a0279ee420c649606edd7815403d3ca1820c07
1/*
2 * Copyright (C) 2013 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.gallery3d.filtershow.category;
18
19import android.content.Context;
20import android.graphics.Bitmap;
21import android.graphics.BitmapFactory;
22import android.graphics.Canvas;
23import android.graphics.Matrix;
24import android.graphics.Paint;
25import android.graphics.Rect;
26import android.graphics.RectF;
27import android.widget.ArrayAdapter;
28import android.widget.ListAdapter;
29import com.android.gallery3d.filtershow.filters.FilterUserPresetRepresentation;
30import com.android.gallery3d.filtershow.pipeline.RenderingRequest;
31import com.android.gallery3d.filtershow.pipeline.RenderingRequestCaller;
32import com.android.gallery3d.filtershow.filters.FilterRepresentation;
33import com.android.gallery3d.filtershow.imageshow.MasterImage;
34import com.android.gallery3d.filtershow.pipeline.ImagePreset;
35
36public class Action implements RenderingRequestCaller {
37
38    private static final String LOGTAG = "Action";
39    private FilterRepresentation mRepresentation;
40    private String mName;
41    private Rect mImageFrame;
42    private Bitmap mImage;
43    private ArrayAdapter mAdapter;
44    public static final int FULL_VIEW = 0;
45    public static final int CROP_VIEW = 1;
46    public static final int ADD_ACTION = 2;
47    public static final int SPACER = 3;
48    private int mType = CROP_VIEW;
49    private Bitmap mPortraitImage;
50    private Bitmap mOverlayBitmap;
51    private Context mContext;
52    private boolean mCanBeRemoved = false;
53
54    public Action(Context context, FilterRepresentation representation, int type,
55                  boolean canBeRemoved) {
56        this(context, representation, type);
57        mCanBeRemoved = canBeRemoved;
58    }
59
60    public Action(Context context, FilterRepresentation representation, int type) {
61        this(context, type);
62        setRepresentation(representation);
63    }
64
65    public Action(Context context, int type) {
66        mContext = context;
67        setType(type);
68    }
69
70    public Action(Context context, FilterRepresentation representation) {
71        this(context, representation, CROP_VIEW);
72    }
73
74    public boolean canBeRemoved() {
75        return mCanBeRemoved;
76    }
77
78    public int getType() {
79        return mType;
80    }
81
82    public FilterRepresentation getRepresentation() {
83        return mRepresentation;
84    }
85
86    public void setRepresentation(FilterRepresentation representation) {
87        mRepresentation = representation;
88        mName = representation.getName();
89    }
90
91    public String getName() {
92        return mName;
93    }
94
95    public void setName(String name) {
96        mName = name;
97    }
98
99    public void setImageFrame(Rect imageFrame, int orientation) {
100        if (mImageFrame != null && mImageFrame.equals(imageFrame)) {
101            return;
102        }
103        Bitmap bitmap = MasterImage.getImage().getLargeThumbnailBitmap();
104        if (bitmap != null) {
105            mImageFrame = imageFrame;
106            int w = mImageFrame.width();
107            int h = mImageFrame.height();
108            if (orientation == CategoryView.VERTICAL
109                && mType == CROP_VIEW) {
110                w /= 2;
111            }
112            Bitmap bitmapCrop = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
113            drawCenteredImage(bitmap, bitmapCrop, true);
114
115            postNewIconRenderRequest(bitmapCrop);
116        }
117    }
118
119    public Bitmap getImage() {
120        return mImage;
121    }
122
123    public void setImage(Bitmap image) {
124        mImage = image;
125    }
126
127    public void setAdapter(ArrayAdapter adapter) {
128        mAdapter = adapter;
129    }
130
131    public void setType(int type) {
132        mType = type;
133    }
134
135    private void postNewIconRenderRequest(Bitmap bitmap) {
136        if (bitmap != null && mRepresentation != null) {
137            ImagePreset preset = new ImagePreset();
138            preset.addFilter(mRepresentation);
139            RenderingRequest.post(mContext, bitmap,
140                    preset, RenderingRequest.ICON_RENDERING, this);
141        }
142    }
143
144    private void drawCenteredImage(Bitmap source, Bitmap destination, boolean scale) {
145        RectF image = new RectF(0, 0, source.getWidth(), source.getHeight());
146        int border = 0;
147        if (!scale) {
148            border = destination.getWidth() - destination.getHeight();
149            if (border < 0) {
150                border = 0;
151            }
152        }
153        RectF frame = new RectF(border, 0,
154                destination.getWidth() - border,
155                destination.getHeight());
156        Matrix m = new Matrix();
157        m.setRectToRect(frame, image, Matrix.ScaleToFit.CENTER);
158        image.set(frame);
159        m.mapRect(image);
160        m.setRectToRect(image, frame, Matrix.ScaleToFit.FILL);
161        Canvas canvas = new Canvas(destination);
162        canvas.drawBitmap(source, m, new Paint(Paint.FILTER_BITMAP_FLAG));
163    }
164
165    @Override
166    public void available(RenderingRequest request) {
167        mImage = request.getBitmap();
168        if (mImage == null) {
169            return;
170        }
171        if (mRepresentation.getOverlayId() != 0 && mOverlayBitmap == null) {
172            mOverlayBitmap = BitmapFactory.decodeResource(
173                    mContext.getResources(),
174                    mRepresentation.getOverlayId());
175        }
176        if (mOverlayBitmap != null) {
177            if (getRepresentation().getFilterType() == FilterRepresentation.TYPE_BORDER) {
178                Canvas canvas = new Canvas(mImage);
179                canvas.drawBitmap(mOverlayBitmap, new Rect(0, 0, mOverlayBitmap.getWidth(), mOverlayBitmap.getHeight()),
180                        new Rect(0, 0, mImage.getWidth(), mImage.getHeight()), new Paint());
181            } else {
182                Canvas canvas = new Canvas(mImage);
183                canvas.drawARGB(128, 0, 0, 0);
184                drawCenteredImage(mOverlayBitmap, mImage, false);
185            }
186        }
187        if (mAdapter != null) {
188            mAdapter.notifyDataSetChanged();
189        }
190    }
191
192    public void setPortraitImage(Bitmap portraitImage) {
193        mPortraitImage = portraitImage;
194    }
195
196    public Bitmap getPortraitImage() {
197        return mPortraitImage;
198    }
199
200    public Bitmap getOverlayBitmap() {
201        return mOverlayBitmap;
202    }
203
204    public void setOverlayBitmap(Bitmap overlayBitmap) {
205        mOverlayBitmap = overlayBitmap;
206    }
207}
208