From b4b939cf1af397cd223ac7ebb12e1d2d61e2a83e Mon Sep 17 00:00:00 2001 From: Kirill Grouchnikov Date: Tue, 8 Mar 2016 10:37:06 -0500 Subject: [PATCH] Tweaks to ViewOverlay. * Clarify Javadocs for add / remove when called with the same Drawable. * Add @NonNull annotation to all add / remove APIs and throw IllegalArgumentException when null value is passed. Bug: 27528798 Change-Id: Ied8f28c70775309e4fa85aff6a7202c1a0eb6aa3 --- core/java/android/view/ViewGroupOverlay.java | 22 ++++++--- core/java/android/view/ViewOverlay.java | 49 ++++++++++++++------ 2 files changed, 50 insertions(+), 21 deletions(-) diff --git a/core/java/android/view/ViewGroupOverlay.java b/core/java/android/view/ViewGroupOverlay.java index 16afc5df31ae4..c2807babdff2f 100644 --- a/core/java/android/view/ViewGroupOverlay.java +++ b/core/java/android/view/ViewGroupOverlay.java @@ -15,6 +15,7 @@ */ package android.view; +import android.annotation.NonNull; import android.content.Context; import android.graphics.drawable.Drawable; @@ -37,7 +38,7 @@ public class ViewGroupOverlay extends ViewOverlay { } /** - * Adds a View to the overlay. The bounds of the added view should be + * Adds a {@code View} to the overlay. The bounds of the added view should be * relative to the host view. Any view added to the overlay should be * removed when it is no longer needed or no longer visible. * @@ -54,23 +55,32 @@ public class ViewGroupOverlay extends ViewOverlay { * and 200 pixels down from the origin of the overlay's * host view, then the view will be offset by (100, 200).

* - * @param view The View to be added to the overlay. The added view will be + *

{@code View}s added with this API will be drawn in the order they were + * added. Drawing of the overlay views will happen before drawing of any of the + * {@code Drawable}s added with {@link #add(Drawable)} API even if a call to + * this API happened after the call to {@link #add(Drawable)}.

+ * + *

Passing null parameter will result in an + * {@link IllegalArgumentException} being thrown.

+ * + * @param view The {@code View} to be added to the overlay. The added view will be * drawn when the overlay is drawn. * @see #remove(View) * @see ViewOverlay#add(Drawable) */ - public void add(View view) { + public void add(@NonNull View view) { mOverlayViewGroup.add(view); } /** - * Removes the specified View from the overlay. + * Removes the specified {@code View} from the overlay. Passing null parameter + * will result in an {@link IllegalArgumentException} being thrown. * - * @param view The View to be removed from the overlay. + * @param view The {@code View} to be removed from the overlay. * @see #add(View) * @see ViewOverlay#remove(Drawable) */ - public void remove(View view) { + public void remove(@NonNull View view) { mOverlayViewGroup.remove(view); } } diff --git a/core/java/android/view/ViewOverlay.java b/core/java/android/view/ViewOverlay.java index 5e5ef299a7990..0d05c542215dd 100644 --- a/core/java/android/view/ViewOverlay.java +++ b/core/java/android/view/ViewOverlay.java @@ -16,6 +16,7 @@ package android.view; import android.animation.LayoutTransition; +import android.annotation.NonNull; import android.content.Context; import android.graphics.Canvas; import android.graphics.Rect; @@ -59,25 +60,30 @@ public class ViewOverlay { } /** - * Adds a Drawable to the overlay. The bounds of the drawable should be relative to + * Adds a {@link 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. + * needed or no longer visible. Adding an already existing {@link Drawable} + * is a no-op. Passing null parameter will result in an + * {@link IllegalArgumentException} being thrown. * - * @param drawable The Drawable to be added to the overlay. This drawable will be - * drawn when the view redraws its overlay. + * @param drawable The {@link Drawable} to be added to the overlay. This drawable will be + * drawn when the view redraws its overlay. {@link Drawable}s will be drawn in the order that + * they were added. * @see #remove(Drawable) */ - public void add(Drawable drawable) { + public void add(@NonNull Drawable drawable) { mOverlayViewGroup.add(drawable); } /** - * Removes the specified Drawable from the overlay. + * Removes the specified {@link Drawable} from the overlay. Removing a {@link Drawable} that was + * not added with {@link #add(Drawable)} is a no-op. Passing null parameter will + * result in an {@link IllegalArgumentException} being thrown. * - * @param drawable The Drawable to be removed from the overlay. + * @param drawable The {@link Drawable} to be removed from the overlay. * @see #add(Drawable) */ - public void remove(Drawable drawable) { + public void remove(@NonNull Drawable drawable) { mOverlayViewGroup.remove(drawable); } @@ -119,7 +125,7 @@ public class ViewOverlay { * The View for which this is an overlay. Invalidations of the overlay are redirected to * this host view. */ - View mHostView; + final View mHostView; /** * The set of drawables to draw when the overlay is rendered. @@ -137,10 +143,12 @@ public class ViewOverlay { mRenderNode.setLeftTopRightBottom(0, 0, mRight, mBottom); } - public void add(Drawable drawable) { + public void add(@NonNull Drawable drawable) { + if (drawable == null) { + throw new IllegalArgumentException("drawable must be non-null"); + } if (mDrawables == null) { - - mDrawables = new ArrayList(); + mDrawables = new ArrayList<>(); } if (!mDrawables.contains(drawable)) { // Make each drawable unique in the overlay; can't add it more than once @@ -150,7 +158,10 @@ public class ViewOverlay { } } - public void remove(Drawable drawable) { + public void remove(@NonNull Drawable drawable) { + if (drawable == null) { + throw new IllegalArgumentException("drawable must be non-null"); + } if (mDrawables != null) { mDrawables.remove(drawable); invalidate(drawable.getBounds()); @@ -163,7 +174,11 @@ public class ViewOverlay { return super.verifyDrawable(who) || (mDrawables != null && mDrawables.contains(who)); } - public void add(View child) { + public void add(@NonNull View child) { + if (child == null) { + throw new IllegalArgumentException("view must be non-null"); + } + if (child.getParent() instanceof ViewGroup) { ViewGroup parent = (ViewGroup) child.getParent(); if (parent != mHostView && parent.getParent() != null && @@ -190,7 +205,11 @@ public class ViewOverlay { super.addView(child); } - public void remove(View view) { + public void remove(@NonNull View view) { + if (view == null) { + throw new IllegalArgumentException("view must be non-null"); + } + super.removeView(view); }