Fix the feature flag mis-checking cause exception issue

Some HCE supported functions return UnsupportedOperationException
when they were called by HCE only devices. Add the condition to
fix the issue.

Bug: 267846276
Test: Run "Screen off HCE Payment" with CtsVerifier and see if it will
crash

Change-Id: Ibdacf947dcfc9e57015a03e38b836745b7353713
Merged-In: Ibdacf947dcfc9e57015a03e38b836745b7353713
This commit is contained in:
chloedai
2023-02-07 09:43:08 +00:00
parent 26043b2920
commit 0b1be85e6d

View File

@@ -420,6 +420,7 @@ public final class NfcAdapter {
// Guarded by NfcAdapter.class // Guarded by NfcAdapter.class
static boolean sIsInitialized = false; static boolean sIsInitialized = false;
static boolean sHasNfcFeature; static boolean sHasNfcFeature;
static boolean sHasCeFeature;
// Final after first constructor, except for // Final after first constructor, except for
// attemptDeadServiceRecovery() when NFC crashes - we accept a best effort // attemptDeadServiceRecovery() when NFC crashes - we accept a best effort
@@ -616,11 +617,13 @@ public final class NfcAdapter {
PackageManager pm; PackageManager pm;
pm = context.getPackageManager(); pm = context.getPackageManager();
sHasNfcFeature = pm.hasSystemFeature(PackageManager.FEATURE_NFC); sHasNfcFeature = pm.hasSystemFeature(PackageManager.FEATURE_NFC);
boolean hasHceFeature = sHasCeFeature =
pm.hasSystemFeature(PackageManager.FEATURE_NFC_HOST_CARD_EMULATION) pm.hasSystemFeature(PackageManager.FEATURE_NFC_HOST_CARD_EMULATION)
|| pm.hasSystemFeature(PackageManager.FEATURE_NFC_HOST_CARD_EMULATION_NFCF); || pm.hasSystemFeature(PackageManager.FEATURE_NFC_HOST_CARD_EMULATION_NFCF)
|| pm.hasSystemFeature(PackageManager.FEATURE_NFC_OFF_HOST_CARD_EMULATION_UICC)
|| pm.hasSystemFeature(PackageManager.FEATURE_NFC_OFF_HOST_CARD_EMULATION_ESE);
/* is this device meant to have NFC */ /* is this device meant to have NFC */
if (!sHasNfcFeature && !hasHceFeature) { if (!sHasNfcFeature && !sHasCeFeature) {
Log.v(TAG, "this device does not have NFC support"); Log.v(TAG, "this device does not have NFC support");
throw new UnsupportedOperationException(); throw new UnsupportedOperationException();
} }
@@ -643,7 +646,7 @@ public final class NfcAdapter {
throw new UnsupportedOperationException(); throw new UnsupportedOperationException();
} }
} }
if (hasHceFeature) { if (sHasCeFeature) {
try { try {
sNfcFCardEmulationService = sService.getNfcFCardEmulationInterface(); sNfcFCardEmulationService = sService.getNfcFCardEmulationInterface();
} catch (RemoteException e) { } catch (RemoteException e) {
@@ -1669,7 +1672,7 @@ public final class NfcAdapter {
@SystemApi @SystemApi
@RequiresPermission(android.Manifest.permission.WRITE_SECURE_SETTINGS) @RequiresPermission(android.Manifest.permission.WRITE_SECURE_SETTINGS)
public boolean enableSecureNfc(boolean enable) { public boolean enableSecureNfc(boolean enable) {
if (!sHasNfcFeature) { if (!sHasNfcFeature && !sHasCeFeature) {
throw new UnsupportedOperationException(); throw new UnsupportedOperationException();
} }
try { try {
@@ -1694,10 +1697,13 @@ public final class NfcAdapter {
* Checks if the device supports Secure NFC functionality. * Checks if the device supports Secure NFC functionality.
* *
* @return True if device supports Secure NFC, false otherwise * @return True if device supports Secure NFC, false otherwise
* @throws UnsupportedOperationException if FEATURE_NFC is unavailable. * @throws UnsupportedOperationException if FEATURE_NFC,
* FEATURE_NFC_HOST_CARD_EMULATION, FEATURE_NFC_HOST_CARD_EMULATION_NFCF,
* FEATURE_NFC_OFF_HOST_CARD_EMULATION_UICC and FEATURE_NFC_OFF_HOST_CARD_EMULATION_ESE
* are unavailable
*/ */
public boolean isSecureNfcSupported() { public boolean isSecureNfcSupported() {
if (!sHasNfcFeature) { if (!sHasNfcFeature && !sHasCeFeature) {
throw new UnsupportedOperationException(); throw new UnsupportedOperationException();
} }
try { try {
@@ -1723,11 +1729,14 @@ public final class NfcAdapter {
* such as their relative positioning on the device. * such as their relative positioning on the device.
* *
* @return Information on the nfc antenna(s) on the device. * @return Information on the nfc antenna(s) on the device.
* @throws UnsupportedOperationException if FEATURE_NFC is unavailable. * @throws UnsupportedOperationException if FEATURE_NFC,
* FEATURE_NFC_HOST_CARD_EMULATION, FEATURE_NFC_HOST_CARD_EMULATION_NFCF,
* FEATURE_NFC_OFF_HOST_CARD_EMULATION_UICC and FEATURE_NFC_OFF_HOST_CARD_EMULATION_ESE
* are unavailable
*/ */
@Nullable @Nullable
public NfcAntennaInfo getNfcAntennaInfo() { public NfcAntennaInfo getNfcAntennaInfo() {
if (!sHasNfcFeature) { if (!sHasNfcFeature && !sHasCeFeature) {
throw new UnsupportedOperationException(); throw new UnsupportedOperationException();
} }
try { try {
@@ -1752,12 +1761,15 @@ public final class NfcAdapter {
* Checks Secure NFC feature is enabled. * Checks Secure NFC feature is enabled.
* *
* @return True if Secure NFC is enabled, false otherwise * @return True if Secure NFC is enabled, false otherwise
* @throws UnsupportedOperationException if FEATURE_NFC is unavailable. * @throws UnsupportedOperationException if FEATURE_NFC,
* FEATURE_NFC_HOST_CARD_EMULATION, FEATURE_NFC_HOST_CARD_EMULATION_NFCF,
* FEATURE_NFC_OFF_HOST_CARD_EMULATION_UICC and FEATURE_NFC_OFF_HOST_CARD_EMULATION_ESE
* are unavailable
* @throws UnsupportedOperationException if device doesn't support * @throws UnsupportedOperationException if device doesn't support
* Secure NFC functionality. {@link #isSecureNfcSupported} * Secure NFC functionality. {@link #isSecureNfcSupported}
*/ */
public boolean isSecureNfcEnabled() { public boolean isSecureNfcEnabled() {
if (!sHasNfcFeature) { if (!sHasNfcFeature && !sHasCeFeature) {
throw new UnsupportedOperationException(); throw new UnsupportedOperationException();
} }
try { try {
@@ -2071,14 +2083,17 @@ public final class NfcAdapter {
* always on. * always on.
* @param value if true the NFCC will be kept on (with no RF enabled if NFC adapter is * @param value if true the NFCC will be kept on (with no RF enabled if NFC adapter is
* disabled), if false the NFCC will follow completely the Nfc adapter state. * disabled), if false the NFCC will follow completely the Nfc adapter state.
* @throws UnsupportedOperationException if FEATURE_NFC is unavailable. * @throws UnsupportedOperationException if FEATURE_NFC,
* FEATURE_NFC_HOST_CARD_EMULATION, FEATURE_NFC_HOST_CARD_EMULATION_NFCF,
* FEATURE_NFC_OFF_HOST_CARD_EMULATION_UICC and FEATURE_NFC_OFF_HOST_CARD_EMULATION_ESE
* are unavailable
* @return void * @return void
* @hide * @hide
*/ */
@SystemApi @SystemApi
@RequiresPermission(android.Manifest.permission.NFC_SET_CONTROLLER_ALWAYS_ON) @RequiresPermission(android.Manifest.permission.NFC_SET_CONTROLLER_ALWAYS_ON)
public boolean setControllerAlwaysOn(boolean value) { public boolean setControllerAlwaysOn(boolean value) {
if (!sHasNfcFeature) { if (!sHasNfcFeature && !sHasCeFeature) {
throw new UnsupportedOperationException(); throw new UnsupportedOperationException();
} }
try { try {
@@ -2103,7 +2118,10 @@ public final class NfcAdapter {
* Checks NFC controller always on feature is enabled. * Checks NFC controller always on feature is enabled.
* *
* @return True if NFC controller always on is enabled, false otherwise * @return True if NFC controller always on is enabled, false otherwise
* @throws UnsupportedOperationException if FEATURE_NFC is unavailable. * @throws UnsupportedOperationException if FEATURE_NFC,
* FEATURE_NFC_HOST_CARD_EMULATION, FEATURE_NFC_HOST_CARD_EMULATION_NFCF,
* FEATURE_NFC_OFF_HOST_CARD_EMULATION_UICC and FEATURE_NFC_OFF_HOST_CARD_EMULATION_ESE
* are unavailable
* @hide * @hide
*/ */
@SystemApi @SystemApi
@@ -2131,13 +2149,16 @@ public final class NfcAdapter {
* Checks if the device supports NFC controller always on functionality. * Checks if the device supports NFC controller always on functionality.
* *
* @return True if device supports NFC controller always on, false otherwise * @return True if device supports NFC controller always on, false otherwise
* @throws UnsupportedOperationException if FEATURE_NFC is unavailable. * @throws UnsupportedOperationException if FEATURE_NFC,
* FEATURE_NFC_HOST_CARD_EMULATION, FEATURE_NFC_HOST_CARD_EMULATION_NFCF,
* FEATURE_NFC_OFF_HOST_CARD_EMULATION_UICC and FEATURE_NFC_OFF_HOST_CARD_EMULATION_ESE
* are unavailable
* @hide * @hide
*/ */
@SystemApi @SystemApi
@RequiresPermission(android.Manifest.permission.NFC_SET_CONTROLLER_ALWAYS_ON) @RequiresPermission(android.Manifest.permission.NFC_SET_CONTROLLER_ALWAYS_ON)
public boolean isControllerAlwaysOnSupported() { public boolean isControllerAlwaysOnSupported() {
if (!sHasNfcFeature) { if (!sHasNfcFeature && !sHasCeFeature) {
throw new UnsupportedOperationException(); throw new UnsupportedOperationException();
} }
try { try {