From 0639fe0aa9d87bae7745e60238d48c79f986b590 Mon Sep 17 00:00:00 2001 From: Daichi Hirono Date: Thu, 7 Jul 2016 17:36:26 +0900 Subject: [PATCH] Add @NonNull / @Nullable annotations to android.mtp API. BUG=26758882 Change-Id: Ic86d2253114b487d68a069f3e63f19b200d6cb97 --- media/java/android/mtp/MtpDeviceInfo.java | 16 +++++++++------- media/java/android/mtp/MtpObjectInfo.java | 25 +++++++++++++++++++------ media/java/android/mtp/MtpStorageInfo.java | 6 ++++-- 3 files changed, 32 insertions(+), 15 deletions(-) diff --git a/media/java/android/mtp/MtpDeviceInfo.java b/media/java/android/mtp/MtpDeviceInfo.java index 86bd599e442c..0304ee386ace 100644 --- a/media/java/android/mtp/MtpDeviceInfo.java +++ b/media/java/android/mtp/MtpDeviceInfo.java @@ -16,6 +16,8 @@ package android.mtp; +import android.annotation.NonNull; + /** * This class encapsulates information about an MTP device. * This corresponds to the DeviceInfo Dataset described in @@ -39,7 +41,7 @@ public class MtpDeviceInfo { * * @return the manufacturer name */ - public final String getManufacturer() { + public final @NonNull String getManufacturer() { return mManufacturer; } @@ -48,7 +50,7 @@ public class MtpDeviceInfo { * * @return the model name */ - public final String getModel() { + public final @NonNull String getModel() { return mModel; } @@ -57,7 +59,7 @@ public class MtpDeviceInfo { * * @return the device version */ - public final String getVersion() { + public final @NonNull String getVersion() { return mVersion; } @@ -66,7 +68,7 @@ public class MtpDeviceInfo { * * @return the serial number */ - public final String getSerialNumber() { + public final @NonNull String getSerialNumber() { return mSerialNumber; } @@ -110,7 +112,7 @@ public class MtpDeviceInfo { * @see MtpConstants#OPERATION_SET_OBJECT_REFERENCES * @see MtpConstants#OPERATION_SKIP */ - public final int[] getOperationsSupported() { + public final @NonNull int[] getOperationsSupported() { return mOperationsSupported; } @@ -137,7 +139,7 @@ public class MtpDeviceInfo { * @see MtpEvent#EVENT_OBJECT_PROP_DESC_CHANGED * @see MtpEvent#EVENT_OBJECT_REFERENCES_CHANGED */ - public final int[] getEventsSupported() { + public final @NonNull int[] getEventsSupported() { return mEventsSupported; } @@ -163,7 +165,7 @@ public class MtpDeviceInfo { * Returns if the code set contains code. * @hide */ - private static boolean isSupported(int[] set, int code) { + private static boolean isSupported(@NonNull int[] set, int code) { for (final int element : set) { if (element == code) { return true; diff --git a/media/java/android/mtp/MtpObjectInfo.java b/media/java/android/mtp/MtpObjectInfo.java index 02092b177fe0..35d8dfbad84a 100644 --- a/media/java/android/mtp/MtpObjectInfo.java +++ b/media/java/android/mtp/MtpObjectInfo.java @@ -16,8 +16,13 @@ package android.mtp; +import android.annotation.NonNull; +import android.os.Build; + import com.android.internal.util.Preconditions; +import dalvik.system.VMRuntime; + /** * This class encapsulates information about an object on an MTP device. * This corresponds to the ObjectInfo Dataset described in @@ -40,10 +45,10 @@ public final class MtpObjectInfo { private int mAssociationType; private int mAssociationDesc; private int mSequenceNumber; - private String mName; + private String mName = ""; private long mDateCreated; private long mDateModified; - private String mKeywords; + private String mKeywords = ""; // only instantiated via JNI or via a builder private MtpObjectInfo() { @@ -311,7 +316,7 @@ public final class MtpObjectInfo { * * @return the object's name */ - public final String getName() { + public final @NonNull String getName() { return mName; } @@ -340,7 +345,7 @@ public final class MtpObjectInfo { * * @return the object's keyword list */ - public final String getKeywords() { + public final @NonNull String getKeywords() { return mKeywords; } @@ -435,12 +440,20 @@ public final class MtpObjectInfo { return this; } - public Builder setKeywords(String value) { + public Builder setKeywords(@NonNull String value) { + if (VMRuntime.getRuntime().getTargetSdkVersion() > Build.VERSION_CODES.N_MR1) { + Preconditions.checkNotNull(value); + } else if (value == null) { + // Before N_MR1 we accept null value and it was regarded as an empty string in + // MtpDevice#sendObjectInfo. + value = ""; + } mObjectInfo.mKeywords = value; return this; } - public Builder setName(String value) { + public Builder setName(@NonNull String value) { + Preconditions.checkNotNull(value); mObjectInfo.mName = value; return this; } diff --git a/media/java/android/mtp/MtpStorageInfo.java b/media/java/android/mtp/MtpStorageInfo.java index d1b86fcfc2e6..af9f24a13db5 100644 --- a/media/java/android/mtp/MtpStorageInfo.java +++ b/media/java/android/mtp/MtpStorageInfo.java @@ -16,6 +16,8 @@ package android.mtp; +import android.annotation.NonNull; + /** * This class encapsulates information about a storage unit on an MTP device. * This corresponds to the StorageInfo Dataset described in @@ -68,7 +70,7 @@ public final class MtpStorageInfo { * * @return the storage unit description */ - public final String getDescription() { + public final @NonNull String getDescription() { return mDescription; } @@ -77,7 +79,7 @@ public final class MtpStorageInfo { * * @return the storage volume identifier */ - public final String getVolumeIdentifier() { + public final @NonNull String getVolumeIdentifier() { return mVolumeIdentifier; } } -- 2.11.0