1/*
2 * Copyright (C) 2012 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.imageshow;
18
19import android.content.Context;
20import android.graphics.Bitmap;
21import android.graphics.Canvas;
22import android.graphics.Color;
23import android.graphics.Paint;
24import android.graphics.Rect;
25import android.util.AttributeSet;
26import android.view.View;
27
28import com.android.gallery3d.filtershow.FilterShowActivity;
29import com.android.gallery3d.filtershow.filters.ImageFilter;
30import com.android.gallery3d.filtershow.presets.ImagePreset;
31
32public class ImageSmallFilter extends ImageShow implements View.OnClickListener {
33
34    private static final String LOGTAG = "ImageSmallFilter";
35    private FilterShowActivity mController = null;
36    protected ImageFilter mImageFilter = null;
37    private boolean mShowTitle = true;
38    private boolean mSetBorder = false;
39    protected final Paint mPaint = new Paint();
40    protected boolean mIsSelected = false;
41
42    // TODO: move this to xml.
43    protected static int mMargin = 12;
44    protected static int mTextMargin = 8;
45    protected static int mBackgroundColor = Color.BLUE;
46    protected final int mSelectedBackgroundColor = Color.WHITE;
47    protected final int mTextColor = Color.WHITE;
48    private ImageSmallFilter mNullFilter;
49
50    public static void setMargin(int value) {
51        mMargin = value;
52    }
53
54    public static void setTextMargin(int value) {
55        mTextMargin = value;
56    }
57
58    public static void setDefaultBackgroundColor(int value) {
59        mBackgroundColor = value;
60    }
61
62    public ImageSmallFilter(Context context, AttributeSet attrs) {
63        super(context, attrs);
64        setOnClickListener(this);
65    }
66
67    public ImageSmallFilter(Context context) {
68        super(context);
69        setOnClickListener(this);
70    }
71
72    public void setImageFilter(ImageFilter filter) {
73        mImageFilter = filter;
74        mImagePreset = new ImagePreset();
75        mImagePreset.setName(filter.getName());
76        filter.setImagePreset(mImagePreset);
77        mImagePreset.add(mImageFilter);
78    }
79
80    @Override
81    public void setSelected(boolean value) {
82        if (mIsSelected != value) {
83            invalidate();
84        }
85        mIsSelected = value;
86    }
87
88    public void setBorder(boolean value) {
89        mSetBorder = value;
90    }
91
92    public void setController(FilterShowActivity activity) {
93        mController = activity;
94    }
95
96    @Override
97    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
98        int parentWidth = MeasureSpec.getSize(widthMeasureSpec);
99        int parentHeight = MeasureSpec.getSize(heightMeasureSpec);
100        int h = mTextSize + mTextPadding;
101        setMeasuredDimension(parentHeight - h, parentHeight);
102    }
103
104    /**
105     * Setting the nullFilter implies that the behavior of the button is toggle
106     *
107     * @param nullFilter
108     */
109    public void setNulfilter(ImageSmallFilter nullFilter) {
110        mNullFilter = nullFilter;
111    }
112
113    @Override
114    public void onClick(View v) {
115        if (mController != null) {
116            if (mImageFilter != null) {
117                if (mIsSelected && mNullFilter != null) {
118                    mNullFilter.onClick(v);
119                }
120                else {
121                    mController.useImageFilter(this, mImageFilter, mSetBorder);
122                }
123            } else if (mImagePreset != null) {
124                if (mIsSelected && mNullFilter != null) {
125                    mNullFilter.onClick(v);
126                }
127                else {
128                    mController.useImagePreset(this, mImagePreset);
129                }
130            }
131        }
132    }
133
134    @Override
135    public boolean updateGeometryFlags() {
136        // We don't want to warn listeners here that the image size has changed, because
137        // we'll be working with the small image...
138        return false;
139    }
140
141    public void setShowTitle(boolean value) {
142        mShowTitle = value;
143        invalidate();
144    }
145
146    @Override
147    public boolean showTitle() {
148        return mShowTitle;
149    }
150
151    @Override
152    public boolean showControls() {
153        return false;
154    }
155
156    @Override
157    public boolean showHires() {
158        return false;
159    }
160
161    @Override
162    public ImagePreset getImagePreset() {
163        return mImagePreset;
164    }
165
166    @Override
167    public void updateImagePresets(boolean force) {
168        ImagePreset preset = getImagePreset();
169        if (preset == null) {
170            return;
171        }
172    }
173
174    @Override
175    public void onDraw(Canvas canvas) {
176        requestFilteredImages();
177        canvas.drawColor(mBackgroundColor);
178        float textWidth = mPaint.measureText(mImageFilter.getName());
179        int h = mTextSize + 2 * mTextPadding;
180        int x = (int) ((getWidth() - textWidth) / 2);
181        int y = getHeight();
182        if (mIsSelected) {
183            mPaint.setColor(mSelectedBackgroundColor);
184            canvas.drawRect(0, mMargin, getWidth(), getWidth() + mMargin, mPaint);
185        }
186        Rect destination = new Rect(mMargin, 2*mMargin, getWidth() - mMargin, getWidth());
187        drawImage(canvas, getFilteredImage(), destination);
188        mPaint.setTextSize(mTextSize);
189        mPaint.setColor(mTextColor);
190        canvas.drawText(mImageFilter.getName(), x, y - mTextMargin, mPaint);
191    }
192
193    public void drawImage(Canvas canvas, Bitmap image, Rect destination) {
194        if (image != null) {
195            int iw = image.getWidth();
196            int ih = image.getHeight();
197            int x = 0;
198            int y = 0;
199            int size = 0;
200            Rect source = null;
201            if (iw > ih) {
202                size = ih;
203                x = (int) ((iw - size) / 2.0f);
204                y = 0;
205            } else {
206                size = iw;
207                x = 0;
208                y = (int) ((ih - size) / 2.0f);
209            }
210            source = new Rect(x, y, x + size, y + size);
211            canvas.drawBitmap(image, source, destination, mPaint);
212        }
213    }
214
215}
216