summaryrefslogtreecommitdiffstats
path: root/src/com/android/gallery3d/filtershow/category/Action.java
blob: 506b2bf0f9838d5a40c381aa22f62c6fa838829f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/*
 * Copyright (C) 2013 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.gallery3d.filtershow.category;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;
import com.android.gallery3d.filtershow.cache.RenderingRequest;
import com.android.gallery3d.filtershow.cache.RenderingRequestCaller;
import com.android.gallery3d.filtershow.filters.FilterRepresentation;
import com.android.gallery3d.filtershow.imageshow.MasterImage;
import com.android.gallery3d.filtershow.presets.ImagePreset;

public class Action implements RenderingRequestCaller {

    private static final String LOGTAG = "Action";
    private FilterRepresentation mRepresentation;
    private String mName;
    private Rect mImageFrame;
    private Bitmap mImage;
    private CategoryAdapter mAdapter;
    public static final int FULL_VIEW = 0;
    public static final int CROP_VIEW = 1;
    private int mType = CROP_VIEW;
    private Bitmap mPortraitImage;
    private Bitmap mOverlayBitmap;
    private Context mContext;

    public Action(Context context, FilterRepresentation representation, int type) {
        mContext = context;
        setRepresentation(representation);
        setType(type);
    }

    public Action(Context context, FilterRepresentation representation) {
        this(context, representation, CROP_VIEW);
    }

    public FilterRepresentation getRepresentation() {
        return mRepresentation;
    }

    public void setRepresentation(FilterRepresentation representation) {
        mRepresentation = representation;
        mName = representation.getName();
    }

    public String getName() {
        return mName;
    }

    public void setName(String name) {
        mName = name;
    }

    public void setImageFrame(Rect imageFrame) {
        if (mImageFrame != null && mImageFrame.equals(imageFrame)) {
            return;
        }
        Bitmap bitmap = MasterImage.getImage().getLargeThumbnailBitmap();
        if (bitmap != null) {
            mImageFrame = imageFrame;
            int w = mImageFrame.width();
            int h = mImageFrame.height();
            if (mType == CROP_VIEW) {
                w /= 2;
            }
            Bitmap bitmapCrop = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
            drawCenteredImage(bitmap, bitmapCrop, true);

            postNewIconRenderRequest(bitmapCrop);
        }
    }

    public Bitmap getImage() {
        return mImage;
    }

    public void setImage(Bitmap image) {
        mImage = image;
    }

    public void setAdapter(CategoryAdapter adapter) {
        mAdapter = adapter;
    }

    public void setType(int type) {
        mType = type;
    }

    private void postNewIconRenderRequest(Bitmap bitmap) {
        if (bitmap != null && mRepresentation != null) {
            ImagePreset preset = new ImagePreset();
            preset.addFilter(mRepresentation);
            RenderingRequest.post(bitmap,
                    preset, RenderingRequest.ICON_RENDERING, this);
        }
    }

    private void drawCenteredImage(Bitmap source, Bitmap destination, boolean scale) {
        RectF image = new RectF(0, 0, source.getWidth(), source.getHeight());
        int border = 0;
        if (!scale) {
            border = destination.getWidth() - destination.getHeight();
            if (border < 0) {
                border = 0;
            }
        }
        RectF frame = new RectF(border, 0,
                destination.getWidth() - border,
                destination.getHeight());
        Matrix m = new Matrix();
        m.setRectToRect(frame, image, Matrix.ScaleToFit.CENTER);
        image.set(frame);
        m.mapRect(image);
        m.setRectToRect(image, frame, Matrix.ScaleToFit.FILL);
        Canvas canvas = new Canvas(destination);
        canvas.drawBitmap(source, m, new Paint());
    }

    @Override
    public void available(RenderingRequest request) {
        mImage = request.getBitmap();
        if (mImage == null) {
            return;
        }
        if (mRepresentation.getOverlayId() != 0 && mOverlayBitmap == null) {
            mOverlayBitmap = BitmapFactory.decodeResource(
                    mContext.getResources(),
                    mRepresentation.getOverlayId());
        }
        if (mOverlayBitmap != null) {
            if (getRepresentation().getFilterType() == FilterRepresentation.TYPE_BORDER) {
                Canvas canvas = new Canvas(mImage);
                canvas.drawBitmap(mOverlayBitmap, new Rect(0, 0, mOverlayBitmap.getWidth(), mOverlayBitmap.getHeight()),
                        new Rect(0, 0, mImage.getWidth(), mImage.getHeight()), new Paint());
            } else {
                Canvas canvas = new Canvas(mImage);
                canvas.drawARGB(128, 0, 0, 0);
                drawCenteredImage(mOverlayBitmap, mImage, false);
            }
        }
        if (mAdapter != null) {
            mAdapter.notifyDataSetChanged();
        }
    }

    public void setPortraitImage(Bitmap portraitImage) {
        mPortraitImage = portraitImage;
    }

    public Bitmap getPortraitImage() {
        return mPortraitImage;
    }

    public Bitmap getOverlayBitmap() {
        return mOverlayBitmap;
    }

    public void setOverlayBitmap(Bitmap overlayBitmap) {
        mOverlayBitmap = overlayBitmap;
    }
}