OSDN Git Service

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