blob: 7992094e6ba1a4072720f5f7da868cecff5fa5ec [file] [log] [blame]
Martijn Coenenbf340612011-01-19 00:57:17 +01001/*
2 * Copyright (C) 2011, The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.nfc;
18
19import android.os.Parcel;
20import android.os.Parcelable;
21
Martijn Coenenfaca12a2011-08-19 14:07:52 +020022import java.io.IOException;
23
Martijn Coenenbf340612011-01-19 00:57:17 +010024/**
25 * Class used to pipe transceive result from the NFC service.
26 *
27 * @hide
28 */
29public final class TransceiveResult implements Parcelable {
Martijn Coenenfaca12a2011-08-19 14:07:52 +020030 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;
Martijn Coenenbf340612011-01-19 00:57:17 +010034
Martijn Coenenfaca12a2011-08-19 14:07:52 +020035 final int mResult;
36 final byte[] mResponseData;
37
38 public TransceiveResult(final int result, final byte[] data) {
39 mResult = result;
Martijn Coenenbf340612011-01-19 00:57:17 +010040 mResponseData = data;
41 }
42
Martijn Coenenfaca12a2011-08-19 14:07:52 +020043 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 }
Martijn Coenenbf340612011-01-19 00:57:17 +010054 }
55
56 @Override
57 public int describeContents() {
58 return 0;
59 }
60
61 @Override
62 public void writeToParcel(Parcel dest, int flags) {
Martijn Coenenfaca12a2011-08-19 14:07:52 +020063 dest.writeInt(mResult);
64 if (mResult == RESULT_SUCCESS) {
Martijn Coenenbf340612011-01-19 00:57:17 +010065 dest.writeInt(mResponseData.length);
66 dest.writeByteArray(mResponseData);
67 }
68 }
69
Jeff Sharkey9e8f83d2019-02-28 12:06:45 -070070 public static final @android.annotation.NonNull Parcelable.Creator<TransceiveResult> CREATOR =
Martijn Coenenbf340612011-01-19 00:57:17 +010071 new Parcelable.Creator<TransceiveResult>() {
72 @Override
73 public TransceiveResult createFromParcel(Parcel in) {
Martijn Coenenfaca12a2011-08-19 14:07:52 +020074 int result = in.readInt();
Martijn Coenenbf340612011-01-19 00:57:17 +010075 byte[] responseData;
76
Martijn Coenenfaca12a2011-08-19 14:07:52 +020077 if (result == RESULT_SUCCESS) {
Martijn Coenenbf340612011-01-19 00:57:17 +010078 int responseLength = in.readInt();
79 responseData = new byte[responseLength];
80 in.readByteArray(responseData);
81 } else {
82 responseData = null;
83 }
Martijn Coenenfaca12a2011-08-19 14:07:52 +020084 return new TransceiveResult(result, responseData);
Martijn Coenenbf340612011-01-19 00:57:17 +010085 }
86
87 @Override
88 public TransceiveResult[] newArray(int size) {
89 return new TransceiveResult[size];
90 }
91 };
92
93}