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 
17 package com.android.gallery3d.filtershow.filters;
18 
19 import android.util.JsonReader;
20 import android.util.JsonWriter;
21 import android.util.Log;
22 
23 import com.android.gallery3d.filtershow.editors.BasicEditor;
24 
25 import java.io.IOException;
26 import java.util.ArrayList;
27 
28 public class FilterRepresentation {
29     private static final String LOGTAG = "FilterRepresentation";
30     private static final boolean DEBUG = false;
31     private String mName;
32     private int mPriority = TYPE_NORMAL;
33     private Class<?> mFilterClass;
34     private boolean mSupportsPartialRendering = false;
35     private int mTextId = 0;
36     private int mEditorId = BasicEditor.ID;
37     private int mButtonId = 0;
38     private int mOverlayId = 0;
39     private boolean mOverlayOnly = false;
40     private boolean mShowParameterValue = true;
41     private boolean mIsBooleanFilter = false;
42     private String mSerializationName;
43     public static final byte TYPE_BORDER = 1;
44     public static final byte TYPE_FX = 2;
45     public static final byte TYPE_WBALANCE = 3;
46     public static final byte TYPE_VIGNETTE = 4;
47     public static final byte TYPE_NORMAL = 5;
48     public static final byte TYPE_TINYPLANET = 6;
49     public static final byte TYPE_GEOMETRY = 7;
50     protected static final String NAME_TAG = "Name";
51 
FilterRepresentation(String name)52     public FilterRepresentation(String name) {
53         mName = name;
54     }
55 
copy()56     public FilterRepresentation copy(){
57         FilterRepresentation representation = new FilterRepresentation(mName);
58         representation.useParametersFrom(this);
59         return representation;
60     }
61 
copyAllParameters(FilterRepresentation representation)62     protected void copyAllParameters(FilterRepresentation representation) {
63         representation.setName(getName());
64         representation.setFilterClass(getFilterClass());
65         representation.setFilterType(getFilterType());
66         representation.setSupportsPartialRendering(supportsPartialRendering());
67         representation.setTextId(getTextId());
68         representation.setEditorId(getEditorId());
69         representation.setOverlayId(getOverlayId());
70         representation.setOverlayOnly(getOverlayOnly());
71         representation.setShowParameterValue(showParameterValue());
72         representation.mSerializationName = mSerializationName;
73         representation.setIsBooleanFilter(isBooleanFilter());
74     }
75 
equals(FilterRepresentation representation)76     public boolean equals(FilterRepresentation representation) {
77         if (representation == null) {
78             return false;
79         }
80         if (representation.mFilterClass == mFilterClass
81                 && representation.mName.equalsIgnoreCase(mName)
82                 && representation.mPriority == mPriority
83                 // TODO: After we enable partial rendering, we can switch back
84                 // to use member variable here.
85                 && representation.supportsPartialRendering() == supportsPartialRendering()
86                 && representation.mTextId == mTextId
87                 && representation.mEditorId == mEditorId
88                 && representation.mButtonId == mButtonId
89                 && representation.mOverlayId == mOverlayId
90                 && representation.mOverlayOnly == mOverlayOnly
91                 && representation.mShowParameterValue == mShowParameterValue
92                 && representation.mIsBooleanFilter == mIsBooleanFilter) {
93             return true;
94         }
95         return false;
96     }
97 
isBooleanFilter()98     public boolean isBooleanFilter() {
99         return mIsBooleanFilter;
100     }
101 
setIsBooleanFilter(boolean value)102     public void setIsBooleanFilter(boolean value) {
103         mIsBooleanFilter = value;
104     }
105 
106     @Override
toString()107     public String toString() {
108         return mName;
109     }
110 
setName(String name)111     public void setName(String name) {
112         mName = name;
113     }
114 
getName()115     public String getName() {
116         return mName;
117     }
118 
setSerializationName(String sname)119     public void setSerializationName(String sname) {
120         mSerializationName = sname;
121     }
122 
getSerializationName()123     public String getSerializationName() {
124         return mSerializationName;
125     }
126 
setFilterType(int priority)127     public void setFilterType(int priority) {
128         mPriority = priority;
129     }
130 
getFilterType()131     public int getFilterType() {
132         return mPriority;
133     }
134 
isNil()135     public boolean isNil() {
136         return false;
137     }
138 
supportsPartialRendering()139     public boolean supportsPartialRendering() {
140         return mSupportsPartialRendering;
141     }
142 
setSupportsPartialRendering(boolean value)143     public void setSupportsPartialRendering(boolean value) {
144         mSupportsPartialRendering = value;
145     }
146 
useParametersFrom(FilterRepresentation a)147     public void useParametersFrom(FilterRepresentation a) {
148     }
149 
allowsSingleInstanceOnly()150     public boolean allowsSingleInstanceOnly() {
151         return false;
152     }
153 
getFilterClass()154     public Class<?> getFilterClass() {
155         return mFilterClass;
156     }
157 
setFilterClass(Class<?> filterClass)158     public void setFilterClass(Class<?> filterClass) {
159         mFilterClass = filterClass;
160     }
161 
162     // This same() function is different from equals(), basically it checks
163     // whether 2 FilterRepresentations are the same type. It doesn't care about
164     // the values.
same(FilterRepresentation b)165     public boolean same(FilterRepresentation b) {
166         if (b == null) {
167             return false;
168         }
169         return getFilterClass() == b.getFilterClass();
170     }
171 
getTextId()172     public int getTextId() {
173         return mTextId;
174     }
175 
setTextId(int textId)176     public void setTextId(int textId) {
177         mTextId = textId;
178     }
179 
getOverlayId()180     public int getOverlayId() {
181         return mOverlayId;
182     }
183 
setOverlayId(int overlayId)184     public void setOverlayId(int overlayId) {
185         mOverlayId = overlayId;
186     }
187 
getOverlayOnly()188     public boolean getOverlayOnly() {
189         return mOverlayOnly;
190     }
191 
setOverlayOnly(boolean value)192     public void setOverlayOnly(boolean value) {
193         mOverlayOnly = value;
194     }
195 
getEditorId()196     final public int getEditorId() {
197         return mEditorId;
198     }
199 
getEditorIds()200     public int[] getEditorIds() {
201         return new int[] {
202         mEditorId };
203     }
204 
setEditorId(int editorId)205     public void setEditorId(int editorId) {
206         mEditorId = editorId;
207     }
208 
showParameterValue()209     public boolean showParameterValue() {
210         return mShowParameterValue;
211     }
212 
setShowParameterValue(boolean showParameterValue)213     public void setShowParameterValue(boolean showParameterValue) {
214         mShowParameterValue = showParameterValue;
215     }
216 
getStateRepresentation()217     public String getStateRepresentation() {
218         return "";
219     }
220 
221     /**
222      * Method must "beginObject()" add its info and "endObject()"
223      * @param writer
224      * @throws IOException
225      */
serializeRepresentation(JsonWriter writer)226     public void serializeRepresentation(JsonWriter writer) throws IOException {
227         writer.beginObject();
228         {
229             String[][] rep = serializeRepresentation();
230             for (int k = 0; k < rep.length; k++) {
231                 writer.name(rep[k][0]);
232                 writer.value(rep[k][1]);
233             }
234         }
235         writer.endObject();
236     }
237 
238     // this is the old way of doing this and will be removed soon
serializeRepresentation()239     public String[][] serializeRepresentation() {
240         String[][] ret = {{NAME_TAG, getName()}};
241         return ret;
242     }
243 
deSerializeRepresentation(JsonReader reader)244     public void deSerializeRepresentation(JsonReader reader) throws IOException {
245         ArrayList<String[]> al = new ArrayList<String[]>();
246         reader.beginObject();
247         while (reader.hasNext()) {
248             String[] kv = {reader.nextName(), reader.nextString()};
249             al.add(kv);
250 
251         }
252         reader.endObject();
253         String[][] oldFormat = al.toArray(new String[al.size()][]);
254 
255         deSerializeRepresentation(oldFormat);
256     }
257 
258     // this is the old way of doing this and will be removed soon
deSerializeRepresentation(String[][] rep)259     public void deSerializeRepresentation(String[][] rep) {
260         for (int i = 0; i < rep.length; i++) {
261             if (NAME_TAG.equals(rep[i][0])) {
262                 mName = rep[i][1];
263                 break;
264             }
265         }
266     }
267 
268     // Override this in subclasses
getStyle()269     public int getStyle() {
270         return -1;
271     }
272 
canMergeWith(FilterRepresentation representation)273     public boolean canMergeWith(FilterRepresentation representation) {
274         if (getFilterType() == FilterRepresentation.TYPE_GEOMETRY
275             && representation.getFilterType() == FilterRepresentation.TYPE_GEOMETRY) {
276             return true;
277         }
278         return false;
279     }
280 }
281