Skip to content

Commit 9293937

Browse files
Nick PellyAndroid (Google) Code Review
authored andcommitted
Merge "Remove attemptDeadServiceRecovery() from TagTechnology's." into gingerbread
2 parents d64d711 + 3dd6c45 commit 9293937

File tree

5 files changed

+62
-49
lines changed

5 files changed

+62
-49
lines changed

core/java/android/nfc/NfcAdapter.java

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,7 @@ public final class NfcAdapter {
195195
// attemptDeadServiceRecovery() when NFC crashes - we accept a best effort
196196
// recovery
197197
private static INfcAdapter sService;
198+
private static INfcTag sTagService;
198199

199200
private final Context mContext;
200201

@@ -233,6 +234,12 @@ private static synchronized INfcAdapter setupService() {
233234
Log.e(TAG, "could not retrieve NFC service");
234235
return null;
235236
}
237+
try {
238+
sTagService = sService.getNfcTagInterface();
239+
} catch (RemoteException e) {
240+
Log.e(TAG, "could not retrieve NFC Tag service");
241+
return null;
242+
}
236243
}
237244
return sService;
238245
}
@@ -298,6 +305,14 @@ public INfcAdapter getService() {
298305
return sService;
299306
}
300307

308+
/**
309+
* Returns the binder interface to the tag service.
310+
* @hide
311+
*/
312+
public INfcTag getTagService() {
313+
return sTagService;
314+
}
315+
301316
/**
302317
* NFC service dead - attempt best effort recovery
303318
* @hide
@@ -307,11 +322,21 @@ public void attemptDeadServiceRecovery(Exception e) {
307322
INfcAdapter service = getServiceInterface();
308323
if (service == null) {
309324
Log.e(TAG, "could not retrieve NFC service during service recovery");
325+
// nothing more can be done now, sService is still stale, we'll hit
326+
// this recovery path again later
310327
return;
311328
}
312-
/* assigning to sService is not thread-safe, but this is best-effort code
313-
* and on a well-behaved system should never happen */
329+
// assigning to sService is not thread-safe, but this is best-effort code
330+
// and on a well-behaved system should never happen
314331
sService = service;
332+
try {
333+
sTagService = service.getNfcTagInterface();
334+
} catch (RemoteException ee) {
335+
Log.e(TAG, "could not retrieve NFC tag service during service recovery");
336+
// nothing more can be done now, sService is still stale, we'll hit
337+
// this recovery path again later
338+
}
339+
315340
return;
316341
}
317342

core/java/android/nfc/technology/BasicTagTechnology.java

Lines changed: 19 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -30,19 +30,14 @@
3030
* A base class for tag technologies that are built on top of transceive().
3131
*/
3232
/* package */ abstract class BasicTagTechnology implements TagTechnology {
33+
private static final String TAG = "NFC";
3334

3435
/*package*/ final Tag mTag;
3536
/*package*/ boolean mIsConnected;
3637
/*package*/ int mSelectedTechnology;
3738
private final NfcAdapter mAdapter;
38-
39-
// Following fields are final after construction, except for
40-
// during attemptDeadServiceRecovery() when NFC crashes.
41-
// Not locked - we accept a best effort attempt when NFC crashes.
42-
/*package*/ INfcAdapter mService;
43-
/*package*/ INfcTag mTagService;
44-
45-
private static final String TAG = "NFC";
39+
/*package*/ final INfcAdapter mService;
40+
/*package*/ final INfcTag mTagService;
4641

4742
/**
4843
* @hide
@@ -64,11 +59,7 @@ public BasicTagTechnology(NfcAdapter adapter, Tag tag, int tech) throws RemoteEx
6459

6560
mAdapter = adapter;
6661
mService = mAdapter.getService();
67-
try {
68-
mTagService = mService.getNfcTagInterface();
69-
} catch (RemoteException e) {
70-
attemptDeadServiceRecovery(e);
71-
}
62+
mTagService = mAdapter.getTagService();
7263
mTag = tag;
7364
mSelectedTechnology = tech;
7465
}
@@ -80,19 +71,6 @@ public BasicTagTechnology(NfcAdapter adapter, Tag tag) throws RemoteException {
8071
this(adapter, tag, tag.getTechnologyList()[0]);
8172
}
8273

83-
/** NFC service dead - attempt best effort recovery */
84-
/*package*/ void attemptDeadServiceRecovery(Exception e) {
85-
mAdapter.attemptDeadServiceRecovery(e);
86-
/* assigning to mService is not thread-safe, but this is best-effort code
87-
* and on a well-behaved system should never happen */
88-
mService = mAdapter.getService();
89-
try {
90-
mTagService = mService.getNfcTagInterface();
91-
} catch (RemoteException e2) {
92-
Log.e(TAG, "second RemoteException trying to recover from dead NFC service", e2);
93-
}
94-
}
95-
9674
/**
9775
* Get the {@link Tag} this connection is associated with.
9876
* <p>Requires {@link android.Manifest.permission#NFC} permission.
@@ -135,7 +113,7 @@ public boolean isConnected() {
135113
try {
136114
return mTagService.isPresent(mTag.getServiceHandle());
137115
} catch (RemoteException e) {
138-
attemptDeadServiceRecovery(e);
116+
Log.e(TAG, "NFC service dead", e);
139117
return false;
140118
}
141119
}
@@ -163,7 +141,7 @@ public void connect() throws IOException {
163141
throw new IOException();
164142
}
165143
} catch (RemoteException e) {
166-
attemptDeadServiceRecovery(e);
144+
Log.e(TAG, "NFC service dead", e);
167145
throw new IOException("NFC service died");
168146
}
169147
}
@@ -183,21 +161,21 @@ public void connect() throws IOException {
183161
public void reconnect() throws IOException {
184162
if (!mIsConnected) {
185163
throw new IllegalStateException("Technology not connected yet");
186-
} else {
187-
try {
188-
int errorCode = mTagService.reconnect(mTag.getServiceHandle());
164+
}
165+
166+
try {
167+
int errorCode = mTagService.reconnect(mTag.getServiceHandle());
189168

190-
if (errorCode != ErrorCodes.SUCCESS) {
191-
mIsConnected = false;
192-
mTag.setTechnologyDisconnected();
193-
throw new IOException();
194-
}
195-
} catch (RemoteException e) {
169+
if (errorCode != ErrorCodes.SUCCESS) {
196170
mIsConnected = false;
197171
mTag.setTechnologyDisconnected();
198-
attemptDeadServiceRecovery(e);
199-
throw new IOException("NFC service died");
172+
throw new IOException();
200173
}
174+
} catch (RemoteException e) {
175+
mIsConnected = false;
176+
mTag.setTechnologyDisconnected();
177+
Log.e(TAG, "NFC service dead", e);
178+
throw new IOException("NFC service died");
201179
}
202180
}
203181

@@ -219,7 +197,7 @@ public void close() {
219197
*/
220198
mTagService.reconnect(mTag.getServiceHandle());
221199
} catch (RemoteException e) {
222-
attemptDeadServiceRecovery(e);
200+
Log.e(TAG, "NFC service dead", e);
223201
} finally {
224202
mIsConnected = false;
225203
mTag.setTechnologyDisconnected();
@@ -237,7 +215,7 @@ public void close() {
237215
}
238216
return response;
239217
} catch (RemoteException e) {
240-
attemptDeadServiceRecovery(e);
218+
Log.e(TAG, "NFC service dead", e);
241219
throw new IOException("NFC service died");
242220
}
243221
}

core/java/android/nfc/technology/IsoDep.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,14 @@ public IsoDep(NfcAdapter adapter, Tag tag, Bundle extras)
5858
/**
5959
* 3A only
6060
*/
61-
public byte[] getHistoricalBytes() { return mHistBytes; }
61+
public byte[] getHistoricalBytes() {
62+
return mHistBytes;
63+
}
6264

6365
/**
6466
* 3B only
6567
*/
66-
public byte[] getHiLayerResponse() { return mHiLayerResponse; }
68+
public byte[] getHiLayerResponse() {
69+
return mHiLayerResponse;
70+
}
6771
}

core/java/android/nfc/technology/Ndef.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import android.nfc.Tag;
2424
import android.os.Bundle;
2525
import android.os.RemoteException;
26+
import android.util.Log;
2627

2728
import java.io.IOException;
2829

@@ -38,6 +39,8 @@
3839
* permission.
3940
*/
4041
public final class Ndef extends BasicTagTechnology {
42+
private static final String TAG = "NFC";
43+
4144
/** @hide */
4245
public static final int NDEF_MODE_READ_ONLY = 1;
4346
/** @hide */
@@ -168,7 +171,7 @@ public NdefMessage getNdefMessage() throws IOException, FormatException {
168171
return null;
169172
}
170173
} catch (RemoteException e) {
171-
attemptDeadServiceRecovery(e);
174+
Log.e(TAG, "NFC service dead", e);
172175
return null;
173176
}
174177
}
@@ -200,7 +203,7 @@ public void writeNdefMessage(NdefMessage msg) throws IOException, FormatExceptio
200203
throw new IOException("Tag is not ndef");
201204
}
202205
} catch (RemoteException e) {
203-
attemptDeadServiceRecovery(e);
206+
Log.e(TAG, "NFC service dead", e);
204207
}
205208
}
206209

@@ -241,7 +244,7 @@ public boolean makeReadonly() throws IOException {
241244
throw new IOException();
242245
}
243246
} catch (RemoteException e) {
244-
attemptDeadServiceRecovery(e);
247+
Log.e(TAG, "NFC service dead", e);
245248
return false;
246249
}
247250
}

core/java/android/nfc/technology/NdefFormatable.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import android.nfc.Tag;
2424
import android.os.Bundle;
2525
import android.os.RemoteException;
26+
import android.util.Log;
2627

2728
import java.io.IOException;
2829

@@ -36,6 +37,8 @@
3637
* permission.
3738
*/
3839
public final class NdefFormatable extends BasicTagTechnology {
40+
private static final String TAG = "NFC";
41+
3942
/**
4043
* Internal constructor, to be used by NfcAdapter
4144
* @hide
@@ -85,7 +88,7 @@ public void format(NdefMessage firstMessage) throws IOException, FormatException
8588
throw new IOException();
8689
}
8790
} catch (RemoteException e) {
88-
attemptDeadServiceRecovery(e);
91+
Log.e(TAG, "NFC service dead", e);
8992
}
9093
}
9194

0 commit comments

Comments
 (0)