From ad5658bb161520de156e4670d3e436b6e10789ba Mon Sep 17 00:00:00 2001 From: Alan Viverette Date: Thu, 3 Feb 2022 15:40:58 -0500 Subject: Nullability annotations for animation and graphics I inferred nullability annotations in Jetpack based on the guaranteed behavior of the platform, so now I'm propagating those inferences back to the platform as annotations. Test: nullability presubmit checks Fixes: 206801689 Change-Id: If969bbe8a2263a3dd6cc1497a01354648a8795e9 --- graphics/java/android/graphics/ImageDecoder.java | 6 +-- .../java/android/graphics/drawable/Drawable.java | 52 ++++++++++------------ .../graphics/drawable/DrawableInflater.java | 20 +++------ .../android/graphics/drawable/DrawableWrapper.java | 2 +- graphics/java/android/graphics/drawable/Icon.java | 39 ++++++++-------- 5 files changed, 53 insertions(+), 66 deletions(-) (limited to 'graphics/java') diff --git a/graphics/java/android/graphics/ImageDecoder.java b/graphics/java/android/graphics/ImageDecoder.java index 373228586161..1629b6ace35d 100644 --- a/graphics/java/android/graphics/ImageDecoder.java +++ b/graphics/java/android/graphics/ImageDecoder.java @@ -368,7 +368,7 @@ public final class ImageDecoder implements AutoCloseable { * Further, unlike other Sources, this one is not reusable. */ private static class InputStreamSource extends Source { - InputStreamSource(Resources res, InputStream is, int inputDensity) { + InputStreamSource(Resources res, @NonNull InputStream is, int inputDensity) { if (is == null) { throw new IllegalArgumentException("The InputStream cannot be null"); } @@ -1020,7 +1020,7 @@ public final class ImageDecoder implements AutoCloseable { */ @AnyThread @NonNull - public static Source createSource(Resources res, InputStream is) { + public static Source createSource(Resources res, @NonNull InputStream is) { return new InputStreamSource(res, is, Bitmap.getDefaultDensity()); } @@ -1034,7 +1034,7 @@ public final class ImageDecoder implements AutoCloseable { @AnyThread @TestApi @NonNull - public static Source createSource(Resources res, InputStream is, int density) { + public static Source createSource(Resources res, @NonNull InputStream is, int density) { return new InputStreamSource(res, is, density); } diff --git a/graphics/java/android/graphics/drawable/Drawable.java b/graphics/java/android/graphics/drawable/Drawable.java index 28b3b04b827d..4972e928dd22 100644 --- a/graphics/java/android/graphics/drawable/Drawable.java +++ b/graphics/java/android/graphics/drawable/Drawable.java @@ -288,8 +288,7 @@ public abstract class Drawable { * * @return A copy of the drawable's bounds */ - @NonNull - public final Rect copyBounds() { + public final @NonNull Rect copyBounds() { return new Rect(mBounds); } @@ -308,8 +307,7 @@ public abstract class Drawable { * @see #copyBounds() * @see #copyBounds(android.graphics.Rect) */ - @NonNull - public final Rect getBounds() { + public final @NonNull Rect getBounds() { if (mBounds == ZERO_BOUNDS_RECT) { mBounds = new Rect(); } @@ -327,8 +325,7 @@ public abstract class Drawable { * * @return The dirty bounds of this drawable */ - @NonNull - public Rect getDirtyBounds() { + public @NonNull Rect getDirtyBounds() { return getBounds(); } @@ -457,8 +454,7 @@ public abstract class Drawable { * * @see #setCallback(android.graphics.drawable.Drawable.Callback) */ - @Nullable - public Callback getCallback() { + public @Nullable Callback getCallback() { return mCallback != null ? mCallback.get() : null; } @@ -569,8 +565,7 @@ public abstract class Drawable { * The default return value is 255 if the class does not override this method to return a value * specific to its use of alpha. */ - @IntRange(from=0,to=255) - public int getAlpha() { + public @IntRange(from=0,to=255) int getAlpha() { return 0xFF; } @@ -999,7 +994,8 @@ public abstract class Drawable { * * @see android.graphics.PixelFormat */ - @Deprecated public abstract @PixelFormat.Opacity int getOpacity(); + @Deprecated + public abstract @PixelFormat.Opacity int getOpacity(); /** * Return the appropriate opacity value for two source opacities. If @@ -1059,7 +1055,7 @@ public abstract class Drawable { * if it looks the same and there is no need to redraw it since its * last state. */ - protected boolean onStateChange(int[] state) { + protected boolean onStateChange(@NonNull int[] state) { return false; } @@ -1078,7 +1074,7 @@ public abstract class Drawable { * Override this in your subclass to change appearance if you vary based on * the bounds. */ - protected void onBoundsChange(Rect bounds) { + protected void onBoundsChange(@NonNull Rect bounds) { // Stub method. } @@ -1209,7 +1205,8 @@ public abstract class Drawable { /** * Create a drawable from an inputstream */ - public static Drawable createFromStream(InputStream is, String srcName) { + public static @Nullable Drawable createFromStream(@Nullable InputStream is, + @Nullable String srcName) { Trace.traceBegin(Trace.TRACE_TAG_RESOURCES, srcName != null ? srcName : "Unknown drawable"); try { return createFromResourceStream(null, null, is, srcName); @@ -1222,8 +1219,8 @@ public abstract class Drawable { * Create a drawable from an inputstream, using the given resources and * value to determine density information. */ - public static Drawable createFromResourceStream(Resources res, TypedValue value, - InputStream is, String srcName) { + public static @Nullable Drawable createFromResourceStream(@Nullable Resources res, + @Nullable TypedValue value, @Nullable InputStream is, @Nullable String srcName) { Trace.traceBegin(Trace.TRACE_TAG_RESOURCES, srcName != null ? srcName : "Unknown drawable"); try { return createFromResourceStream(res, value, is, srcName, null); @@ -1238,8 +1235,7 @@ public abstract class Drawable { * * @deprecated Prefer the version without an Options object. */ - @Nullable - public static Drawable createFromResourceStream(@Nullable Resources res, + public static @Nullable Drawable createFromResourceStream(@Nullable Resources res, @Nullable TypedValue value, @Nullable InputStream is, @Nullable String srcName, @Nullable BitmapFactory.Options opts) { if (is == null) { @@ -1281,7 +1277,8 @@ public abstract class Drawable { return null; } - private static Drawable getBitmapDrawable(Resources res, TypedValue value, InputStream is) { + private static Drawable getBitmapDrawable(Resources res, @Nullable TypedValue value, + @NonNull InputStream is) { try { ImageDecoder.Source source = null; if (value != null) { @@ -1369,9 +1366,9 @@ public abstract class Drawable { * a tag in an XML document, tries to create a Drawable from that tag. * Returns null if the tag is not a valid drawable. */ - @NonNull - public static Drawable createFromXmlInner(@NonNull Resources r, @NonNull XmlPullParser parser, - @NonNull AttributeSet attrs) throws XmlPullParserException, IOException { + public static @NonNull Drawable createFromXmlInner(@NonNull Resources r, + @NonNull XmlPullParser parser, @NonNull AttributeSet attrs) + throws XmlPullParserException, IOException { return createFromXmlInner(r, parser, attrs, null); } @@ -1381,9 +1378,8 @@ public abstract class Drawable { * document, tries to create a Drawable from that tag. Returns {@code null} * if the tag is not a valid drawable. */ - @NonNull - public static Drawable createFromXmlInner(@NonNull Resources r, @NonNull XmlPullParser parser, - @NonNull AttributeSet attrs, @Nullable Theme theme) + public static @NonNull Drawable createFromXmlInner(@NonNull Resources r, + @NonNull XmlPullParser parser, @NonNull AttributeSet attrs, @Nullable Theme theme) throws XmlPullParserException, IOException { return createFromXmlInnerForDensity(r, parser, attrs, 0, theme); } @@ -1392,8 +1388,7 @@ public abstract class Drawable { * Version of {@link #createFromXmlInner(Resources, XmlPullParser, AttributeSet, Theme)} that * accepts an override density. */ - @NonNull - static Drawable createFromXmlInnerForDensity(@NonNull Resources r, + static @NonNull Drawable createFromXmlInnerForDensity(@NonNull Resources r, @NonNull XmlPullParser parser, @NonNull AttributeSet attrs, int density, @Nullable Theme theme) throws XmlPullParserException, IOException { return r.getDrawableInflater().inflateFromXmlForDensity(parser.getName(), parser, attrs, @@ -1403,8 +1398,7 @@ public abstract class Drawable { /** * Create a drawable from file path name. */ - @Nullable - public static Drawable createFromPath(String pathName) { + public static @Nullable Drawable createFromPath(String pathName) { if (pathName == null) { return null; } diff --git a/graphics/java/android/graphics/drawable/DrawableInflater.java b/graphics/java/android/graphics/drawable/DrawableInflater.java index 66752a2536d3..8debe26b8c6a 100644 --- a/graphics/java/android/graphics/drawable/DrawableInflater.java +++ b/graphics/java/android/graphics/drawable/DrawableInflater.java @@ -61,8 +61,7 @@ public final class DrawableInflater { * @param id the identifier of the drawable resource * @return a drawable, or {@code null} if the drawable failed to load */ - @Nullable - public static Drawable loadDrawable(@NonNull Context context, @DrawableRes int id) { + public static @Nullable Drawable loadDrawable(@NonNull Context context, @DrawableRes int id) { return loadDrawable(context.getResources(), context.getTheme(), id); } @@ -74,9 +73,8 @@ public final class DrawableInflater { * @param id the identifier of the drawable resource * @return a drawable, or {@code null} if the drawable failed to load */ - @Nullable - public static Drawable loadDrawable( - @NonNull Resources resources, @Nullable Theme theme, @DrawableRes int id) { + public static @Nullable Drawable loadDrawable(@NonNull Resources resources, + @Nullable Theme theme, @DrawableRes int id) { return resources.getDrawable(id, theme); } @@ -111,8 +109,7 @@ public final class DrawableInflater { * @throws XmlPullParserException * @throws IOException */ - @NonNull - public Drawable inflateFromXml(@NonNull String name, @NonNull XmlPullParser parser, + public @NonNull Drawable inflateFromXml(@NonNull String name, @NonNull XmlPullParser parser, @NonNull AttributeSet attrs, @Nullable Theme theme) throws XmlPullParserException, IOException { return inflateFromXmlForDensity(name, parser, attrs, 0, theme); @@ -122,8 +119,7 @@ public final class DrawableInflater { * Version of {@link #inflateFromXml(String, XmlPullParser, AttributeSet, Theme)} that accepts * an override density. */ - @NonNull - Drawable inflateFromXmlForDensity(@NonNull String name, @NonNull XmlPullParser parser, + @NonNull Drawable inflateFromXmlForDensity(@NonNull String name, @NonNull XmlPullParser parser, @NonNull AttributeSet attrs, int density, @Nullable Theme theme) throws XmlPullParserException, IOException { // Inner classes must be referenced as Outer$Inner, but XML tag names @@ -146,9 +142,8 @@ public final class DrawableInflater { return drawable; } - @NonNull @SuppressWarnings("deprecation") - private Drawable inflateFromTag(@NonNull String name) { + private @Nullable Drawable inflateFromTag(@NonNull String name) { switch (name) { case "selector": return new StateListDrawable(); @@ -195,8 +190,7 @@ public final class DrawableInflater { } } - @NonNull - private Drawable inflateFromClass(@NonNull String className) { + private @NonNull Drawable inflateFromClass(@NonNull String className) { try { Constructor constructor; synchronized (CONSTRUCTOR_MAP) { diff --git a/graphics/java/android/graphics/drawable/DrawableWrapper.java b/graphics/java/android/graphics/drawable/DrawableWrapper.java index ebde75775e84..a63d7f660801 100644 --- a/graphics/java/android/graphics/drawable/DrawableWrapper.java +++ b/graphics/java/android/graphics/drawable/DrawableWrapper.java @@ -352,7 +352,7 @@ public abstract class DrawableWrapper extends Drawable implements Drawable.Callb } @Override - protected boolean onStateChange(int[] state) { + protected boolean onStateChange(@NonNull int[] state) { if (mDrawable != null && mDrawable.isStateful()) { final boolean changed = mDrawable.setState(state); if (changed) { diff --git a/graphics/java/android/graphics/drawable/Icon.java b/graphics/java/android/graphics/drawable/Icon.java index a03931bfab24..b04b82629b92 100644 --- a/graphics/java/android/graphics/drawable/Icon.java +++ b/graphics/java/android/graphics/drawable/Icon.java @@ -302,7 +302,7 @@ public final class Icon implements Parcelable { * is available. The {@link android.os.Message#obj obj} * property is populated with the Drawable. */ - public void loadDrawableAsync(Context context, Message andThen) { + public void loadDrawableAsync(@NonNull Context context, @NonNull Message andThen) { if (andThen.getTarget() == null) { throw new IllegalArgumentException("callback message must have a target handler"); } @@ -320,7 +320,7 @@ public final class Icon implements Parcelable { * {@link #loadDrawable(Context)} finished * @param handler {@link Handler} on which to notify the {@code listener} */ - public void loadDrawableAsync(Context context, final OnDrawableLoadedListener listener, + public void loadDrawableAsync(@NonNull Context context, final OnDrawableLoadedListener listener, Handler handler) { new LoadDrawableTask(context, handler, listener).runAsync(); } @@ -335,7 +335,7 @@ public final class Icon implements Parcelable { * to access {@link android.content.res.Resources Resources}, for example. * @return A fresh instance of a drawable for this image, yours to keep. */ - public Drawable loadDrawable(Context context) { + public @Nullable Drawable loadDrawable(Context context) { final Drawable result = loadDrawableInner(context); if (result != null && hasTint()) { result.mutate(); @@ -415,7 +415,7 @@ public final class Icon implements Parcelable { return null; } - private InputStream getUriInputStream(Context context) { + private @Nullable InputStream getUriInputStream(Context context) { final Uri uri = getUri(); final String scheme = uri.getScheme(); if (ContentResolver.SCHEME_CONTENT.equals(scheme) @@ -496,7 +496,7 @@ public final class Icon implements Parcelable { * @param stream The stream on which to serialize the Icon. * @hide */ - public void writeToStream(OutputStream stream) throws IOException { + public void writeToStream(@NonNull OutputStream stream) throws IOException { DataOutputStream dataStream = new DataOutputStream(stream); dataStream.writeInt(VERSION_STREAM_SERIALIZER); @@ -532,7 +532,7 @@ public final class Icon implements Parcelable { * @param stream The input stream from which to reconstruct the Icon. * @hide */ - public static Icon createFromStream(InputStream stream) throws IOException { + public static @Nullable Icon createFromStream(@NonNull InputStream stream) throws IOException { DataInputStream inputStream = new DataInputStream(stream); final int version = inputStream.readInt(); @@ -571,7 +571,7 @@ public final class Icon implements Parcelable { * @return whether this icon is the same as the another one * @hide */ - public boolean sameAs(Icon otherIcon) { + public boolean sameAs(@NonNull Icon otherIcon) { if (otherIcon == this) { return true; } @@ -602,7 +602,7 @@ public final class Icon implements Parcelable { * given resource ID. * @param resId ID of the drawable resource */ - public static Icon createWithResource(Context context, @DrawableRes int resId) { + public static @NonNull Icon createWithResource(Context context, @DrawableRes int resId) { if (context == null) { throw new IllegalArgumentException("Context must not be null."); } @@ -617,7 +617,7 @@ public final class Icon implements Parcelable { * @hide */ @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553) - public static Icon createWithResource(Resources res, @DrawableRes int resId) { + public static @NonNull Icon createWithResource(Resources res, @DrawableRes int resId) { if (res == null) { throw new IllegalArgumentException("Resource must not be null."); } @@ -632,7 +632,7 @@ public final class Icon implements Parcelable { * @param resPackage Name of the package containing the resource in question * @param resId ID of the drawable resource */ - public static Icon createWithResource(String resPackage, @DrawableRes int resId) { + public static @NonNull Icon createWithResource(String resPackage, @DrawableRes int resId) { if (resPackage == null) { throw new IllegalArgumentException("Resource package name must not be null."); } @@ -646,7 +646,7 @@ public final class Icon implements Parcelable { * Create an Icon pointing to a bitmap in memory. * @param bits A valid {@link android.graphics.Bitmap} object */ - public static Icon createWithBitmap(Bitmap bits) { + public static @NonNull Icon createWithBitmap(Bitmap bits) { if (bits == null) { throw new IllegalArgumentException("Bitmap must not be null."); } @@ -660,7 +660,7 @@ public final class Icon implements Parcelable { * by {@link AdaptiveIconDrawable}. * @param bits A valid {@link android.graphics.Bitmap} object */ - public static Icon createWithAdaptiveBitmap(Bitmap bits) { + public static @NonNull Icon createWithAdaptiveBitmap(Bitmap bits) { if (bits == null) { throw new IllegalArgumentException("Bitmap must not be null."); } @@ -677,7 +677,7 @@ public final class Icon implements Parcelable { * @param offset Offset into data at which the bitmap data starts * @param length Length of the bitmap data */ - public static Icon createWithData(byte[] data, int offset, int length) { + public static @NonNull Icon createWithData(byte[] data, int offset, int length) { if (data == null) { throw new IllegalArgumentException("Data must not be null."); } @@ -693,7 +693,7 @@ public final class Icon implements Parcelable { * * @param uri A uri referring to local content:// or file:// image data. */ - public static Icon createWithContentUri(String uri) { + public static @NonNull Icon createWithContentUri(String uri) { if (uri == null) { throw new IllegalArgumentException("Uri must not be null."); } @@ -707,7 +707,7 @@ public final class Icon implements Parcelable { * * @param uri A uri referring to local content:// or file:// image data. */ - public static Icon createWithContentUri(Uri uri) { + public static @NonNull Icon createWithContentUri(Uri uri) { if (uri == null) { throw new IllegalArgumentException("Uri must not be null."); } @@ -720,8 +720,7 @@ public final class Icon implements Parcelable { * * @param uri A uri referring to local content:// or file:// image data. */ - @NonNull - public static Icon createWithAdaptiveBitmapContentUri(@NonNull String uri) { + public static @NonNull Icon createWithAdaptiveBitmapContentUri(@NonNull String uri) { if (uri == null) { throw new IllegalArgumentException("Uri must not be null."); } @@ -750,7 +749,7 @@ public final class Icon implements Parcelable { * @param tint a color, as in {@link Drawable#setTint(int)} * @return this same object, for use in chained construction */ - public Icon setTint(@ColorInt int tint) { + public @NonNull Icon setTint(@ColorInt int tint) { return setTintList(ColorStateList.valueOf(tint)); } @@ -760,7 +759,7 @@ public final class Icon implements Parcelable { * @param tintList as in {@link Drawable#setTintList(ColorStateList)}, null to remove tint * @return this same object, for use in chained construction */ - public Icon setTintList(ColorStateList tintList) { + public @NonNull Icon setTintList(ColorStateList tintList) { mTintList = tintList; return this; } @@ -809,7 +808,7 @@ public final class Icon implements Parcelable { * @param path A path to a file that contains compressed bitmap data of * a type that {@link android.graphics.BitmapFactory} can decode. */ - public static Icon createWithFilePath(String path) { + public static @NonNull Icon createWithFilePath(String path) { if (path == null) { throw new IllegalArgumentException("Path must not be null."); } -- cgit v1.2.3-59-g8ed1b