Merge changes from topic "Backport: tag availability checking mechanism" into sc-v2-dev

* changes:
  Add null checking to fix NullPointerException while creating mock tag
  Add tag availability checking mechanism
This commit is contained in:
Chloe Dai
2022-03-08 02:13:47 +00:00
committed by Android (Google) Code Review
2 changed files with 32 additions and 0 deletions

View File

@@ -45,4 +45,7 @@ interface INfcTag
boolean canMakeReadOnly(int ndefType); boolean canMakeReadOnly(int ndefType);
int getMaxTransceiveLength(int technology); int getMaxTransceiveLength(int technology);
boolean getExtendedLengthApdusSupported(); boolean getExtendedLengthApdusSupported();
void setTagUpToDate(long cookie);
boolean isTagUpToDate(long cookie);
} }

View File

@@ -34,6 +34,7 @@ import android.os.Bundle;
import android.os.Parcel; import android.os.Parcel;
import android.os.Parcelable; import android.os.Parcelable;
import android.os.RemoteException; import android.os.RemoteException;
import android.os.SystemClock;
import java.io.IOException; import java.io.IOException;
import java.util.Arrays; import java.util.Arrays;
@@ -121,6 +122,7 @@ public final class Tag implements Parcelable {
final INfcTag mTagService; // interface to NFC service, will be null if mock tag final INfcTag mTagService; // interface to NFC service, will be null if mock tag
int mConnectedTechnology; int mConnectedTechnology;
long mCookie;
/** /**
* Hidden constructor to be used by NFC service and internal classes. * Hidden constructor to be used by NFC service and internal classes.
@@ -140,6 +142,17 @@ public final class Tag implements Parcelable {
mTagService = tagService; mTagService = tagService;
mConnectedTechnology = -1; mConnectedTechnology = -1;
mCookie = SystemClock.elapsedRealtime();
if (tagService == null) {
return;
}
try {
tagService.setTagUpToDate(mCookie);
} catch (RemoteException e) {
throw e.rethrowAsRuntimeException();
}
} }
/** /**
@@ -361,6 +374,22 @@ public final class Tag implements Parcelable {
/** @hide */ /** @hide */
@UnsupportedAppUsage @UnsupportedAppUsage
public INfcTag getTagService() { public INfcTag getTagService() {
if (mTagService == null) {
return null;
}
try {
if (!mTagService.isTagUpToDate(mCookie)) {
String id_str = "";
for (int i = 0; i < mId.length; i++) {
id_str = id_str + String.format("%02X ", mId[i]);
}
String msg = "Permission Denial: Tag ( ID: " + id_str + ") is out of date";
throw new SecurityException(msg);
}
} catch (RemoteException e) {
throw e.rethrowAsRuntimeException();
}
return mTagService; return mTagService;
} }