Merge "Remove attemptDeadServiceRecovery() from TagTechnology's." into gingerbread

This commit is contained in:
Nick Pelly
2011-01-10 12:02:36 -08:00
committed by Android (Google) Code Review
5 changed files with 62 additions and 49 deletions

View File

@@ -195,6 +195,7 @@ public final class NfcAdapter {
// attemptDeadServiceRecovery() when NFC crashes - we accept a best effort
// recovery
private static INfcAdapter sService;
private static INfcTag sTagService;
private final Context mContext;
@@ -233,6 +234,12 @@ public final class NfcAdapter {
Log.e(TAG, "could not retrieve NFC service");
return null;
}
try {
sTagService = sService.getNfcTagInterface();
} catch (RemoteException e) {
Log.e(TAG, "could not retrieve NFC Tag service");
return null;
}
}
return sService;
}
@@ -298,6 +305,14 @@ public final class NfcAdapter {
return sService;
}
/**
* Returns the binder interface to the tag service.
* @hide
*/
public INfcTag getTagService() {
return sTagService;
}
/**
* NFC service dead - attempt best effort recovery
* @hide
@@ -307,11 +322,21 @@ public final class NfcAdapter {
INfcAdapter service = getServiceInterface();
if (service == null) {
Log.e(TAG, "could not retrieve NFC service during service recovery");
// nothing more can be done now, sService is still stale, we'll hit
// this recovery path again later
return;
}
/* assigning to sService is not thread-safe, but this is best-effort code
* and on a well-behaved system should never happen */
// assigning to sService is not thread-safe, but this is best-effort code
// and on a well-behaved system should never happen
sService = service;
try {
sTagService = service.getNfcTagInterface();
} catch (RemoteException ee) {
Log.e(TAG, "could not retrieve NFC tag service during service recovery");
// nothing more can be done now, sService is still stale, we'll hit
// this recovery path again later
}
return;
}

View File

@@ -30,19 +30,14 @@ import android.util.Log;
* A base class for tag technologies that are built on top of transceive().
*/
/* package */ abstract class BasicTagTechnology implements TagTechnology {
private static final String TAG = "NFC";
/*package*/ final Tag mTag;
/*package*/ boolean mIsConnected;
/*package*/ int mSelectedTechnology;
private final NfcAdapter mAdapter;
// Following fields are final after construction, except for
// during attemptDeadServiceRecovery() when NFC crashes.
// Not locked - we accept a best effort attempt when NFC crashes.
/*package*/ INfcAdapter mService;
/*package*/ INfcTag mTagService;
private static final String TAG = "NFC";
/*package*/ final INfcAdapter mService;
/*package*/ final INfcTag mTagService;
/**
* @hide
@@ -64,11 +59,7 @@ import android.util.Log;
mAdapter = adapter;
mService = mAdapter.getService();
try {
mTagService = mService.getNfcTagInterface();
} catch (RemoteException e) {
attemptDeadServiceRecovery(e);
}
mTagService = mAdapter.getTagService();
mTag = tag;
mSelectedTechnology = tech;
}
@@ -80,19 +71,6 @@ import android.util.Log;
this(adapter, tag, tag.getTechnologyList()[0]);
}
/** NFC service dead - attempt best effort recovery */
/*package*/ void attemptDeadServiceRecovery(Exception e) {
mAdapter.attemptDeadServiceRecovery(e);
/* assigning to mService is not thread-safe, but this is best-effort code
* and on a well-behaved system should never happen */
mService = mAdapter.getService();
try {
mTagService = mService.getNfcTagInterface();
} catch (RemoteException e2) {
Log.e(TAG, "second RemoteException trying to recover from dead NFC service", e2);
}
}
/**
* Get the {@link Tag} this connection is associated with.
* <p>Requires {@link android.Manifest.permission#NFC} permission.
@@ -135,7 +113,7 @@ import android.util.Log;
try {
return mTagService.isPresent(mTag.getServiceHandle());
} catch (RemoteException e) {
attemptDeadServiceRecovery(e);
Log.e(TAG, "NFC service dead", e);
return false;
}
}
@@ -163,7 +141,7 @@ import android.util.Log;
throw new IOException();
}
} catch (RemoteException e) {
attemptDeadServiceRecovery(e);
Log.e(TAG, "NFC service dead", e);
throw new IOException("NFC service died");
}
}
@@ -183,21 +161,21 @@ import android.util.Log;
public void reconnect() throws IOException {
if (!mIsConnected) {
throw new IllegalStateException("Technology not connected yet");
} else {
try {
int errorCode = mTagService.reconnect(mTag.getServiceHandle());
}
if (errorCode != ErrorCodes.SUCCESS) {
mIsConnected = false;
mTag.setTechnologyDisconnected();
throw new IOException();
}
} catch (RemoteException e) {
try {
int errorCode = mTagService.reconnect(mTag.getServiceHandle());
if (errorCode != ErrorCodes.SUCCESS) {
mIsConnected = false;
mTag.setTechnologyDisconnected();
attemptDeadServiceRecovery(e);
throw new IOException("NFC service died");
throw new IOException();
}
} catch (RemoteException e) {
mIsConnected = false;
mTag.setTechnologyDisconnected();
Log.e(TAG, "NFC service dead", e);
throw new IOException("NFC service died");
}
}
@@ -219,7 +197,7 @@ import android.util.Log;
*/
mTagService.reconnect(mTag.getServiceHandle());
} catch (RemoteException e) {
attemptDeadServiceRecovery(e);
Log.e(TAG, "NFC service dead", e);
} finally {
mIsConnected = false;
mTag.setTechnologyDisconnected();
@@ -237,7 +215,7 @@ import android.util.Log;
}
return response;
} catch (RemoteException e) {
attemptDeadServiceRecovery(e);
Log.e(TAG, "NFC service dead", e);
throw new IOException("NFC service died");
}
}

View File

@@ -58,10 +58,14 @@ public final class IsoDep extends BasicTagTechnology {
/**
* 3A only
*/
public byte[] getHistoricalBytes() { return mHistBytes; }
public byte[] getHistoricalBytes() {
return mHistBytes;
}
/**
* 3B only
*/
public byte[] getHiLayerResponse() { return mHiLayerResponse; }
public byte[] getHiLayerResponse() {
return mHiLayerResponse;
}
}

View File

@@ -23,6 +23,7 @@ import android.nfc.NfcAdapter;
import android.nfc.Tag;
import android.os.Bundle;
import android.os.RemoteException;
import android.util.Log;
import java.io.IOException;
@@ -38,6 +39,8 @@ import java.io.IOException;
* permission.
*/
public final class Ndef extends BasicTagTechnology {
private static final String TAG = "NFC";
/** @hide */
public static final int NDEF_MODE_READ_ONLY = 1;
/** @hide */
@@ -168,7 +171,7 @@ public final class Ndef extends BasicTagTechnology {
return null;
}
} catch (RemoteException e) {
attemptDeadServiceRecovery(e);
Log.e(TAG, "NFC service dead", e);
return null;
}
}
@@ -200,7 +203,7 @@ public final class Ndef extends BasicTagTechnology {
throw new IOException("Tag is not ndef");
}
} catch (RemoteException e) {
attemptDeadServiceRecovery(e);
Log.e(TAG, "NFC service dead", e);
}
}
@@ -241,7 +244,7 @@ public final class Ndef extends BasicTagTechnology {
throw new IOException();
}
} catch (RemoteException e) {
attemptDeadServiceRecovery(e);
Log.e(TAG, "NFC service dead", e);
return false;
}
}

View File

@@ -23,6 +23,7 @@ import android.nfc.NfcAdapter;
import android.nfc.Tag;
import android.os.Bundle;
import android.os.RemoteException;
import android.util.Log;
import java.io.IOException;
@@ -36,6 +37,8 @@ import java.io.IOException;
* permission.
*/
public final class NdefFormatable extends BasicTagTechnology {
private static final String TAG = "NFC";
/**
* Internal constructor, to be used by NfcAdapter
* @hide
@@ -85,7 +88,7 @@ public final class NdefFormatable extends BasicTagTechnology {
throw new IOException();
}
} catch (RemoteException e) {
attemptDeadServiceRecovery(e);
Log.e(TAG, "NFC service dead", e);
}
}