Home | History | Annotate | Download | only in filters
      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 
     20 import android.util.Log;
     21 
     22 import com.android.gallery3d.filtershow.controller.Control;
     23 import com.android.gallery3d.filtershow.controller.FilterView;
     24 import com.android.gallery3d.filtershow.controller.Parameter;
     25 import com.android.gallery3d.filtershow.controller.ParameterInteger;
     26 
     27 public class FilterBasicRepresentation extends FilterRepresentation implements ParameterInteger {
     28     private static final String LOGTAG = "FilterBasicRep";
     29     private int mMinimum;
     30     private int mValue;
     31     private int mMaximum;
     32     private int mDefaultValue;
     33     private int mPreviewValue;
     34     public static final String SERIAL_NAME = "Name";
     35     public static final String SERIAL_VALUE = "Value";
     36     private boolean mLogVerbose = Log.isLoggable(LOGTAG, Log.VERBOSE);
     37 
     38     public FilterBasicRepresentation(String name, int minimum, int value, int maximum) {
     39         super(name);
     40         mMinimum = minimum;
     41         mMaximum = maximum;
     42         setValue(value);
     43     }
     44 
     45     @Override
     46     public String toString() {
     47         return getName() + " : " + mMinimum + " < " + mValue + " < " + mMaximum;
     48     }
     49 
     50     @Override
     51     public FilterRepresentation copy() {
     52         FilterBasicRepresentation representation = new FilterBasicRepresentation(getName(),0,0,0);
     53         copyAllParameters(representation);
     54         return representation;
     55     }
     56 
     57     @Override
     58     protected void copyAllParameters(FilterRepresentation representation) {
     59         super.copyAllParameters(representation);
     60         representation.useParametersFrom(this);
     61     }
     62 
     63     @Override
     64     public void useParametersFrom(FilterRepresentation a) {
     65         if (a instanceof FilterBasicRepresentation) {
     66             FilterBasicRepresentation representation = (FilterBasicRepresentation) a;
     67             setMinimum(representation.getMinimum());
     68             setMaximum(representation.getMaximum());
     69             setValue(representation.getValue());
     70             setDefaultValue(representation.getDefaultValue());
     71             setPreviewValue(representation.getPreviewValue());
     72         }
     73     }
     74 
     75     @Override
     76     public boolean equals(FilterRepresentation representation) {
     77         if (!super.equals(representation)) {
     78             return false;
     79         }
     80         if (representation instanceof FilterBasicRepresentation) {
     81             FilterBasicRepresentation basic = (FilterBasicRepresentation) representation;
     82             if (basic.mMinimum == mMinimum
     83                     && basic.mMaximum == mMaximum
     84                     && basic.mValue == mValue
     85                     && basic.mDefaultValue == mDefaultValue
     86                     && basic.mPreviewValue == mPreviewValue) {
     87                 return true;
     88             }
     89         }
     90         return false;
     91     }
     92 
     93     @Override
     94     public int getMinimum() {
     95         return mMinimum;
     96     }
     97 
     98     public void setMinimum(int minimum) {
     99         mMinimum = minimum;
    100     }
    101 
    102     @Override
    103     public int getValue() {
    104         return mValue;
    105     }
    106 
    107     @Override
    108     public void setValue(int value) {
    109         mValue = value;
    110         if (mValue < mMinimum) {
    111             mValue = mMinimum;
    112         }
    113         if (mValue > mMaximum) {
    114             mValue = mMaximum;
    115         }
    116     }
    117 
    118     @Override
    119     public int getMaximum() {
    120         return mMaximum;
    121     }
    122 
    123     public void setMaximum(int maximum) {
    124         mMaximum = maximum;
    125     }
    126 
    127     public void setDefaultValue(int defaultValue) {
    128         mDefaultValue = defaultValue;
    129     }
    130 
    131     @Override
    132     public int getDefaultValue() {
    133         return mDefaultValue;
    134     }
    135 
    136     public int getPreviewValue() {
    137         return mPreviewValue;
    138     }
    139 
    140     public void setPreviewValue(int previewValue) {
    141         mPreviewValue = previewValue;
    142     }
    143 
    144     @Override
    145     public String getStateRepresentation() {
    146         int val = getValue();
    147         return ((val > 0) ? "+" : "") + val;
    148     }
    149 
    150     @Override
    151     public String getParameterType(){
    152         return sParameterType;
    153     }
    154 
    155     @Override
    156     public void setController(Control control) {
    157     }
    158 
    159     @Override
    160     public String getValueString() {
    161         return getStateRepresentation();
    162     }
    163 
    164     @Override
    165     public String getParameterName() {
    166         return getName();
    167     }
    168 
    169     @Override
    170     public void setFilterView(FilterView editor) {
    171     }
    172 
    173     @Override
    174     public void copyFrom(Parameter src) {
    175         useParametersFrom((FilterBasicRepresentation) src);
    176     }
    177 
    178     @Override
    179     public String[][] serializeRepresentation() {
    180         String[][] ret = {
    181                 {SERIAL_NAME  , getName() },
    182                 {SERIAL_VALUE , Integer.toString(mValue)}};
    183         return ret;
    184     }
    185 
    186     @Override
    187     public void deSerializeRepresentation(String[][] rep) {
    188         super.deSerializeRepresentation(rep);
    189         for (int i = 0; i < rep.length; i++) {
    190             if (SERIAL_VALUE.equals(rep[i][0])) {
    191                 mValue = Integer.parseInt(rep[i][1]);
    192                 break;
    193             }
    194         }
    195     }
    196 }
    197