Fix blinking animation during Visibility transitions.

Bug 15618501

Change-Id: I9dff9eb386a4bfb51caec24f3f7cc0fad06ae04c
diff --git a/core/java/android/transition/Transition.java b/core/java/android/transition/Transition.java
index 508769d..e936232 100644
--- a/core/java/android/transition/Transition.java
+++ b/core/java/android/transition/Transition.java
@@ -673,7 +673,7 @@
                             startDelays.put(mAnimators.size(), delay);
                             minStartDelay = Math.min(delay, minStartDelay);
                         }
-                        AnimationInfo info = new AnimationInfo(view, getName(),
+                        AnimationInfo info = new AnimationInfo(view, getName(), this,
                                 sceneRoot.getWindowId(), infoValues);
                         runningAnimators.put(animator, info);
                         mAnimators.add(animator);
@@ -1587,30 +1587,10 @@
                 AnimationInfo oldInfo = runningAnimators.get(anim);
                 if (oldInfo != null && oldInfo.view != null &&
                         oldInfo.view.getContext() == sceneRoot.getContext()) {
-                    boolean cancel = false;
                     TransitionValues oldValues = oldInfo.values;
                     View oldView = oldInfo.view;
                     TransitionValues newValues = mEndValues.viewValues.get(oldView);
-                    if (oldValues != null) {
-                        // if oldValues null, then transition didn't care to stash values,
-                        // and won't get canceled
-                        if (newValues != null) {
-                            for (String key : oldValues.values.keySet()) {
-                                Object oldValue = oldValues.values.get(key);
-                                Object newValue = newValues.values.get(key);
-                                if (oldValue != null && newValue != null &&
-                                        !oldValue.equals(newValue)) {
-                                    cancel = true;
-                                    if (DBG) {
-                                        Log.d(LOG_TAG, "Transition.playTransition: " +
-                                                "oldValue != newValue for " + key +
-                                                ": old, new = " + oldValue + ", " + newValue);
-                                    }
-                                    break;
-                                }
-                            }
-                        }
-                    }
+                    boolean cancel = oldInfo.transition.areValuesChanged(oldValues, newValues);
                     if (cancel) {
                         if (anim.isRunning() || anim.isStarted()) {
                             if (DBG) {
@@ -1632,6 +1612,29 @@
         runAnimators();
     }
 
+    boolean areValuesChanged(TransitionValues oldValues, TransitionValues newValues) {
+        boolean valuesChanged = false;
+        // if oldValues null, then transition didn't care to stash values,
+        // and won't get canceled
+        if (oldValues != null && newValues != null) {
+            for (String key : oldValues.values.keySet()) {
+                Object oldValue = oldValues.values.get(key);
+                Object newValue = newValues.values.get(key);
+                if (oldValue != null && newValue != null &&
+                        !oldValue.equals(newValue)) {
+                    valuesChanged = true;
+                    if (DBG) {
+                        Log.d(LOG_TAG, "Transition.playTransition: " +
+                                "oldValue != newValue for " + key +
+                                ": old, new = " + oldValue + ", " + newValue);
+                    }
+                    break;
+                }
+            }
+        }
+        return valuesChanged;
+    }
+
     /**
      * This is a utility method used by subclasses to handle standard parts of
      * setting up and running an Animator: it sets the {@link #getDuration()
@@ -2070,12 +2073,15 @@
         String name;
         TransitionValues values;
         WindowId windowId;
+        Transition transition;
 
-        AnimationInfo(View view, String name, WindowId windowId, TransitionValues values) {
+        AnimationInfo(View view, String name, Transition transition,
+                WindowId windowId, TransitionValues values) {
             this.view = view;
             this.name = name;
             this.values = values;
             this.windowId = windowId;
+            this.transition = transition;
         }
     }
 
diff --git a/core/java/android/transition/Visibility.java b/core/java/android/transition/Visibility.java
index c6c8337..947e1a7 100644
--- a/core/java/android/transition/Visibility.java
+++ b/core/java/android/transition/Visibility.java
@@ -331,16 +331,6 @@
                     public void onAnimationEnd(Animator animation) {
                         finalSceneRoot.getOverlay().remove(finalOverlayView);
                     }
-
-                    @Override
-                    public void onAnimationPause(Animator animation) {
-                        finalSceneRoot.getOverlay().remove(finalOverlayView);
-                    }
-
-                    @Override
-                    public void onAnimationResume(Animator animation) {
-                        finalSceneRoot.getOverlay().add(finalOverlayView);
-                    }
                 });
             }
             return animator;
@@ -409,6 +399,16 @@
         return overlayView;
     }
 
+    @Override
+    boolean areValuesChanged(TransitionValues oldValues, TransitionValues newValues) {
+        VisibilityInfo changeInfo = getVisibilityChangeInfo(oldValues, newValues);
+        if (oldValues == null && newValues == null) {
+            return false;
+        }
+        return changeInfo.visibilityChange && (changeInfo.startVisibility == View.VISIBLE ||
+            changeInfo.endVisibility == View.VISIBLE);
+    }
+
     /**
      * The default implementation of this method returns a null Animator. Subclasses should
      * override this method to make targets disappear with the desired transition. The