diff options
author | Martijn Coenen <maco@google.com> | 2011-08-19 14:07:52 +0200 |
---|---|---|
committer | Martijn Coenen <maco@google.com> | 2011-08-26 16:30:58 -0700 |
commit | faca12adc62d148505fadfd286e6a2752c197fa0 (patch) | |
tree | fe8feadadc2428e92eea87b8cb7cb0049a53a1f5 /core/java/android/nfc | |
parent | cfc0f2c206af24350245f83b36e0032a7d4de49a (diff) | |
download | frameworks_base-faca12adc62d148505fadfd286e6a2752c197fa0.zip frameworks_base-faca12adc62d148505fadfd286e6a2752c197fa0.tar.gz frameworks_base-faca12adc62d148505fadfd286e6a2752c197fa0.tar.bz2 |
Add getMaxTransceiveLength() API.
Also moved canMakeReadOnly() down in the stack, and
cleaned up TransceiveResult.
Change-Id: I85576c52478ab79f0726606659b0c17d00b222e6
Diffstat (limited to 'core/java/android/nfc')
-rw-r--r-- | core/java/android/nfc/INfcTag.aidl | 2 | ||||
-rw-r--r-- | core/java/android/nfc/TransceiveResult.java | 51 | ||||
-rw-r--r-- | core/java/android/nfc/tech/BasicTagTechnology.java | 20 | ||||
-rw-r--r-- | core/java/android/nfc/tech/IsoDep.java | 11 | ||||
-rw-r--r-- | core/java/android/nfc/tech/MifareClassic.java | 11 | ||||
-rw-r--r-- | core/java/android/nfc/tech/MifareUltralight.java | 11 | ||||
-rw-r--r-- | core/java/android/nfc/tech/Ndef.java | 8 | ||||
-rw-r--r-- | core/java/android/nfc/tech/NfcA.java | 11 | ||||
-rw-r--r-- | core/java/android/nfc/tech/NfcB.java | 11 | ||||
-rw-r--r-- | core/java/android/nfc/tech/NfcF.java | 11 | ||||
-rw-r--r-- | core/java/android/nfc/tech/NfcV.java | 12 |
11 files changed, 122 insertions, 37 deletions
diff --git a/core/java/android/nfc/INfcTag.aidl b/core/java/android/nfc/INfcTag.aidl index 7bdefe7..bb5a9fd 100644 --- a/core/java/android/nfc/INfcTag.aidl +++ b/core/java/android/nfc/INfcTag.aidl @@ -46,4 +46,6 @@ interface INfcTag int setTimeout(int technology, int timeout); int getTimeout(int technology); void resetTimeouts(); + boolean canMakeReadOnly(int ndefType); + int getMaxTransceiveLength(int technology); } diff --git a/core/java/android/nfc/TransceiveResult.java b/core/java/android/nfc/TransceiveResult.java index 16244b8..3538825 100644 --- a/core/java/android/nfc/TransceiveResult.java +++ b/core/java/android/nfc/TransceiveResult.java @@ -19,33 +19,38 @@ package android.nfc; import android.os.Parcel; import android.os.Parcelable; +import java.io.IOException; + /** * Class used to pipe transceive result from the NFC service. * * @hide */ public final class TransceiveResult implements Parcelable { - private final boolean mTagLost; - private final boolean mSuccess; - private final byte[] mResponseData; + public static final int RESULT_SUCCESS = 0; + public static final int RESULT_FAILURE = 1; + public static final int RESULT_TAGLOST = 2; + public static final int RESULT_EXCEEDED_LENGTH = 3; - public TransceiveResult(final boolean success, final boolean tagIsLost, - final byte[] data) { - mSuccess = success; - mTagLost = tagIsLost; - mResponseData = data; - } + final int mResult; + final byte[] mResponseData; - public boolean isSuccessful() { - return mSuccess; - } - - public boolean isTagLost() { - return mTagLost; + public TransceiveResult(final int result, final byte[] data) { + mResult = result; + mResponseData = data; } - public byte[] getResponseData() { - return mResponseData; + public byte[] getResponseOrThrow() throws IOException { + switch (mResult) { + case RESULT_SUCCESS: + return mResponseData; + case RESULT_TAGLOST: + throw new TagLostException("Tag was lost."); + case RESULT_EXCEEDED_LENGTH: + throw new IOException("Transceive length exceeds supported maximum"); + default: + throw new IOException("Transceive failed"); + } } @Override @@ -55,9 +60,8 @@ public final class TransceiveResult implements Parcelable { @Override public void writeToParcel(Parcel dest, int flags) { - dest.writeInt(mSuccess ? 1 : 0); - dest.writeInt(mTagLost ? 1 : 0); - if (mSuccess) { + dest.writeInt(mResult); + if (mResult == RESULT_SUCCESS) { dest.writeInt(mResponseData.length); dest.writeByteArray(mResponseData); } @@ -67,18 +71,17 @@ public final class TransceiveResult implements Parcelable { new Parcelable.Creator<TransceiveResult>() { @Override public TransceiveResult createFromParcel(Parcel in) { - boolean success = (in.readInt() == 1) ? true : false; - boolean tagLost = (in.readInt() == 1) ? true : false; + int result = in.readInt(); byte[] responseData; - if (success) { + if (result == RESULT_SUCCESS) { int responseLength = in.readInt(); responseData = new byte[responseLength]; in.readByteArray(responseData); } else { responseData = null; } - return new TransceiveResult(success, tagLost, responseData); + return new TransceiveResult(result, responseData); } @Override diff --git a/core/java/android/nfc/tech/BasicTagTechnology.java b/core/java/android/nfc/tech/BasicTagTechnology.java index bcb7199..913ae0e 100644 --- a/core/java/android/nfc/tech/BasicTagTechnology.java +++ b/core/java/android/nfc/tech/BasicTagTechnology.java @@ -129,6 +129,15 @@ import java.io.IOException; } } + /** Internal getMaxTransceiveLength() */ + int getMaxTransceiveLengthInternal() { + try { + return mTag.getTagService().getMaxTransceiveLength(mSelectedTechnology); + } catch (RemoteException e) { + Log.e(TAG, "NFC service dead", e); + return 0; + } + } /** Internal transceive */ /*package*/ byte[] transceive(byte[] data, boolean raw) throws IOException { checkConnected(); @@ -139,16 +148,7 @@ import java.io.IOException; if (result == null) { throw new IOException("transceive failed"); } else { - if (result.isSuccessful()) { - return result.getResponseData(); - } else { - if (result.isTagLost()) { - throw new TagLostException("Tag was lost."); - } - else { - throw new IOException("transceive failed"); - } - } + return result.getResponseOrThrow(); } } catch (RemoteException e) { Log.e(TAG, "NFC service dead", e); diff --git a/core/java/android/nfc/tech/IsoDep.java b/core/java/android/nfc/tech/IsoDep.java index 0672a4e..6054fe8 100644 --- a/core/java/android/nfc/tech/IsoDep.java +++ b/core/java/android/nfc/tech/IsoDep.java @@ -156,6 +156,9 @@ public final class IsoDep extends BasicTagTechnology { * will be automatically fragmented and defragmented by {@link #transceive} if * it exceeds FSD/FSC limits. * + * <p>Use {@link #getMaxTransceiveLength} to retrieve the maximum number of bytes + * that can be sent with {@link #transceive}. + * * <p>This is an I/O operation and will block until complete. It must * not be called from the main application thread. A blocked call will be canceled with * {@link IOException} if {@link #close} is called from another thread. @@ -170,4 +173,12 @@ public final class IsoDep extends BasicTagTechnology { public byte[] transceive(byte[] data) throws IOException { return transceive(data, true); } + + /** + * Return the maximum number of bytes that can be sent with {@link #transceive}. + * @return the maximum number of bytes that can be sent with {@link #transceive}. + */ + public int getMaxTransceiveLength() { + return getMaxTransceiveLengthInternal(); + } } diff --git a/core/java/android/nfc/tech/MifareClassic.java b/core/java/android/nfc/tech/MifareClassic.java index 93e7cbd..ce923ae 100644 --- a/core/java/android/nfc/tech/MifareClassic.java +++ b/core/java/android/nfc/tech/MifareClassic.java @@ -560,6 +560,9 @@ public final class MifareClassic extends BasicTagTechnology { * and calling {@link NfcA#transceive}. Note that all MIFARE Classic * tags are based on {@link NfcA} technology. * + * <p>Use {@link #getMaxTransceiveLength} to retrieve the maximum number of bytes + * that can be sent with {@link #transceive}. + * * <p>This is an I/O operation and will block until complete. It must * not be called from the main application thread. A blocked call will be canceled with * {@link IOException} if {@link #close} is called from another thread. @@ -573,6 +576,14 @@ public final class MifareClassic extends BasicTagTechnology { } /** + * Return the maximum number of bytes that can be sent with {@link #transceive}. + * @return the maximum number of bytes that can be sent with {@link #transceive}. + */ + public int getMaxTransceiveLength() { + return getMaxTransceiveLengthInternal(); + } + + /** * Set the timeout of {@link #transceive} in milliseconds. * <p>The timeout only applies to MifareUltralight {@link #transceive}, * and is reset to a default value when {@link #close} is called. diff --git a/core/java/android/nfc/tech/MifareUltralight.java b/core/java/android/nfc/tech/MifareUltralight.java index ca74ebe..890b735 100644 --- a/core/java/android/nfc/tech/MifareUltralight.java +++ b/core/java/android/nfc/tech/MifareUltralight.java @@ -200,6 +200,9 @@ public final class MifareUltralight extends BasicTagTechnology { * and calling {@link NfcA#transceive}. Note that all MIFARE Classic * tags are based on {@link NfcA} technology. * + * <p>Use {@link #getMaxTransceiveLength} to retrieve the maximum number of bytes + * that can be sent with {@link #transceive}. + * * <p>This is an I/O operation and will block until complete. It must * not be called from the main application thread. A blocked call will be canceled with * {@link IOException} if {@link #close} is called from another thread. @@ -213,6 +216,14 @@ public final class MifareUltralight extends BasicTagTechnology { } /** + * Return the maximum number of bytes that can be sent with {@link #transceive}. + * @return the maximum number of bytes that can be sent with {@link #transceive}. + */ + public int getMaxTransceiveLength() { + return getMaxTransceiveLengthInternal(); + } + + /** * Set the timeout of {@link #transceive} in milliseconds. * <p>The timeout only applies to MifareUltralight {@link #transceive}, * and is reset to a default value when {@link #close} is called. diff --git a/core/java/android/nfc/tech/Ndef.java b/core/java/android/nfc/tech/Ndef.java index e4daa57..b266bb6 100644 --- a/core/java/android/nfc/tech/Ndef.java +++ b/core/java/android/nfc/tech/Ndef.java @@ -334,9 +334,11 @@ public final class Ndef extends BasicTagTechnology { * @return true if it is possible to make this tag read-only */ public boolean canMakeReadOnly() { - if (mNdefType == TYPE_1 || mNdefType == TYPE_2) { - return true; - } else { + INfcTag tagService = mTag.getTagService(); + try { + return tagService.canMakeReadOnly(mNdefType); + } catch (RemoteException e) { + Log.e(TAG, "NFC service dead", e); return false; } } diff --git a/core/java/android/nfc/tech/NfcA.java b/core/java/android/nfc/tech/NfcA.java index bd1f95a..bb8aec9 100644 --- a/core/java/android/nfc/tech/NfcA.java +++ b/core/java/android/nfc/tech/NfcA.java @@ -102,6 +102,9 @@ public final class NfcA extends BasicTagTechnology { * for example a SENS_REQ is not possible (these are used to * manage tag polling and initialization). * + * <p>Use {@link #getMaxTransceiveLength} to retrieve the maximum number of bytes + * that can be sent with {@link #transceive}. + * * <p>This is an I/O operation and will block until complete. It must * not be called from the main application thread. A blocked call will be canceled with * {@link IOException} if {@link #close} is called from another thread. @@ -118,6 +121,14 @@ public final class NfcA extends BasicTagTechnology { } /** + * Return the maximum number of bytes that can be sent with {@link #transceive}. + * @return the maximum number of bytes that can be sent with {@link #transceive}. + */ + public int getMaxTransceiveLength() { + return getMaxTransceiveLengthInternal(); + } + + /** * Set the timeout of {@link #transceive} in milliseconds. * <p>The timeout only applies to NfcA {@link #transceive}, and is * reset to a default value when {@link #close} is called. diff --git a/core/java/android/nfc/tech/NfcB.java b/core/java/android/nfc/tech/NfcB.java index 22cb11d..3ebd47f 100644 --- a/core/java/android/nfc/tech/NfcB.java +++ b/core/java/android/nfc/tech/NfcB.java @@ -97,6 +97,9 @@ public final class NfcB extends BasicTagTechnology { * <p>Applications must not send commands that manage the polling * loop and initialization (SENSB_REQ, SLOT_MARKER etc). * + * <p>Use {@link #getMaxTransceiveLength} to retrieve the maximum number of bytes + * that can be sent with {@link #transceive}. + * * <p>This is an I/O operation and will block until complete. It must * not be called from the main application thread. A blocked call will be canceled with * {@link IOException} if {@link #close} is called from another thread. @@ -111,4 +114,12 @@ public final class NfcB extends BasicTagTechnology { public byte[] transceive(byte[] data) throws IOException { return transceive(data, true); } + + /** + * Return the maximum number of bytes that can be sent with {@link #transceive}. + * @return the maximum number of bytes that can be sent with {@link #transceive}. + */ + public int getMaxTransceiveLength() { + return getMaxTransceiveLengthInternal(); + } } diff --git a/core/java/android/nfc/tech/NfcF.java b/core/java/android/nfc/tech/NfcF.java index 7b25a72..0938fb4 100644 --- a/core/java/android/nfc/tech/NfcF.java +++ b/core/java/android/nfc/tech/NfcF.java @@ -101,6 +101,9 @@ public final class NfcF extends BasicTagTechnology { * <p>Applications must not append the SoD (length) or EoD (CRC) to the payload, * it will be automatically calculated. * + * <p>Use {@link #getMaxTransceiveLength} to retrieve the maximum amount of bytes + * that can be sent with {@link #transceive}. + * * <p>This is an I/O operation and will block until complete. It must * not be called from the main application thread. A blocked call will be canceled with * {@link IOException} if {@link #close} is called from another thread. @@ -117,6 +120,14 @@ public final class NfcF extends BasicTagTechnology { } /** + * Return the maximum number of bytes that can be sent with {@link #transceive}. + * @return the maximum number of bytes that can be sent with {@link #transceive}. + */ + public int getMaxTransceiveLength() { + return getMaxTransceiveLengthInternal(); + } + + /** * Set the timeout of {@link #transceive} in milliseconds. * <p>The timeout only applies to NfcF {@link #transceive}, and is * reset to a default value when {@link #close} is called. diff --git a/core/java/android/nfc/tech/NfcV.java b/core/java/android/nfc/tech/NfcV.java index fe721c8..186c63b 100644 --- a/core/java/android/nfc/tech/NfcV.java +++ b/core/java/android/nfc/tech/NfcV.java @@ -97,6 +97,9 @@ public final class NfcV extends BasicTagTechnology { * it will be automatically calculated. The application does * provide FLAGS, CMD and PARAMETER bytes. * + * <p>Use {@link #getMaxTransceiveLength} to retrieve the maximum amount of bytes + * that can be sent with {@link #transceive}. + * * <p>This is an I/O operation and will block until complete. It must * not be called from the main application thread. A blocked call will be canceled with * {@link IOException} if {@link #close} is called from another thread. @@ -111,4 +114,13 @@ public final class NfcV extends BasicTagTechnology { public byte[] transceive(byte[] data) throws IOException { return transceive(data, true); } + + + /** + * Return the maximum number of bytes that can be sent with {@link #transceive}. + * @return the maximum number of bytes that can be sent with {@link #transceive}. + */ + public int getMaxTransceiveLength() { + return getMaxTransceiveLengthInternal(); + } } |