Martijn Coenen | bf34061 | 2011-01-19 00:57:17 +0100 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package android.nfc; |
| 18 | |
| 19 | import android.os.Parcel; |
| 20 | import android.os.Parcelable; |
| 21 | |
Martijn Coenen | faca12a | 2011-08-19 14:07:52 +0200 | [diff] [blame] | 22 | import java.io.IOException; |
| 23 | |
Martijn Coenen | bf34061 | 2011-01-19 00:57:17 +0100 | [diff] [blame] | 24 | /** |
| 25 | * Class used to pipe transceive result from the NFC service. |
| 26 | * |
| 27 | * @hide |
| 28 | */ |
| 29 | public final class TransceiveResult implements Parcelable { |
Martijn Coenen | faca12a | 2011-08-19 14:07:52 +0200 | [diff] [blame] | 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; |
Martijn Coenen | bf34061 | 2011-01-19 00:57:17 +0100 | [diff] [blame] | 34 | |
Martijn Coenen | faca12a | 2011-08-19 14:07:52 +0200 | [diff] [blame] | 35 | final int mResult; |
| 36 | final byte[] mResponseData; |
| 37 | |
| 38 | public TransceiveResult(final int result, final byte[] data) { |
| 39 | mResult = result; |
Martijn Coenen | bf34061 | 2011-01-19 00:57:17 +0100 | [diff] [blame] | 40 | mResponseData = data; |
| 41 | } |
| 42 | |
Martijn Coenen | faca12a | 2011-08-19 14:07:52 +0200 | [diff] [blame] | 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 | } |
Martijn Coenen | bf34061 | 2011-01-19 00:57:17 +0100 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | @Override |
| 57 | public int describeContents() { |
| 58 | return 0; |
| 59 | } |
| 60 | |
| 61 | @Override |
| 62 | public void writeToParcel(Parcel dest, int flags) { |
Martijn Coenen | faca12a | 2011-08-19 14:07:52 +0200 | [diff] [blame] | 63 | dest.writeInt(mResult); |
| 64 | if (mResult == RESULT_SUCCESS) { |
Martijn Coenen | bf34061 | 2011-01-19 00:57:17 +0100 | [diff] [blame] | 65 | dest.writeInt(mResponseData.length); |
| 66 | dest.writeByteArray(mResponseData); |
| 67 | } |
| 68 | } |
| 69 | |
Jeff Sharkey | 9e8f83d | 2019-02-28 12:06:45 -0700 | [diff] [blame^] | 70 | public static final @android.annotation.NonNull Parcelable.Creator<TransceiveResult> CREATOR = |
Martijn Coenen | bf34061 | 2011-01-19 00:57:17 +0100 | [diff] [blame] | 71 | new Parcelable.Creator<TransceiveResult>() { |
| 72 | @Override |
| 73 | public TransceiveResult createFromParcel(Parcel in) { |
Martijn Coenen | faca12a | 2011-08-19 14:07:52 +0200 | [diff] [blame] | 74 | int result = in.readInt(); |
Martijn Coenen | bf34061 | 2011-01-19 00:57:17 +0100 | [diff] [blame] | 75 | byte[] responseData; |
| 76 | |
Martijn Coenen | faca12a | 2011-08-19 14:07:52 +0200 | [diff] [blame] | 77 | if (result == RESULT_SUCCESS) { |
Martijn Coenen | bf34061 | 2011-01-19 00:57:17 +0100 | [diff] [blame] | 78 | int responseLength = in.readInt(); |
| 79 | responseData = new byte[responseLength]; |
| 80 | in.readByteArray(responseData); |
| 81 | } else { |
| 82 | responseData = null; |
| 83 | } |
Martijn Coenen | faca12a | 2011-08-19 14:07:52 +0200 | [diff] [blame] | 84 | return new TransceiveResult(result, responseData); |
Martijn Coenen | bf34061 | 2011-01-19 00:57:17 +0100 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | @Override |
| 88 | public TransceiveResult[] newArray(int size) { |
| 89 | return new TransceiveResult[size]; |
| 90 | } |
| 91 | }; |
| 92 | |
| 93 | } |