Home | History | Annotate | Download | only in nfc
      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 
     22 import java.io.IOException;
     23 
     24 /**
     25  * Class used to pipe transceive result from the NFC service.
     26  *
     27  * @hide
     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 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 }
     94