Action.java revision 32cc4dd751569721aa19218b4d947145577060d0
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.util.Log;
28import android.widget.ArrayAdapter;
29import android.widget.ListAdapter;
30
31import com.android.gallery3d.R;
32import com.android.gallery3d.filtershow.FilterShowActivity;
33import com.android.gallery3d.filtershow.filters.FilterUserPresetRepresentation;
34import com.android.gallery3d.filtershow.pipeline.RenderingRequest;
35import com.android.gallery3d.filtershow.pipeline.RenderingRequestCaller;
36import com.android.gallery3d.filtershow.filters.FilterRepresentation;
37import com.android.gallery3d.filtershow.imageshow.MasterImage;
38import com.android.gallery3d.filtershow.pipeline.ImagePreset;
39
40public class Action implements RenderingRequestCaller {
41
42    private static final String LOGTAG = "Action";
43    private FilterRepresentation mRepresentation;
44    private String mName;
45    private Rect mImageFrame;
46    private Bitmap mImage;
47    private ArrayAdapter mAdapter;
48    public static final int FULL_VIEW = 0;
49    public static final int CROP_VIEW = 1;
50    public static final int ADD_ACTION = 2;
51    public static final int SPACER = 3;
52    private int mType = CROP_VIEW;
53    private Bitmap mPortraitImage;
54    private Bitmap mOverlayBitmap;
55    private FilterShowActivity mContext;
56    private boolean mCanBeRemoved = false;
57    private int mTextSize = 32;
58
59    public Action(FilterShowActivity context, FilterRepresentation representation, int type,
60                  boolean canBeRemoved) {
61        this(context, representation, type);
62        mCanBeRemoved = canBeRemoved;
63        mTextSize = context.getResources().getDimensionPixelSize(
64                R.dimen.category_panel_text_size);
65    }
66
67    public Action(FilterShowActivity context, FilterRepresentation representation, int type) {
68        this(context, type);
69        setRepresentation(representation);
70    }
71
72    public Action(FilterShowActivity context, int type) {
73        mContext = context;
74        setType(type);
75        mContext.registerAction(this);
76    }
77
78    public Action(FilterShowActivity context, FilterRepresentation representation) {
79        this(context, representation, CROP_VIEW);
80    }
81
82    public boolean canBeRemoved() {
83        return mCanBeRemoved;
84    }
85
86    public int getType() {
87        return mType;
88    }
89
90    public FilterRepresentation getRepresentation() {
91        return mRepresentation;
92    }
93
94    public void setRepresentation(FilterRepresentation representation) {
95        mRepresentation = representation;
96        mName = representation.getName();
97    }
98
99    public String getName() {
100        return mName;
101    }
102
103    public void setName(String name) {
104        mName = name;
105    }
106
107    public void setImageFrame(Rect imageFrame, int orientation) {
108        if (mImageFrame != null && mImageFrame.equals(imageFrame)) {
109            return;
110        }
111        if (getType() == Action.ADD_ACTION) {
112            return;
113        }
114        Bitmap temp = MasterImage.getImage().getTemporaryThumbnailBitmap();
115        if (temp != null) {
116            mImage = temp;
117        }
118        Bitmap bitmap = MasterImage.getImage().getThumbnailBitmap();
119        if (bitmap != null) {
120            mImageFrame = imageFrame;
121            int w = mImageFrame.width();
122            int h = mImageFrame.height();
123            postNewIconRenderRequest(w, h);
124        }
125    }
126
127    public Bitmap getImage() {
128        return mImage;
129    }
130
131    public void setImage(Bitmap image) {
132        mImage = image;
133    }
134
135    public void setAdapter(ArrayAdapter adapter) {
136        mAdapter = adapter;
137    }
138
139    public void setType(int type) {
140        mType = type;
141    }
142
143    private void postNewIconRenderRequest(int w, int h) {
144        if (mRepresentation != null) {
145            ImagePreset preset = new ImagePreset();
146            preset.addFilter(mRepresentation);
147            RenderingRequest.postIconRequest(mContext, w, h, preset, this);
148        }
149    }
150
151    private void drawCenteredImage(Bitmap source, Bitmap destination, boolean scale) {
152        int minSide = Math.min(destination.getWidth(), destination.getHeight());
153        Matrix m = new Matrix();
154        float scaleFactor = minSide / (float) Math.min(source.getWidth(), source.getHeight());
155
156        float dx = (destination.getWidth() - source.getWidth() * scaleFactor) / 2.0f;
157        float dy = (destination.getHeight() - source.getHeight() * scaleFactor) / 2.0f;
158        if (mImageFrame.height() > mImageFrame.width()) {
159            // if portrait
160            dy -= mTextSize;
161        }
162        m.setScale(scaleFactor, scaleFactor);
163        m.postTranslate(dx, dy);
164        Canvas canvas = new Canvas(destination);
165        canvas.drawBitmap(source, m, new Paint(Paint.FILTER_BITMAP_FLAG));
166    }
167
168    @Override
169    public void available(RenderingRequest request) {
170        clearBitmap();
171        mImage = request.getBitmap();
172        if (mImage == null) {
173            mImageFrame = null;
174            return;
175        }
176        if (mRepresentation.getOverlayId() != 0 && mOverlayBitmap == null) {
177            mOverlayBitmap = BitmapFactory.decodeResource(
178                    mContext.getResources(),
179                    mRepresentation.getOverlayId());
180        }
181        if (mOverlayBitmap != null) {
182            if (getRepresentation().getFilterType() == FilterRepresentation.TYPE_BORDER) {
183                Canvas canvas = new Canvas(mImage);
184                canvas.drawBitmap(mOverlayBitmap, new Rect(0, 0, mOverlayBitmap.getWidth(), mOverlayBitmap.getHeight()),
185                        new Rect(0, 0, mImage.getWidth(), mImage.getHeight()), new Paint());
186            } else {
187                Canvas canvas = new Canvas(mImage);
188                canvas.drawARGB(128, 0, 0, 0);
189                drawCenteredImage(mOverlayBitmap, mImage, false);
190            }
191        }
192        if (mAdapter != null) {
193            mAdapter.notifyDataSetChanged();
194        }
195    }
196
197    public void setPortraitImage(Bitmap portraitImage) {
198        mPortraitImage = portraitImage;
199    }
200
201    public Bitmap getPortraitImage() {
202        return mPortraitImage;
203    }
204
205    public Bitmap getOverlayBitmap() {
206        return mOverlayBitmap;
207    }
208
209    public void setOverlayBitmap(Bitmap overlayBitmap) {
210        mOverlayBitmap = overlayBitmap;
211    }
212
213    public void clearBitmap() {
214        if (mImage != null
215                && mImage != MasterImage.getImage().getTemporaryThumbnailBitmap()) {
216            MasterImage.getImage().getBitmapCache().cache(mImage);
217        }
218        mImage = null;
219    }
220}
221