Adjust Tag availability checking mechanism
1. The nonce could only set by NfcService
2. Use random number instead of the system time
3. Clear the nonce after disconnecting to the tag
Bug: 199291025
Test: test with some reader apps, test pass
Change-Id: Idfce8dfe75282d10b00fab68dda8c9a8629757a3
Merged-In: Idfce8dfe75282d10b00fab68dda8c9a8629757a3
diff --git a/src/com/android/nfc/NfcService.java b/src/com/android/nfc/NfcService.java
index 6ab45a5..bb6caca 100644
--- a/src/com/android/nfc/NfcService.java
+++ b/src/com/android/nfc/NfcService.java
@@ -108,6 +108,7 @@
import java.io.UnsupportedEncodingException;
import java.nio.ByteBuffer;
import java.nio.file.Files;
+import java.security.SecureRandom;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
@@ -260,6 +261,8 @@
private final BackupManager mBackupManager;
+ private final SecureRandom mCookieGenerator = new SecureRandom();
+
// cached version of installed packages requesting Android.permission.NFC_TRANSACTION_EVENTS
// for current user and profiles. The Integer part is the userId.
HashMap<Integer, List<String>> mNfcEventInstalledPackages =
@@ -343,7 +346,7 @@
boolean mNotifyReadFailed;
// for recording the latest Tag object cookie
- long mCookieUpToDate = 0;
+ long mCookieUpToDate = -1;
private NfcDispatcher mNfcDispatcher;
private PowerManager mPowerManager;
@@ -2021,8 +2024,10 @@
tag.findAndReadNdef();
// Build a new Tag object to return
try {
+ /* Avoid setting mCookieUpToDate to negative values */
+ mCookieUpToDate = mCookieGenerator.nextLong() >>> 1;
Tag newTag = new Tag(tag.getUid(), tag.getTechList(),
- tag.getTechExtras(), tag.getHandle(), this);
+ tag.getTechExtras(), tag.getHandle(), mCookieUpToDate, this);
return newTag;
} catch (Exception e) {
Log.e(TAG, "Tag creation exception.", e);
@@ -2073,14 +2078,8 @@
}
@Override
- public void setTagUpToDate(long cookie) throws RemoteException {
- if (DBG) Log.d(TAG, "Register Tag " + Long.toString(cookie) + " as the latest");
- mCookieUpToDate = cookie;
- }
-
- @Override
public boolean isTagUpToDate(long cookie) throws RemoteException {
- if (mCookieUpToDate == cookie) {
+ if (mCookieUpToDate != -1 && mCookieUpToDate == cookie) {
if (DBG) Log.d(TAG, "Tag " + Long.toString(cookie) + " is up to date");
return true;
}
@@ -2622,9 +2621,11 @@
extras.putInt(Ndef.EXTRA_NDEF_MAXLENGTH, 0);
extras.putInt(Ndef.EXTRA_NDEF_CARDSTATE, Ndef.NDEF_MODE_READ_ONLY);
extras.putInt(Ndef.EXTRA_NDEF_TYPE, Ndef.TYPE_OTHER);
+ /* Avoid setting mCookieUpToDate to negative values */
+ mCookieUpToDate = mCookieGenerator.nextLong() >>> 1;
Tag tag = Tag.createMockTag(new byte[]{0x00},
new int[]{TagTechnology.NDEF},
- new Bundle[]{extras});
+ new Bundle[]{extras}, mCookieUpToDate);
Log.d(TAG, "mock NDEF tag, starting corresponding activity");
Log.d(TAG, tag.toString());
int dispatchStatus = mNfcDispatcher.dispatchTag(tag);
@@ -2654,6 +2655,7 @@
new DeviceHost.TagDisconnectedCallback() {
@Override
public void onTagDisconnected(long handle) {
+ mCookieUpToDate = -1;
applyRouting(false);
}
};
@@ -3105,8 +3107,11 @@
private void dispatchTagEndpoint(TagEndpoint tagEndpoint, ReaderModeParams readerParams) {
try {
+ /* Avoid setting mCookieUpToDate to negative values */
+ mCookieUpToDate = mCookieGenerator.nextLong() >>> 1;
Tag tag = new Tag(tagEndpoint.getUid(), tagEndpoint.getTechList(),
- tagEndpoint.getTechExtras(), tagEndpoint.getHandle(), mNfcTagService);
+ tagEndpoint.getTechExtras(), tagEndpoint.getHandle(),
+ mCookieUpToDate, mNfcTagService);
registerTagObject(tagEndpoint);
if (readerParams != null) {
try {