Manage drawable invalidation automatically for Overlays

Drawables added to a view's Overlay will now cause the Overlay to
be invalidated via the normal drawable-invalidation mechanism. That is,
changes to any of the drawables in the overlay should cause invalidation of
the proper area of the overlay and thus the hostView, causing the appropriate
area to be redrawn.

Also, fixed a bug in drawable invalidation so that bounds changes will now
correctly invalidate both the old and new bounds areas.

Issue #8350510 Add APIs needed for future animation capabilities

Change-Id: Icae5fa0e420232ee17dc39be10084345bae8dbd8
diff --git a/core/java/android/view/Overlay.java b/core/java/android/view/Overlay.java
index f15d4d2..210bc31 100644
--- a/core/java/android/view/Overlay.java
+++ b/core/java/android/view/Overlay.java
@@ -20,11 +20,7 @@
 /**
  * An overlay is an extra layer that sits on top of a View (the "host view") which is drawn after
  * all other content in that view (including children, if the view is a ViewGroup). Interaction
- * with the overlay layer is done in terms of adding/removing views and drawables. Invalidation and
- * redrawing of the overlay layer (and its host view) is handled differently for views versus
- * drawables in the overlay. Views invalidate themselves as usual, causing appropriate redrawing
- * to occur automatically. Drawables, on the other hand, do not manage invalidation, so changes to
- * drawable objects should be accompanied by appropriate calls to invalidate() on the host view.
+ * with the overlay layer is done in terms of adding/removing views and drawables.
  *
  * @see android.view.View#getOverlay()
  */
@@ -33,9 +29,7 @@
     /**
      * Adds a Drawable to the overlay. The bounds of the drawable should be relative to
      * the host view. Any drawable added to the overlay should be removed when it is no longer
-     * needed or no longer visible. There is no automatic invalidation of the host view; changes to
-     * the drawable should be accompanied by appropriate invalidation calls to the host view
-     * to cause the proper area of the view, and the overlay, to be redrawn.
+     * needed or no longer visible.
      *
      * @param drawable The Drawable to be added to the overlay. This drawable will be
      * drawn when the view redraws its overlay.
diff --git a/core/java/android/view/ViewOverlay.java b/core/java/android/view/ViewOverlay.java
index 8c2ab9d..939377d 100644
--- a/core/java/android/view/ViewOverlay.java
+++ b/core/java/android/view/ViewOverlay.java
@@ -26,8 +26,8 @@
  * ViewOverlay is a container that View uses to host all objects (views and drawables) that
  * are added to its "overlay", gotten through {@link View#getOverlay()}. Views and drawables are
  * added to the overlay via the add/remove methods in this class. These views and drawables are
- * then drawn whenever the view itself is drawn, after which it will draw its overlay (if it
- * exists).
+ * drawn whenever the view itself is drawn; first the view draws its own content (and children,
+ * if it is a ViewGroup), then it draws its overlay (if it has one).
  *
  * Besides managing and drawing the list of drawables, this class serves two purposes:
  * (1) it noops layout calls because children are absolutely positioned and
@@ -65,6 +65,7 @@
             // Make each drawable unique in the overlay; can't add it more than once
             mDrawables.add(drawable);
             invalidate(drawable.getBounds());
+            drawable.setCallback(this);
         }
     }
 
@@ -73,10 +74,16 @@
         if (mDrawables != null) {
             mDrawables.remove(drawable);
             invalidate(drawable.getBounds());
+            drawable.setCallback(null);
         }
     }
 
     @Override
+    public void invalidateDrawable(Drawable drawable) {
+        invalidate(drawable.getBounds());
+    }
+
+    @Override
     public void add(View child) {
         super.addView(child);
     }
diff --git a/graphics/java/android/graphics/drawable/Drawable.java b/graphics/java/android/graphics/drawable/Drawable.java
index 37f2250..c90f400 100644
--- a/graphics/java/android/graphics/drawable/Drawable.java
+++ b/graphics/java/android/graphics/drawable/Drawable.java
@@ -146,6 +146,10 @@
 
         if (oldBounds.left != left || oldBounds.top != top ||
                 oldBounds.right != right || oldBounds.bottom != bottom) {
+            if (!oldBounds.isEmpty()) {
+                // first invalidate the previous bounds
+                invalidateSelf();
+            }
             mBounds.set(left, top, right, bottom);
             onBoundsChange(mBounds);
         }