Catch SecurityException from using a stale Tag object

Applications are not supposed to be able to do any I/O using a stale Tag
object. This was not checked in older Android versions, but enforced
later since Android 13 (and possibly in Android 12L).

When we are checking for a disconnected tag, the Tag object can become
stale if the same tag (or another tag) gets rediscovered. In addition,
if NfcService is restarted for some reason, the cookie value used to
check for this will also be reset to 0. In any case, a SecurityException
could be raised when checking for a disconnected tag, and we should be
able to handle this case.
This commit is contained in:
Peter Cai 2022-12-12 18:27:40 -05:00
parent 6f38af1582
commit 8f9728cd60
1 changed files with 10 additions and 4 deletions

View File

@ -331,12 +331,18 @@ public class SecurityTokenOperationActivity extends BaseSecurityTokenActivity {
protected Void doInBackground(Void... params) {
// check all 200ms if Security Token has been taken away
while (true) {
if (stConnection.isConnected()) {
try {
try {
if (stConnection.isConnected()) {
Thread.sleep(200);
} catch (InterruptedException ignored) {
} else {
return null;
}
} else {
} catch (InterruptedException ignored) {
// Sleep interrupted; ignore
} catch (SecurityException ignored) {
// In newer version of Android, isConnected() can throw an SecurityException
// when the Tag object becomes "stale"; this simply means the tag has been removed
// (and possibly rediscovered), so we can safely break from here.
return null;
}
}