SnapdragonGallery: Fix memory leak in watermark

1. don't save historyItem of watermark
2. replace representation instead of add in ImagePreset.mFilter

Change-Id: Ibaeee715b777f5e9e37a2fd532b03fcdb0e50729
CRs-Fixed: 2146096
diff --git a/src/com/android/gallery3d/filtershow/FilterShowActivity.java b/src/com/android/gallery3d/filtershow/FilterShowActivity.java
index 670507e..e256115 100755
--- a/src/com/android/gallery3d/filtershow/FilterShowActivity.java
+++ b/src/com/android/gallery3d/filtershow/FilterShowActivity.java
@@ -1387,6 +1387,8 @@
         if (filterRepresentation.getFilterType() == FilterWatermarkRepresentation.TYPE_WATERMARK_CATEGORY) {
             return;
         }
+        boolean addToHistory = filterRepresentation.getFilterType()
+                != FilterWatermarkRepresentation.TYPE_WATERMARK;
         if (filterRepresentation instanceof FilterUserPresetRepresentation
                 || filterRepresentation instanceof FilterRotateRepresentation
                 || filterRepresentation instanceof FilterMirrorRepresentation) {
@@ -1411,7 +1413,7 @@
                 }
             }
         }
-        MasterImage.getImage().setPreset(copy, filterRepresentation, true);
+        MasterImage.getImage().setPreset(copy, filterRepresentation, addToHistory);
         MasterImage.getImage().setCurrentFilterRepresentation(filterRepresentation);
     }
 
@@ -1470,6 +1472,11 @@
             }
         }
         if (representation.getFilterType() == FilterRepresentation.TYPE_WATERMARK) {
+            if (MasterImage.getImage().getCurrentFilterRepresentation() != null
+                    && representation.getSerializationName().equals(MasterImage.getImage()
+                    .getCurrentFilterRepresentation().getSerializationName())) {
+                return;
+            }
             showWaterMark(representation);
         }
         if (TrueScannerActs.SERIALIZATION_NAME.equals(representation.getSerializationName())) {
diff --git a/src/com/android/gallery3d/filtershow/filters/FilterRepresentation.java b/src/com/android/gallery3d/filtershow/filters/FilterRepresentation.java
index b33491f..dae2de8 100644
--- a/src/com/android/gallery3d/filtershow/filters/FilterRepresentation.java
+++ b/src/com/android/gallery3d/filtershow/filters/FilterRepresentation.java
@@ -17,6 +17,7 @@
 package com.android.gallery3d.filtershow.filters;
 
 import android.content.res.Resources;
+import android.text.TextUtils;
 import android.util.JsonReader;
 import android.util.JsonWriter;
 import android.util.Log;
@@ -67,6 +68,10 @@
     public FilterRepresentation copy(){
         FilterRepresentation representation = new FilterRepresentation(mName);
         representation.useParametersFrom(this);
+        if (getFilterType() == TYPE_WATERMARK) {
+            representation.setSerializationName(getSerializationName());
+            representation.setFilterType(TYPE_WATERMARK);
+        }
         return representation;
     }
 
diff --git a/src/com/android/gallery3d/filtershow/pipeline/ImagePreset.java b/src/com/android/gallery3d/filtershow/pipeline/ImagePreset.java
index 484d8f2..2bd1216 100644
--- a/src/com/android/gallery3d/filtershow/pipeline/ImagePreset.java
+++ b/src/com/android/gallery3d/filtershow/pipeline/ImagePreset.java
@@ -444,6 +444,20 @@
             if (!replaced && !isNonePresetFilter(representation)) {
                 mFilters.add(representation);
             }
+        } else if (representation.getFilterType() == FilterRepresentation.TYPE_WATERMARK) {
+            boolean replaced = false;
+            for (int i = 0; i < mFilters.size(); i++) {
+                FilterRepresentation current = mFilters.elementAt(i);
+                if (current.getFilterType() == FilterRepresentation.TYPE_WATERMARK) {
+                    mFilters.remove(i);
+                    replaced = true;
+                    mFilters.add(i, representation);
+                    break;
+                }
+            }
+            if (!replaced) {
+                mFilters.add(representation);
+            }
         } else {
             mFilters.add(representation);
         }