176a8b489a62ed82919c8b6339123488c09fc0168Ruben Brunk/* 276a8b489a62ed82919c8b6339123488c09fc0168Ruben Brunk * Copyright (C) 2013 The Android Open Source Project 376a8b489a62ed82919c8b6339123488c09fc0168Ruben Brunk * 476a8b489a62ed82919c8b6339123488c09fc0168Ruben Brunk * Licensed under the Apache License, Version 2.0 (the "License"); 576a8b489a62ed82919c8b6339123488c09fc0168Ruben Brunk * you may not use this file except in compliance with the License. 676a8b489a62ed82919c8b6339123488c09fc0168Ruben Brunk * You may obtain a copy of the License at 776a8b489a62ed82919c8b6339123488c09fc0168Ruben Brunk * 876a8b489a62ed82919c8b6339123488c09fc0168Ruben Brunk * http://www.apache.org/licenses/LICENSE-2.0 976a8b489a62ed82919c8b6339123488c09fc0168Ruben Brunk * 1076a8b489a62ed82919c8b6339123488c09fc0168Ruben Brunk * Unless required by applicable law or agreed to in writing, software 1176a8b489a62ed82919c8b6339123488c09fc0168Ruben Brunk * distributed under the License is distributed on an "AS IS" BASIS, 1276a8b489a62ed82919c8b6339123488c09fc0168Ruben Brunk * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 1376a8b489a62ed82919c8b6339123488c09fc0168Ruben Brunk * See the License for the specific language governing permissions and 1476a8b489a62ed82919c8b6339123488c09fc0168Ruben Brunk * limitations under the License. 1576a8b489a62ed82919c8b6339123488c09fc0168Ruben Brunk */ 1676a8b489a62ed82919c8b6339123488c09fc0168Ruben Brunk 1776a8b489a62ed82919c8b6339123488c09fc0168Ruben Brunkpackage com.android.gallery3d.filtershow.filters; 1876a8b489a62ed82919c8b6339123488c09fc0168Ruben Brunk 1976a8b489a62ed82919c8b6339123488c09fc0168Ruben Brunkimport android.util.JsonReader; 2076a8b489a62ed82919c8b6339123488c09fc0168Ruben Brunkimport android.util.JsonWriter; 2176a8b489a62ed82919c8b6339123488c09fc0168Ruben Brunkimport android.util.Log; 2276a8b489a62ed82919c8b6339123488c09fc0168Ruben Brunk 2376a8b489a62ed82919c8b6339123488c09fc0168Ruben Brunkimport com.android.gallery3d.R; 2476a8b489a62ed82919c8b6339123488c09fc0168Ruben Brunkimport com.android.gallery3d.filtershow.editors.EditorRotate; 2557bd092bd5757576f07341135272ed1c3df729ffnicolasroardimport com.android.gallery3d.filtershow.editors.ImageOnlyEditor; 2676a8b489a62ed82919c8b6339123488c09fc0168Ruben Brunk 2776a8b489a62ed82919c8b6339123488c09fc0168Ruben Brunkimport java.io.IOException; 2876a8b489a62ed82919c8b6339123488c09fc0168Ruben Brunk 2976a8b489a62ed82919c8b6339123488c09fc0168Ruben Brunkpublic class FilterRotateRepresentation extends FilterRepresentation { 3076a8b489a62ed82919c8b6339123488c09fc0168Ruben Brunk public static final String SERIALIZATION_NAME = "ROTATION"; 3176a8b489a62ed82919c8b6339123488c09fc0168Ruben Brunk public static final String SERIALIZATION_ROTATE_VALUE = "value"; 3276a8b489a62ed82919c8b6339123488c09fc0168Ruben Brunk private static final String TAG = FilterRotateRepresentation.class.getSimpleName(); 3376a8b489a62ed82919c8b6339123488c09fc0168Ruben Brunk 34b0f7a8f7f7d95ae12e92f529fd9a8a37f75b105cRuben Brunk Rotation mRotation; 3576a8b489a62ed82919c8b6339123488c09fc0168Ruben Brunk 3676a8b489a62ed82919c8b6339123488c09fc0168Ruben Brunk public enum Rotation { 3776a8b489a62ed82919c8b6339123488c09fc0168Ruben Brunk ZERO(0), NINETY(90), ONE_EIGHTY(180), TWO_SEVENTY(270); 3876a8b489a62ed82919c8b6339123488c09fc0168Ruben Brunk private final int mValue; 3976a8b489a62ed82919c8b6339123488c09fc0168Ruben Brunk 4076a8b489a62ed82919c8b6339123488c09fc0168Ruben Brunk private Rotation(int value) { 4176a8b489a62ed82919c8b6339123488c09fc0168Ruben Brunk mValue = value; 4276a8b489a62ed82919c8b6339123488c09fc0168Ruben Brunk } 4376a8b489a62ed82919c8b6339123488c09fc0168Ruben Brunk 4476a8b489a62ed82919c8b6339123488c09fc0168Ruben Brunk public int value() { 4576a8b489a62ed82919c8b6339123488c09fc0168Ruben Brunk return mValue; 4676a8b489a62ed82919c8b6339123488c09fc0168Ruben Brunk } 4776a8b489a62ed82919c8b6339123488c09fc0168Ruben Brunk 4876a8b489a62ed82919c8b6339123488c09fc0168Ruben Brunk public static Rotation fromValue(int value) { 4976a8b489a62ed82919c8b6339123488c09fc0168Ruben Brunk switch (value) { 5076a8b489a62ed82919c8b6339123488c09fc0168Ruben Brunk case 0: 5176a8b489a62ed82919c8b6339123488c09fc0168Ruben Brunk return ZERO; 5276a8b489a62ed82919c8b6339123488c09fc0168Ruben Brunk case 90: 5376a8b489a62ed82919c8b6339123488c09fc0168Ruben Brunk return NINETY; 5476a8b489a62ed82919c8b6339123488c09fc0168Ruben Brunk case 180: 5576a8b489a62ed82919c8b6339123488c09fc0168Ruben Brunk return ONE_EIGHTY; 5676a8b489a62ed82919c8b6339123488c09fc0168Ruben Brunk case 270: 5776a8b489a62ed82919c8b6339123488c09fc0168Ruben Brunk return TWO_SEVENTY; 5876a8b489a62ed82919c8b6339123488c09fc0168Ruben Brunk default: 5976a8b489a62ed82919c8b6339123488c09fc0168Ruben Brunk return null; 6076a8b489a62ed82919c8b6339123488c09fc0168Ruben Brunk } 6176a8b489a62ed82919c8b6339123488c09fc0168Ruben Brunk } 6276a8b489a62ed82919c8b6339123488c09fc0168Ruben Brunk } 6376a8b489a62ed82919c8b6339123488c09fc0168Ruben Brunk 6476a8b489a62ed82919c8b6339123488c09fc0168Ruben Brunk public FilterRotateRepresentation(Rotation rotation) { 653bc6e6f3b5ac0bb989c5e2f1580bdb40ead5dc25nicolasroard super(SERIALIZATION_NAME); 6676a8b489a62ed82919c8b6339123488c09fc0168Ruben Brunk setSerializationName(SERIALIZATION_NAME); 6757bd092bd5757576f07341135272ed1c3df729ffnicolasroard setShowParameterValue(false); 6876a8b489a62ed82919c8b6339123488c09fc0168Ruben Brunk setFilterClass(FilterRotateRepresentation.class); 6976a8b489a62ed82919c8b6339123488c09fc0168Ruben Brunk setFilterType(FilterRepresentation.TYPE_GEOMETRY); 70430e46b06f8e7ee1ca3e7ecdcef3e0a978637c03nicolasroard setSupportsPartialRendering(true); 7176a8b489a62ed82919c8b6339123488c09fc0168Ruben Brunk setTextId(R.string.rotate); 7257bd092bd5757576f07341135272ed1c3df729ffnicolasroard setEditorId(ImageOnlyEditor.ID); 7376a8b489a62ed82919c8b6339123488c09fc0168Ruben Brunk setRotation(rotation); 7476a8b489a62ed82919c8b6339123488c09fc0168Ruben Brunk } 7576a8b489a62ed82919c8b6339123488c09fc0168Ruben Brunk 7676a8b489a62ed82919c8b6339123488c09fc0168Ruben Brunk public FilterRotateRepresentation(FilterRotateRepresentation r) { 7776a8b489a62ed82919c8b6339123488c09fc0168Ruben Brunk this(r.getRotation()); 783bc6e6f3b5ac0bb989c5e2f1580bdb40ead5dc25nicolasroard setName(r.getName()); 7976a8b489a62ed82919c8b6339123488c09fc0168Ruben Brunk } 8076a8b489a62ed82919c8b6339123488c09fc0168Ruben Brunk 8176a8b489a62ed82919c8b6339123488c09fc0168Ruben Brunk public FilterRotateRepresentation() { 82b0f7a8f7f7d95ae12e92f529fd9a8a37f75b105cRuben Brunk this(getNil()); 8376a8b489a62ed82919c8b6339123488c09fc0168Ruben Brunk } 8476a8b489a62ed82919c8b6339123488c09fc0168Ruben Brunk 8576a8b489a62ed82919c8b6339123488c09fc0168Ruben Brunk public Rotation getRotation() { 8676a8b489a62ed82919c8b6339123488c09fc0168Ruben Brunk return mRotation; 8776a8b489a62ed82919c8b6339123488c09fc0168Ruben Brunk } 8876a8b489a62ed82919c8b6339123488c09fc0168Ruben Brunk 89b0f7a8f7f7d95ae12e92f529fd9a8a37f75b105cRuben Brunk public void rotateCW() { 90b0f7a8f7f7d95ae12e92f529fd9a8a37f75b105cRuben Brunk switch(mRotation) { 91b0f7a8f7f7d95ae12e92f529fd9a8a37f75b105cRuben Brunk case ZERO: 92b0f7a8f7f7d95ae12e92f529fd9a8a37f75b105cRuben Brunk mRotation = Rotation.NINETY; 93b0f7a8f7f7d95ae12e92f529fd9a8a37f75b105cRuben Brunk break; 94b0f7a8f7f7d95ae12e92f529fd9a8a37f75b105cRuben Brunk case NINETY: 95b0f7a8f7f7d95ae12e92f529fd9a8a37f75b105cRuben Brunk mRotation = Rotation.ONE_EIGHTY; 96b0f7a8f7f7d95ae12e92f529fd9a8a37f75b105cRuben Brunk break; 97b0f7a8f7f7d95ae12e92f529fd9a8a37f75b105cRuben Brunk case ONE_EIGHTY: 98b0f7a8f7f7d95ae12e92f529fd9a8a37f75b105cRuben Brunk mRotation = Rotation.TWO_SEVENTY; 99b0f7a8f7f7d95ae12e92f529fd9a8a37f75b105cRuben Brunk break; 100b0f7a8f7f7d95ae12e92f529fd9a8a37f75b105cRuben Brunk case TWO_SEVENTY: 101b0f7a8f7f7d95ae12e92f529fd9a8a37f75b105cRuben Brunk mRotation = Rotation.ZERO; 102b0f7a8f7f7d95ae12e92f529fd9a8a37f75b105cRuben Brunk break; 103b0f7a8f7f7d95ae12e92f529fd9a8a37f75b105cRuben Brunk } 104b0f7a8f7f7d95ae12e92f529fd9a8a37f75b105cRuben Brunk } 105b0f7a8f7f7d95ae12e92f529fd9a8a37f75b105cRuben Brunk 10676a8b489a62ed82919c8b6339123488c09fc0168Ruben Brunk public void set(FilterRotateRepresentation r) { 10776a8b489a62ed82919c8b6339123488c09fc0168Ruben Brunk mRotation = r.mRotation; 10876a8b489a62ed82919c8b6339123488c09fc0168Ruben Brunk } 10976a8b489a62ed82919c8b6339123488c09fc0168Ruben Brunk 11076a8b489a62ed82919c8b6339123488c09fc0168Ruben Brunk public void setRotation(Rotation rotation) { 11176a8b489a62ed82919c8b6339123488c09fc0168Ruben Brunk if (rotation == null) { 11276a8b489a62ed82919c8b6339123488c09fc0168Ruben Brunk throw new IllegalArgumentException("Argument to setRotation is null"); 11376a8b489a62ed82919c8b6339123488c09fc0168Ruben Brunk } 11476a8b489a62ed82919c8b6339123488c09fc0168Ruben Brunk mRotation = rotation; 11576a8b489a62ed82919c8b6339123488c09fc0168Ruben Brunk } 11676a8b489a62ed82919c8b6339123488c09fc0168Ruben Brunk 11776a8b489a62ed82919c8b6339123488c09fc0168Ruben Brunk @Override 11876a8b489a62ed82919c8b6339123488c09fc0168Ruben Brunk public boolean allowsSingleInstanceOnly() { 11976a8b489a62ed82919c8b6339123488c09fc0168Ruben Brunk return true; 12076a8b489a62ed82919c8b6339123488c09fc0168Ruben Brunk } 12176a8b489a62ed82919c8b6339123488c09fc0168Ruben Brunk 12276a8b489a62ed82919c8b6339123488c09fc0168Ruben Brunk @Override 12376a8b489a62ed82919c8b6339123488c09fc0168Ruben Brunk public FilterRepresentation copy() { 12476a8b489a62ed82919c8b6339123488c09fc0168Ruben Brunk return new FilterRotateRepresentation(this); 12576a8b489a62ed82919c8b6339123488c09fc0168Ruben Brunk } 12676a8b489a62ed82919c8b6339123488c09fc0168Ruben Brunk 12776a8b489a62ed82919c8b6339123488c09fc0168Ruben Brunk @Override 12876a8b489a62ed82919c8b6339123488c09fc0168Ruben Brunk protected void copyAllParameters(FilterRepresentation representation) { 12976a8b489a62ed82919c8b6339123488c09fc0168Ruben Brunk if (!(representation instanceof FilterRotateRepresentation)) { 13076a8b489a62ed82919c8b6339123488c09fc0168Ruben Brunk throw new IllegalArgumentException("calling copyAllParameters with incompatible types!"); 13176a8b489a62ed82919c8b6339123488c09fc0168Ruben Brunk } 13276a8b489a62ed82919c8b6339123488c09fc0168Ruben Brunk super.copyAllParameters(representation); 13376a8b489a62ed82919c8b6339123488c09fc0168Ruben Brunk representation.useParametersFrom(this); 13476a8b489a62ed82919c8b6339123488c09fc0168Ruben Brunk } 13576a8b489a62ed82919c8b6339123488c09fc0168Ruben Brunk 13676a8b489a62ed82919c8b6339123488c09fc0168Ruben Brunk @Override 13776a8b489a62ed82919c8b6339123488c09fc0168Ruben Brunk public void useParametersFrom(FilterRepresentation a) { 13876a8b489a62ed82919c8b6339123488c09fc0168Ruben Brunk if (!(a instanceof FilterRotateRepresentation)) { 13976a8b489a62ed82919c8b6339123488c09fc0168Ruben Brunk throw new IllegalArgumentException("calling useParametersFrom with incompatible types!"); 14076a8b489a62ed82919c8b6339123488c09fc0168Ruben Brunk } 14176a8b489a62ed82919c8b6339123488c09fc0168Ruben Brunk setRotation(((FilterRotateRepresentation) a).getRotation()); 14276a8b489a62ed82919c8b6339123488c09fc0168Ruben Brunk } 14376a8b489a62ed82919c8b6339123488c09fc0168Ruben Brunk 14476a8b489a62ed82919c8b6339123488c09fc0168Ruben Brunk @Override 14576a8b489a62ed82919c8b6339123488c09fc0168Ruben Brunk public boolean isNil() { 146b0f7a8f7f7d95ae12e92f529fd9a8a37f75b105cRuben Brunk return mRotation == getNil(); 147b0f7a8f7f7d95ae12e92f529fd9a8a37f75b105cRuben Brunk } 148b0f7a8f7f7d95ae12e92f529fd9a8a37f75b105cRuben Brunk 149b0f7a8f7f7d95ae12e92f529fd9a8a37f75b105cRuben Brunk public static Rotation getNil() { 150b0f7a8f7f7d95ae12e92f529fd9a8a37f75b105cRuben Brunk return Rotation.ZERO; 15176a8b489a62ed82919c8b6339123488c09fc0168Ruben Brunk } 15276a8b489a62ed82919c8b6339123488c09fc0168Ruben Brunk 15376a8b489a62ed82919c8b6339123488c09fc0168Ruben Brunk @Override 15476a8b489a62ed82919c8b6339123488c09fc0168Ruben Brunk public void serializeRepresentation(JsonWriter writer) throws IOException { 15576a8b489a62ed82919c8b6339123488c09fc0168Ruben Brunk writer.beginObject(); 15676a8b489a62ed82919c8b6339123488c09fc0168Ruben Brunk writer.name(SERIALIZATION_ROTATE_VALUE).value(mRotation.value()); 15776a8b489a62ed82919c8b6339123488c09fc0168Ruben Brunk writer.endObject(); 15876a8b489a62ed82919c8b6339123488c09fc0168Ruben Brunk } 15976a8b489a62ed82919c8b6339123488c09fc0168Ruben Brunk 16076a8b489a62ed82919c8b6339123488c09fc0168Ruben Brunk @Override 1618f442fae60e0154867d2a6927eb9a35bcb7014e6nicolasroard public boolean equals(FilterRepresentation rep) { 1628f442fae60e0154867d2a6927eb9a35bcb7014e6nicolasroard if (!(rep instanceof FilterRotateRepresentation)) { 1638f442fae60e0154867d2a6927eb9a35bcb7014e6nicolasroard return false; 1648f442fae60e0154867d2a6927eb9a35bcb7014e6nicolasroard } 1658f442fae60e0154867d2a6927eb9a35bcb7014e6nicolasroard FilterRotateRepresentation rotate = (FilterRotateRepresentation) rep; 1668f442fae60e0154867d2a6927eb9a35bcb7014e6nicolasroard if (rotate.mRotation.value() != mRotation.value()) { 1678f442fae60e0154867d2a6927eb9a35bcb7014e6nicolasroard return false; 1688f442fae60e0154867d2a6927eb9a35bcb7014e6nicolasroard } 1698f442fae60e0154867d2a6927eb9a35bcb7014e6nicolasroard return true; 1708f442fae60e0154867d2a6927eb9a35bcb7014e6nicolasroard } 1718f442fae60e0154867d2a6927eb9a35bcb7014e6nicolasroard 1728f442fae60e0154867d2a6927eb9a35bcb7014e6nicolasroard @Override 17376a8b489a62ed82919c8b6339123488c09fc0168Ruben Brunk public void deSerializeRepresentation(JsonReader reader) throws IOException { 17476a8b489a62ed82919c8b6339123488c09fc0168Ruben Brunk boolean unset = true; 17576a8b489a62ed82919c8b6339123488c09fc0168Ruben Brunk reader.beginObject(); 17676a8b489a62ed82919c8b6339123488c09fc0168Ruben Brunk while (reader.hasNext()) { 17776a8b489a62ed82919c8b6339123488c09fc0168Ruben Brunk String name = reader.nextName(); 17876a8b489a62ed82919c8b6339123488c09fc0168Ruben Brunk if (SERIALIZATION_ROTATE_VALUE.equals(name)) { 17976a8b489a62ed82919c8b6339123488c09fc0168Ruben Brunk Rotation r = Rotation.fromValue(reader.nextInt()); 18076a8b489a62ed82919c8b6339123488c09fc0168Ruben Brunk if (r != null) { 18176a8b489a62ed82919c8b6339123488c09fc0168Ruben Brunk setRotation(r); 18276a8b489a62ed82919c8b6339123488c09fc0168Ruben Brunk unset = false; 18376a8b489a62ed82919c8b6339123488c09fc0168Ruben Brunk } 18476a8b489a62ed82919c8b6339123488c09fc0168Ruben Brunk } else { 18576a8b489a62ed82919c8b6339123488c09fc0168Ruben Brunk reader.skipValue(); 18676a8b489a62ed82919c8b6339123488c09fc0168Ruben Brunk } 18776a8b489a62ed82919c8b6339123488c09fc0168Ruben Brunk } 18876a8b489a62ed82919c8b6339123488c09fc0168Ruben Brunk if (unset) { 18976a8b489a62ed82919c8b6339123488c09fc0168Ruben Brunk Log.w(TAG, "WARNING: bad value when deserializing " + SERIALIZATION_NAME); 19076a8b489a62ed82919c8b6339123488c09fc0168Ruben Brunk } 19176a8b489a62ed82919c8b6339123488c09fc0168Ruben Brunk reader.endObject(); 19276a8b489a62ed82919c8b6339123488c09fc0168Ruben Brunk } 19376a8b489a62ed82919c8b6339123488c09fc0168Ruben Brunk} 194