| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package android.nfc; |
| 18 |
|
| 19 |
import android.os.Parcel; |
| 20 |
import android.os.Parcelable; |
| 21 |
|
| 22 |
import java.io.IOException; |
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
public final class TransceiveResult implements Parcelable { |
| 30 |
public static final int RESULT_SUCCESS = 0; |
| 31 |
public static final int RESULT_FAILURE = 1; |
| 32 |
public static final int RESULT_TAGLOST = 2; |
| 33 |
public static final int RESULT_EXCEEDED_LENGTH = 3; |
| 34 |
|
| 35 |
final int mResult; |
| 36 |
final byte[] mResponseData; |
| 37 |
|
| 38 |
public TransceiveResult(final int result, final byte[] data) { |
| 39 |
mResult = result; |
| 40 |
mResponseData = data; |
| 41 |
} |
| 42 |
|
| 43 |
public byte[] getResponseOrThrow() throws IOException { |
| 44 |
switch (mResult) { |
| 45 |
case RESULT_SUCCESS: |
| 46 |
return mResponseData; |
| 47 |
case RESULT_TAGLOST: |
| 48 |
throw new TagLostException("Tag was lost."); |
| 49 |
case RESULT_EXCEEDED_LENGTH: |
| 50 |
throw new IOException("Transceive length exceeds supported maximum"); |
| 51 |
default: |
| 52 |
throw new IOException("Transceive failed"); |
| 53 |
} |
| 54 |
} |
| 55 |
|
| 56 |
@Override |
| 57 |
public int describeContents() { |
| 58 |
return 0; |
| 59 |
} |
| 60 |
|
| 61 |
@Override |
| 62 |
public void writeToParcel(Parcel dest, int flags) { |
| 63 |
dest.writeInt(mResult); |
| 64 |
if (mResult == RESULT_SUCCESS) { |
| 65 |
dest.writeInt(mResponseData.length); |
| 66 |
dest.writeByteArray(mResponseData); |
| 67 |
} |
| 68 |
} |
| 69 |
|
| 70 |
public static final @android.annotation.NonNull Parcelable.Creator<TransceiveResult> CREATOR = |
| 71 |
new Parcelable.Creator<TransceiveResult>() { |
| 72 |
@Override |
| 73 |
public TransceiveResult createFromParcel(Parcel in) { |
| 74 |
int result = in.readInt(); |
| 75 |
byte[] responseData; |
| 76 |
|
| 77 |
if (result == RESULT_SUCCESS) { |
| 78 |
int responseLength = in.readInt(); |
| 79 |
responseData = new byte[responseLength]; |
| 80 |
in.readByteArray(responseData); |
| 81 |
} else { |
| 82 |
responseData = null; |
| 83 |
} |
| 84 |
return new TransceiveResult(result, responseData); |
| 85 |
} |
| 86 |
|
| 87 |
@Override |
| 88 |
public TransceiveResult[] newArray(int size) { |
| 89 |
return new TransceiveResult[size]; |
| 90 |
} |
| 91 |
}; |
| 92 |
|
| 93 |
} |