Refactoring GeometryMetadata.

Bug: 9170644

- First part of multi-step refactor, splits
  GeometryMetadata functionality into 4
  FilterRepresentation subclasses as requested.

Change-Id: I212193db8c154f64c550adb9735c0502bcaa7468
diff --git a/src/com/android/gallery3d/filtershow/filters/FilterCropRepresentation.java b/src/com/android/gallery3d/filtershow/filters/FilterCropRepresentation.java
new file mode 100644
index 0000000..345c2f1
--- /dev/null
+++ b/src/com/android/gallery3d/filtershow/filters/FilterCropRepresentation.java
@@ -0,0 +1,167 @@
+/*
+ * 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.filters;
+
+import android.graphics.RectF;
+import android.util.JsonReader;
+import android.util.JsonWriter;
+
+import com.android.gallery3d.R;
+import com.android.gallery3d.filtershow.editors.EditorCrop;
+
+import java.io.IOException;
+
+public class FilterCropRepresentation extends FilterRepresentation {
+    public static final String SERIALIZATION_NAME = "CROP";
+    public static final String[] BOUNDS = {
+            "C0", "C1", "C2", "C3", "I0", "I1", "I2", "I3"
+    };
+    private static final String TAG = FilterCropRepresentation.class.getSimpleName();
+
+    RectF mCrop = new RectF();
+    RectF mImage = new RectF();
+
+    public FilterCropRepresentation(RectF crop, RectF image) {
+        super(FilterCropRepresentation.class.getSimpleName());
+        setSerializationName(SERIALIZATION_NAME);
+        setShowParameterValue(true);
+        setFilterClass(FilterCropRepresentation.class);
+        setFilterType(FilterRepresentation.TYPE_GEOMETRY);
+        setTextId(R.string.crop);
+        setEditorId(EditorCrop.ID);
+        setCrop(crop);
+        setImage(image);
+    }
+
+    public FilterCropRepresentation(FilterCropRepresentation m) {
+        this(m.getCrop(), m.getImage());
+    }
+
+    public FilterCropRepresentation() {
+        this(new RectF(), new RectF());
+    }
+
+    public void set(FilterCropRepresentation r) {
+        mCrop.set(r.mCrop);
+        mImage.set(r.mImage);
+    }
+
+    public RectF getCrop() {
+        return new RectF(mCrop);
+    }
+
+    public void getCrop(RectF r) {
+        r.set(mCrop);
+    }
+
+    public void setCrop(RectF crop) {
+        if (crop == null) {
+            throw new IllegalArgumentException("Argument to setCrop is null");
+        }
+        mCrop.set(crop);
+    }
+
+    public RectF getImage() {
+        return new RectF(mImage);
+    }
+
+    public void getImage(RectF r) {
+        r.set(mImage);
+    }
+
+    public void setImage(RectF image) {
+        if (image == null) {
+            throw new IllegalArgumentException("Argument to setImage is null");
+        }
+        mImage.set(image);
+    }
+
+    @Override
+    public boolean allowsSingleInstanceOnly() {
+        return true;
+    }
+
+    @Override
+    public FilterRepresentation copy(){
+        return new FilterCropRepresentation(this);
+    }
+
+    @Override
+    protected void copyAllParameters(FilterRepresentation representation) {
+        if (!(representation instanceof FilterCropRepresentation)) {
+            throw new IllegalArgumentException("calling copyAllParameters with incompatible types!");
+        }
+        super.copyAllParameters(representation);
+        representation.useParametersFrom(this);
+    }
+
+    @Override
+    public void useParametersFrom(FilterRepresentation a) {
+        if (!(a instanceof FilterCropRepresentation)) {
+            throw new IllegalArgumentException("calling useParametersFrom with incompatible types!");
+        }
+        setCrop(((FilterCropRepresentation) a).mCrop);
+        setImage(((FilterCropRepresentation) a).mImage);
+    }
+
+    @Override
+    public boolean isNil() {
+        return mCrop.equals(mImage);
+    }
+
+    @Override
+    public void serializeRepresentation(JsonWriter writer) throws IOException {
+        writer.beginObject();
+        writer.name(BOUNDS[0]).value(mCrop.left);
+        writer.name(BOUNDS[1]).value(mCrop.top);
+        writer.name(BOUNDS[2]).value(mCrop.right);
+        writer.name(BOUNDS[3]).value(mCrop.bottom);
+        writer.name(BOUNDS[4]).value(mImage.left);
+        writer.name(BOUNDS[5]).value(mImage.top);
+        writer.name(BOUNDS[6]).value(mImage.right);
+        writer.name(BOUNDS[7]).value(mImage.bottom);
+        writer.endObject();
+    }
+
+    @Override
+    public void deSerializeRepresentation(JsonReader reader) throws IOException {
+        reader.beginObject();
+        while (reader.hasNext()) {
+            String name = reader.nextName();
+            if (BOUNDS[0].equals(name)) {
+                mCrop.left = (float) reader.nextDouble();
+            } else if (BOUNDS[1].equals(name)) {
+                mCrop.top = (float) reader.nextDouble();
+            } else if (BOUNDS[2].equals(name)) {
+                mCrop.right = (float) reader.nextDouble();
+            } else if (BOUNDS[3].equals(name)) {
+                mCrop.bottom = (float) reader.nextDouble();
+            } else if (BOUNDS[4].equals(name)) {
+                mImage.left = (float) reader.nextDouble();
+            } else if (BOUNDS[5].equals(name)) {
+                mImage.top = (float) reader.nextDouble();
+            } else if (BOUNDS[6].equals(name)) {
+                mImage.right = (float) reader.nextDouble();
+            } else if (BOUNDS[7].equals(name)) {
+                mImage.bottom = (float) reader.nextDouble();
+            } else {
+                reader.skipValue();
+            }
+        }
+        reader.endObject();
+    }
+}
diff --git a/src/com/android/gallery3d/filtershow/filters/FilterMirrorRepresentation.java b/src/com/android/gallery3d/filtershow/filters/FilterMirrorRepresentation.java
new file mode 100644
index 0000000..0acf70e
--- /dev/null
+++ b/src/com/android/gallery3d/filtershow/filters/FilterMirrorRepresentation.java
@@ -0,0 +1,157 @@
+/*
+ * 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.filters;
+
+import android.util.JsonReader;
+import android.util.JsonWriter;
+import android.util.Log;
+
+import com.android.gallery3d.R;
+import com.android.gallery3d.filtershow.editors.EditorFlip;
+
+import java.io.IOException;
+
+public class FilterMirrorRepresentation extends FilterRepresentation {
+    public static final String SERIALIZATION_NAME = "MIRROR";
+    private static final String SERIALIZATION_MIRROR_VALUE = "value";
+    private static final String TAG = FilterMirrorRepresentation.class.getSimpleName();
+
+    Mirror mMirror = Mirror.NONE;
+
+    public enum Mirror {
+        NONE('N'), VERTICAL('V'), HORIZONTAL('H'), BOTH('B');
+        char mValue;
+
+        private Mirror(char value) {
+            mValue = value;
+        }
+
+        public char value() {
+            return mValue;
+        }
+
+        public static Mirror fromValue(char value) {
+            switch (value) {
+                case 'N':
+                    return NONE;
+                case 'V':
+                    return VERTICAL;
+                case 'H':
+                    return HORIZONTAL;
+                case 'B':
+                    return BOTH;
+                default:
+                    return null;
+            }
+        }
+    }
+
+    public FilterMirrorRepresentation(Mirror mirror) {
+        super(FilterMirrorRepresentation.class.getSimpleName());
+        setSerializationName(SERIALIZATION_NAME);
+        setShowParameterValue(true);
+        setFilterClass(FilterMirrorRepresentation.class);
+        setFilterType(FilterRepresentation.TYPE_GEOMETRY);
+        setTextId(R.string.mirror);
+        setEditorId(EditorFlip.ID);
+        setMirror(mirror);
+    }
+
+    public FilterMirrorRepresentation(FilterMirrorRepresentation m) {
+        this(m.getMirror());
+    }
+
+    public FilterMirrorRepresentation() {
+        this(Mirror.NONE);
+    }
+
+    public Mirror getMirror() {
+        return mMirror;
+    }
+
+    public void set(FilterMirrorRepresentation r) {
+        mMirror = r.mMirror;
+    }
+
+    public void setMirror(Mirror mirror) {
+        if (mirror == null) {
+            throw new IllegalArgumentException("Argument to setMirror is null");
+        }
+        mMirror = mirror;
+    }
+
+    @Override
+    public boolean allowsSingleInstanceOnly() {
+        return true;
+    }
+
+    @Override
+    public FilterRepresentation copy() {
+        return new FilterMirrorRepresentation(this);
+    }
+
+    @Override
+    protected void copyAllParameters(FilterRepresentation representation) {
+        if (!(representation instanceof FilterMirrorRepresentation)) {
+            throw new IllegalArgumentException("calling copyAllParameters with incompatible types!");
+        }
+        super.copyAllParameters(representation);
+        representation.useParametersFrom(this);
+    }
+
+    @Override
+    public void useParametersFrom(FilterRepresentation a) {
+        if (!(a instanceof FilterMirrorRepresentation)) {
+            throw new IllegalArgumentException("calling useParametersFrom with incompatible types!");
+        }
+        setMirror(((FilterMirrorRepresentation) a).getMirror());
+    }
+
+    @Override
+    public boolean isNil() {
+        return mMirror == Mirror.NONE;
+    }
+
+    @Override
+    public void serializeRepresentation(JsonWriter writer) throws IOException {
+        writer.beginObject();
+        writer.name(SERIALIZATION_MIRROR_VALUE).value(mMirror.value());
+        writer.endObject();
+    }
+
+    @Override
+    public void deSerializeRepresentation(JsonReader reader) throws IOException {
+        boolean unset = true;
+        reader.beginObject();
+        while (reader.hasNext()) {
+            String name = reader.nextName();
+            if (SERIALIZATION_MIRROR_VALUE.equals(name)) {
+                Mirror r = Mirror.fromValue((char) reader.nextInt());
+                if (r != null) {
+                    setMirror(r);
+                    unset = false;
+                }
+            } else {
+                reader.skipValue();
+            }
+        }
+        if (unset) {
+            Log.w(TAG, "WARNING: bad value when deserializing " + SERIALIZATION_NAME);
+        }
+        reader.endObject();
+    }
+}
diff --git a/src/com/android/gallery3d/filtershow/filters/FilterRepresentation.java b/src/com/android/gallery3d/filtershow/filters/FilterRepresentation.java
index ca9587c..5b33ffb 100644
--- a/src/com/android/gallery3d/filtershow/filters/FilterRepresentation.java
+++ b/src/com/android/gallery3d/filtershow/filters/FilterRepresentation.java
@@ -45,6 +45,7 @@
     public static final byte TYPE_VIGNETTE = 4;
     public static final byte TYPE_NORMAL = 5;
     public static final byte TYPE_TINYPLANET = 6;
+    public static final byte TYPE_GEOMETRY = 7;
     protected static final String NAME_TAG = "Name";
 
     public FilterRepresentation(String name) {
diff --git a/src/com/android/gallery3d/filtershow/filters/FilterRotateRepresentation.java b/src/com/android/gallery3d/filtershow/filters/FilterRotateRepresentation.java
new file mode 100644
index 0000000..8f7d8eb
--- /dev/null
+++ b/src/com/android/gallery3d/filtershow/filters/FilterRotateRepresentation.java
@@ -0,0 +1,157 @@
+/*
+ * 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.filters;
+
+import android.util.JsonReader;
+import android.util.JsonWriter;
+import android.util.Log;
+
+import com.android.gallery3d.R;
+import com.android.gallery3d.filtershow.editors.EditorRotate;
+
+import java.io.IOException;
+
+public class FilterRotateRepresentation extends FilterRepresentation {
+    public static final String SERIALIZATION_NAME = "ROTATION";
+    public static final String SERIALIZATION_ROTATE_VALUE = "value";
+    private static final String TAG = FilterRotateRepresentation.class.getSimpleName();
+
+    Rotation mRotation = Rotation.ZERO;
+
+    public enum Rotation {
+        ZERO(0), NINETY(90), ONE_EIGHTY(180), TWO_SEVENTY(270);
+        private final int mValue;
+
+        private Rotation(int value) {
+            mValue = value;
+        }
+
+        public int value() {
+            return mValue;
+        }
+
+        public static Rotation fromValue(int value) {
+            switch (value) {
+                case 0:
+                    return ZERO;
+                case 90:
+                    return NINETY;
+                case 180:
+                    return ONE_EIGHTY;
+                case 270:
+                    return TWO_SEVENTY;
+                default:
+                    return null;
+            }
+        }
+    }
+
+    public FilterRotateRepresentation(Rotation rotation) {
+        super(FilterRotateRepresentation.class.getSimpleName());
+        setSerializationName(SERIALIZATION_NAME);
+        setShowParameterValue(true);
+        setFilterClass(FilterRotateRepresentation.class);
+        setFilterType(FilterRepresentation.TYPE_GEOMETRY);
+        setTextId(R.string.rotate);
+        setEditorId(EditorRotate.ID);
+        setRotation(rotation);
+    }
+
+    public FilterRotateRepresentation(FilterRotateRepresentation r) {
+        this(r.getRotation());
+    }
+
+    public FilterRotateRepresentation() {
+        this(Rotation.ZERO);
+    }
+
+    public Rotation getRotation() {
+        return mRotation;
+    }
+
+    public void set(FilterRotateRepresentation r) {
+        mRotation = r.mRotation;
+    }
+
+    public void setRotation(Rotation rotation) {
+        if (rotation == null) {
+            throw new IllegalArgumentException("Argument to setRotation is null");
+        }
+        mRotation = rotation;
+    }
+
+    @Override
+    public boolean allowsSingleInstanceOnly() {
+        return true;
+    }
+
+    @Override
+    public FilterRepresentation copy() {
+        return new FilterRotateRepresentation(this);
+    }
+
+    @Override
+    protected void copyAllParameters(FilterRepresentation representation) {
+        if (!(representation instanceof FilterRotateRepresentation)) {
+            throw new IllegalArgumentException("calling copyAllParameters with incompatible types!");
+        }
+        super.copyAllParameters(representation);
+        representation.useParametersFrom(this);
+    }
+
+    @Override
+    public void useParametersFrom(FilterRepresentation a) {
+        if (!(a instanceof FilterRotateRepresentation)) {
+            throw new IllegalArgumentException("calling useParametersFrom with incompatible types!");
+        }
+        setRotation(((FilterRotateRepresentation) a).getRotation());
+    }
+
+    @Override
+    public boolean isNil() {
+        return mRotation == Rotation.ZERO;
+    }
+
+    @Override
+    public void serializeRepresentation(JsonWriter writer) throws IOException {
+        writer.beginObject();
+        writer.name(SERIALIZATION_ROTATE_VALUE).value(mRotation.value());
+        writer.endObject();
+    }
+
+    @Override
+    public void deSerializeRepresentation(JsonReader reader) throws IOException {
+        boolean unset = true;
+        reader.beginObject();
+        while (reader.hasNext()) {
+            String name = reader.nextName();
+            if (SERIALIZATION_ROTATE_VALUE.equals(name)) {
+                Rotation r = Rotation.fromValue(reader.nextInt());
+                if (r != null) {
+                    setRotation(r);
+                    unset = false;
+                }
+            } else {
+                reader.skipValue();
+            }
+        }
+        if (unset) {
+            Log.w(TAG, "WARNING: bad value when deserializing " + SERIALIZATION_NAME);
+        }
+        reader.endObject();
+    }
+}
diff --git a/src/com/android/gallery3d/filtershow/filters/FilterStraightenRepresentation.java b/src/com/android/gallery3d/filtershow/filters/FilterStraightenRepresentation.java
new file mode 100644
index 0000000..a06216e
--- /dev/null
+++ b/src/com/android/gallery3d/filtershow/filters/FilterStraightenRepresentation.java
@@ -0,0 +1,136 @@
+/*
+ * 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.filters;
+
+import android.util.JsonReader;
+import android.util.JsonWriter;
+import android.util.Log;
+
+import com.android.gallery3d.R;
+import com.android.gallery3d.filtershow.editors.EditorStraighten;
+
+import java.io.IOException;
+
+public class FilterStraightenRepresentation extends FilterRepresentation {
+    public static final String SERIALIZATION_NAME = "STRAIGHTEN";
+    public static final String SERIALIZATION_STRAIGHTEN_VALUE = "value";
+    private static final String TAG = FilterStraightenRepresentation.class.getSimpleName();
+
+    float mStraighten;
+
+    public FilterStraightenRepresentation(float straighten) {
+        super(FilterStraightenRepresentation.class.getSimpleName());
+        setSerializationName(SERIALIZATION_NAME);
+        setShowParameterValue(true);
+        setFilterClass(FilterStraightenRepresentation.class);
+        setFilterType(FilterRepresentation.TYPE_GEOMETRY);
+        setTextId(R.string.straighten);
+        setEditorId(EditorStraighten.ID);
+        setStraighten(straighten);
+    }
+
+    public FilterStraightenRepresentation(FilterStraightenRepresentation s) {
+        this(s.getStraighten());
+    }
+
+    public FilterStraightenRepresentation() {
+        this(0);
+    }
+
+    public void set(FilterStraightenRepresentation r) {
+        mStraighten = r.mStraighten;
+    }
+
+    public float getStraighten() {
+        return mStraighten;
+    }
+
+    public void setStraighten(float straighten) {
+        if (!rangeCheck(straighten)) {
+            straighten = Math.min(Math.max(straighten, -45), 45);
+        }
+        mStraighten = straighten;
+    }
+
+    @Override
+    public boolean allowsSingleInstanceOnly() {
+        return true;
+    }
+
+    @Override
+    public FilterRepresentation copy() {
+        return new FilterStraightenRepresentation(this);
+    }
+
+    @Override
+    protected void copyAllParameters(FilterRepresentation representation) {
+        if (!(representation instanceof FilterStraightenRepresentation)) {
+            throw new IllegalArgumentException("calling copyAllParameters with incompatible types!");
+        }
+        super.copyAllParameters(representation);
+        representation.useParametersFrom(this);
+    }
+
+    @Override
+    public void useParametersFrom(FilterRepresentation a) {
+        if (!(a instanceof FilterStraightenRepresentation)) {
+            throw new IllegalArgumentException("calling useParametersFrom with incompatible types!");
+        }
+        setStraighten(((FilterStraightenRepresentation) a).getStraighten());
+    }
+
+    @Override
+    public boolean isNil() {
+        return mStraighten == 0;
+    }
+
+    @Override
+    public void serializeRepresentation(JsonWriter writer) throws IOException {
+        writer.beginObject();
+        writer.name(SERIALIZATION_STRAIGHTEN_VALUE).value(mStraighten);
+        writer.endObject();
+    }
+
+    @Override
+    public void deSerializeRepresentation(JsonReader reader) throws IOException {
+        boolean unset = true;
+        reader.beginObject();
+        while (reader.hasNext()) {
+            String name = reader.nextName();
+            if (SERIALIZATION_STRAIGHTEN_VALUE.equals(name)) {
+                int s = reader.nextInt();
+                if (rangeCheck(s)) {
+                    setStraighten(s);
+                    unset = false;
+                }
+            } else {
+                reader.skipValue();
+            }
+        }
+        if (unset) {
+            Log.w(TAG, "WARNING: bad value when deserializing " + SERIALIZATION_NAME);
+        }
+        reader.endObject();
+    }
+
+    private boolean rangeCheck(float s) {
+        if (s < -45 || s > 45) {
+            return false;
+        }
+        return true;
+    }
+}
diff --git a/src/com/android/gallery3d/filtershow/pipeline/ImagePreset.java b/src/com/android/gallery3d/filtershow/pipeline/ImagePreset.java
index cc4938c..26dce37 100644
--- a/src/com/android/gallery3d/filtershow/pipeline/ImagePreset.java
+++ b/src/com/android/gallery3d/filtershow/pipeline/ImagePreset.java
@@ -26,9 +26,13 @@
 import com.android.gallery3d.R;
 import com.android.gallery3d.filtershow.cache.ImageLoader;
 import com.android.gallery3d.filtershow.filters.BaseFiltersManager;
+import com.android.gallery3d.filtershow.filters.FilterCropRepresentation;
 import com.android.gallery3d.filtershow.filters.FilterFxRepresentation;
 import com.android.gallery3d.filtershow.filters.FilterImageBorderRepresentation;
+import com.android.gallery3d.filtershow.filters.FilterMirrorRepresentation;
 import com.android.gallery3d.filtershow.filters.FilterRepresentation;
+import com.android.gallery3d.filtershow.filters.FilterRotateRepresentation;
+import com.android.gallery3d.filtershow.filters.FilterStraightenRepresentation;
 import com.android.gallery3d.filtershow.filters.FiltersManager;
 import com.android.gallery3d.filtershow.filters.ImageFilter;
 import com.android.gallery3d.filtershow.imageshow.GeometryMetadata;
@@ -659,8 +663,17 @@
     }
 
     FilterRepresentation creatFilterFromName(String name) {
+        // TODO: move these to FiltersManager pattern.
         if (GeometryMetadata.SERIALIZATION_NAME.equalsIgnoreCase(name)) {
             return new GeometryMetadata();
+        } else if (FilterRotateRepresentation.SERIALIZATION_NAME.equals(name)) {
+            return new FilterRotateRepresentation();
+        } else if (FilterMirrorRepresentation.SERIALIZATION_NAME.equals(name)) {
+            return new FilterMirrorRepresentation();
+        } else if (FilterStraightenRepresentation.SERIALIZATION_NAME.equals(name)) {
+            return new FilterStraightenRepresentation();
+        } else if (FilterCropRepresentation.SERIALIZATION_NAME.equals(name)) {
+            return new FilterCropRepresentation();
         }
         FiltersManager filtersManager = FiltersManager.getManager();
         return filtersManager.createFilterFromName(name);