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.R;
24 import com.android.gallery3d.filtershow.editors.EditorRotate;
25 import com.android.gallery3d.filtershow.editors.ImageOnlyEditor;
26 
27 import java.io.IOException;
28 
29 public class FilterRotateRepresentation extends FilterRepresentation {
30     public static final String SERIALIZATION_NAME = "ROTATION";
31     public static final String SERIALIZATION_ROTATE_VALUE = "value";
32     private static final String TAG = FilterRotateRepresentation.class.getSimpleName();
33 
34     Rotation mRotation;
35 
36     public enum Rotation {
37         ZERO(0), NINETY(90), ONE_EIGHTY(180), TWO_SEVENTY(270);
38         private final int mValue;
39 
Rotation(int value)40         private Rotation(int value) {
41             mValue = value;
42         }
43 
value()44         public int value() {
45             return mValue;
46         }
47 
fromValue(int value)48         public static Rotation fromValue(int value) {
49             switch (value) {
50                 case 0:
51                     return ZERO;
52                 case 90:
53                     return NINETY;
54                 case 180:
55                     return ONE_EIGHTY;
56                 case 270:
57                     return TWO_SEVENTY;
58                 default:
59                     return null;
60             }
61         }
62     }
63 
FilterRotateRepresentation(Rotation rotation)64     public FilterRotateRepresentation(Rotation rotation) {
65         super(SERIALIZATION_NAME);
66         setSerializationName(SERIALIZATION_NAME);
67         setShowParameterValue(false);
68         setFilterClass(FilterRotateRepresentation.class);
69         setFilterType(FilterRepresentation.TYPE_GEOMETRY);
70         setSupportsPartialRendering(true);
71         setTextId(R.string.rotate);
72         setEditorId(ImageOnlyEditor.ID);
73         setRotation(rotation);
74     }
75 
FilterRotateRepresentation(FilterRotateRepresentation r)76     public FilterRotateRepresentation(FilterRotateRepresentation r) {
77         this(r.getRotation());
78         setName(r.getName());
79     }
80 
FilterRotateRepresentation()81     public FilterRotateRepresentation() {
82         this(getNil());
83     }
84 
getRotation()85     public Rotation getRotation() {
86         return mRotation;
87     }
88 
rotateCW()89     public void rotateCW() {
90         switch(mRotation) {
91             case ZERO:
92                 mRotation = Rotation.NINETY;
93                 break;
94             case NINETY:
95                 mRotation = Rotation.ONE_EIGHTY;
96                 break;
97             case ONE_EIGHTY:
98                 mRotation = Rotation.TWO_SEVENTY;
99                 break;
100             case TWO_SEVENTY:
101                 mRotation = Rotation.ZERO;
102                 break;
103         }
104     }
105 
set(FilterRotateRepresentation r)106     public void set(FilterRotateRepresentation r) {
107         mRotation = r.mRotation;
108     }
109 
setRotation(Rotation rotation)110     public void setRotation(Rotation rotation) {
111         if (rotation == null) {
112             throw new IllegalArgumentException("Argument to setRotation is null");
113         }
114         mRotation = rotation;
115     }
116 
117     @Override
allowsSingleInstanceOnly()118     public boolean allowsSingleInstanceOnly() {
119         return true;
120     }
121 
122     @Override
copy()123     public FilterRepresentation copy() {
124         return new FilterRotateRepresentation(this);
125     }
126 
127     @Override
copyAllParameters(FilterRepresentation representation)128     protected void copyAllParameters(FilterRepresentation representation) {
129         if (!(representation instanceof FilterRotateRepresentation)) {
130             throw new IllegalArgumentException("calling copyAllParameters with incompatible types!");
131         }
132         super.copyAllParameters(representation);
133         representation.useParametersFrom(this);
134     }
135 
136     @Override
useParametersFrom(FilterRepresentation a)137     public void useParametersFrom(FilterRepresentation a) {
138         if (!(a instanceof FilterRotateRepresentation)) {
139             throw new IllegalArgumentException("calling useParametersFrom with incompatible types!");
140         }
141         setRotation(((FilterRotateRepresentation) a).getRotation());
142     }
143 
144     @Override
isNil()145     public boolean isNil() {
146         return mRotation == getNil();
147     }
148 
getNil()149     public static Rotation getNil() {
150         return Rotation.ZERO;
151     }
152 
153     @Override
serializeRepresentation(JsonWriter writer)154     public void serializeRepresentation(JsonWriter writer) throws IOException {
155         writer.beginObject();
156         writer.name(SERIALIZATION_ROTATE_VALUE).value(mRotation.value());
157         writer.endObject();
158     }
159 
160     @Override
equals(FilterRepresentation rep)161     public boolean equals(FilterRepresentation rep) {
162         if (!(rep instanceof FilterRotateRepresentation)) {
163             return false;
164         }
165         FilterRotateRepresentation rotate = (FilterRotateRepresentation) rep;
166         if (rotate.mRotation.value() != mRotation.value()) {
167             return false;
168         }
169         return true;
170     }
171 
172     @Override
deSerializeRepresentation(JsonReader reader)173     public void deSerializeRepresentation(JsonReader reader) throws IOException {
174         boolean unset = true;
175         reader.beginObject();
176         while (reader.hasNext()) {
177             String name = reader.nextName();
178             if (SERIALIZATION_ROTATE_VALUE.equals(name)) {
179                 Rotation r = Rotation.fromValue(reader.nextInt());
180                 if (r != null) {
181                     setRotation(r);
182                     unset = false;
183                 }
184             } else {
185                 reader.skipValue();
186             }
187         }
188         if (unset) {
189             Log.w(TAG, "WARNING: bad value when deserializing " + SERIALIZATION_NAME);
190         }
191         reader.endObject();
192     }
193 }
194