Merge changes Ifcde87f4,Ie0606e5d am: f2d204c793

Original change: https://android-review.googlesource.com/c/platform/frameworks/base/+/2264526

Change-Id: Ie1fc56275d98e4a0e9114cae20ee71a274053274
Signed-off-by: Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>
This commit is contained in:
Alisher Alikhodjaev
2022-11-11 23:50:03 +00:00
committed by Automerger Merge Worker
3 changed files with 42 additions and 103 deletions

View File

@@ -29,7 +29,6 @@ import android.app.PendingIntent;
import android.compat.annotation.UnsupportedAppUsage; import android.compat.annotation.UnsupportedAppUsage;
import android.content.Context; import android.content.Context;
import android.content.IntentFilter; import android.content.IntentFilter;
import android.content.pm.IPackageManager;
import android.content.pm.PackageManager; import android.content.pm.PackageManager;
import android.net.Uri; import android.net.Uri;
import android.nfc.tech.MifareClassic; import android.nfc.tech.MifareClassic;
@@ -524,66 +523,6 @@ public final class NfcAdapter {
public boolean onUnlockAttempted(Tag tag); public boolean onUnlockAttempted(Tag tag);
} }
/**
* Helper to check if this device has FEATURE_NFC_BEAM, but without using
* a context.
* Equivalent to
* context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_NFC_BEAM)
*/
private static boolean hasBeamFeature() {
IPackageManager pm = ActivityThread.getPackageManager();
if (pm == null) {
Log.e(TAG, "Cannot get package manager, assuming no Android Beam feature");
return false;
}
try {
return pm.hasSystemFeature(PackageManager.FEATURE_NFC_BEAM, 0);
} catch (RemoteException e) {
Log.e(TAG, "Package manager query failed, assuming no Android Beam feature", e);
return false;
}
}
/**
* Helper to check if this device has FEATURE_NFC, but without using
* a context.
* Equivalent to
* context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_NFC)
*/
private static boolean hasNfcFeature() {
IPackageManager pm = ActivityThread.getPackageManager();
if (pm == null) {
Log.e(TAG, "Cannot get package manager, assuming no NFC feature");
return false;
}
try {
return pm.hasSystemFeature(PackageManager.FEATURE_NFC, 0);
} catch (RemoteException e) {
Log.e(TAG, "Package manager query failed, assuming no NFC feature", e);
return false;
}
}
/**
* Helper to check if this device is NFC HCE capable, by checking for
* FEATURE_NFC_HOST_CARD_EMULATION and/or FEATURE_NFC_HOST_CARD_EMULATION_NFCF,
* but without using a context.
*/
private static boolean hasNfcHceFeature() {
IPackageManager pm = ActivityThread.getPackageManager();
if (pm == null) {
Log.e(TAG, "Cannot get package manager, assuming no NFC feature");
return false;
}
try {
return pm.hasSystemFeature(PackageManager.FEATURE_NFC_HOST_CARD_EMULATION, 0)
|| pm.hasSystemFeature(PackageManager.FEATURE_NFC_HOST_CARD_EMULATION_NFCF, 0);
} catch (RemoteException e) {
Log.e(TAG, "Package manager query failed, assuming no NFC feature", e);
return false;
}
}
/** /**
* Return list of Secure Elements which support off host card emulation. * Return list of Secure Elements which support off host card emulation.
* *
@@ -593,23 +532,21 @@ public final class NfcAdapter {
* @hide * @hide
*/ */
public @NonNull List<String> getSupportedOffHostSecureElements() { public @NonNull List<String> getSupportedOffHostSecureElements() {
if (mContext == null) {
throw new UnsupportedOperationException("You need a context on NfcAdapter to use the "
+ " getSupportedOffHostSecureElements APIs");
}
List<String> offHostSE = new ArrayList<String>(); List<String> offHostSE = new ArrayList<String>();
IPackageManager pm = ActivityThread.getPackageManager(); PackageManager pm = mContext.getPackageManager();
if (pm == null) { if (pm == null) {
Log.e(TAG, "Cannot get package manager, assuming no off-host CE feature"); Log.e(TAG, "Cannot get package manager, assuming no off-host CE feature");
return offHostSE; return offHostSE;
} }
try { if (pm.hasSystemFeature(PackageManager.FEATURE_NFC_OFF_HOST_CARD_EMULATION_UICC)) {
if (pm.hasSystemFeature(PackageManager.FEATURE_NFC_OFF_HOST_CARD_EMULATION_UICC, 0)) { offHostSE.add("SIM");
offHostSE.add("SIM"); }
} if (pm.hasSystemFeature(PackageManager.FEATURE_NFC_OFF_HOST_CARD_EMULATION_ESE)) {
if (pm.hasSystemFeature(PackageManager.FEATURE_NFC_OFF_HOST_CARD_EMULATION_ESE, 0)) { offHostSE.add("eSE");
offHostSE.add("eSE");
}
} catch (RemoteException e) {
Log.e(TAG, "Package manager query failed, assuming no off-host CE feature", e);
offHostSE.clear();
return offHostSE;
} }
return offHostSE; return offHostSE;
} }
@@ -621,10 +558,19 @@ public final class NfcAdapter {
*/ */
@UnsupportedAppUsage @UnsupportedAppUsage
public static synchronized NfcAdapter getNfcAdapter(Context context) { public static synchronized NfcAdapter getNfcAdapter(Context context) {
if (context == null) {
if (sNullContextNfcAdapter == null) {
sNullContextNfcAdapter = new NfcAdapter(null);
}
return sNullContextNfcAdapter;
}
if (!sIsInitialized) { if (!sIsInitialized) {
sHasNfcFeature = hasNfcFeature(); PackageManager pm = context.getPackageManager();
sHasBeamFeature = hasBeamFeature(); sHasNfcFeature = pm.hasSystemFeature(PackageManager.FEATURE_NFC);
boolean hasHceFeature = hasNfcHceFeature(); sHasBeamFeature = pm.hasSystemFeature(PackageManager.FEATURE_NFC_BEAM);
boolean hasHceFeature =
pm.hasSystemFeature(PackageManager.FEATURE_NFC_HOST_CARD_EMULATION)
|| pm.hasSystemFeature(PackageManager.FEATURE_NFC_HOST_CARD_EMULATION_NFCF);
/* is this device meant to have NFC */ /* is this device meant to have NFC */
if (!sHasNfcFeature && !hasHceFeature) { if (!sHasNfcFeature && !hasHceFeature) {
Log.v(TAG, "this device does not have NFC support"); Log.v(TAG, "this device does not have NFC support");
@@ -660,12 +606,6 @@ public final class NfcAdapter {
sIsInitialized = true; sIsInitialized = true;
} }
if (context == null) {
if (sNullContextNfcAdapter == null) {
sNullContextNfcAdapter = new NfcAdapter(null);
}
return sNullContextNfcAdapter;
}
NfcAdapter adapter = sNfcAdapters.get(context); NfcAdapter adapter = sNfcAdapters.get(context);
if (adapter == null) { if (adapter == null) {
adapter = new NfcAdapter(context); adapter = new NfcAdapter(context);
@@ -676,8 +616,12 @@ public final class NfcAdapter {
/** get handle to NFC service interface */ /** get handle to NFC service interface */
private static INfcAdapter getServiceInterface() { private static INfcAdapter getServiceInterface() {
if (!sHasNfcFeature) {
/* NFC is not supported */
return null;
}
/* get a handle to NFC service */ /* get a handle to NFC service */
IBinder b = ServiceManager.getService("nfc"); IBinder b = ServiceManager.waitForService("nfc");
if (b == null) { if (b == null) {
return null; return null;
} }
@@ -707,6 +651,15 @@ public final class NfcAdapter {
"context not associated with any application (using a mock context?)"); "context not associated with any application (using a mock context?)");
} }
synchronized (NfcAdapter.class) {
if (!sIsInitialized) {
PackageManager pm = context.getPackageManager();
sHasNfcFeature = pm.hasSystemFeature(PackageManager.FEATURE_NFC);
}
if (!sHasNfcFeature) {
return null;
}
}
if (getServiceInterface() == null) { if (getServiceInterface() == null) {
// NFC is not available // NFC is not available
return null; return null;

View File

@@ -22,11 +22,9 @@ import android.annotation.RequiresPermission;
import android.annotation.SdkConstant; import android.annotation.SdkConstant;
import android.annotation.SdkConstant.SdkConstantType; import android.annotation.SdkConstant.SdkConstantType;
import android.app.Activity; import android.app.Activity;
import android.app.ActivityThread;
import android.content.ComponentName; import android.content.ComponentName;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.content.pm.IPackageManager;
import android.content.pm.PackageManager; import android.content.pm.PackageManager;
import android.nfc.INfcCardEmulation; import android.nfc.INfcCardEmulation;
import android.nfc.NfcAdapter; import android.nfc.NfcAdapter;
@@ -158,18 +156,13 @@ public final class CardEmulation {
throw new UnsupportedOperationException(); throw new UnsupportedOperationException();
} }
if (!sIsInitialized) { if (!sIsInitialized) {
IPackageManager pm = ActivityThread.getPackageManager(); PackageManager pm = context.getPackageManager();
if (pm == null) { if (pm == null) {
Log.e(TAG, "Cannot get PackageManager"); Log.e(TAG, "Cannot get PackageManager");
throw new UnsupportedOperationException(); throw new UnsupportedOperationException();
} }
try { if (!pm.hasSystemFeature(PackageManager.FEATURE_NFC_HOST_CARD_EMULATION)) {
if (!pm.hasSystemFeature(PackageManager.FEATURE_NFC_HOST_CARD_EMULATION, 0)) { Log.e(TAG, "This device does not support card emulation");
Log.e(TAG, "This device does not support card emulation");
throw new UnsupportedOperationException();
}
} catch (RemoteException e) {
Log.e(TAG, "PackageManager query failed.");
throw new UnsupportedOperationException(); throw new UnsupportedOperationException();
} }
sIsInitialized = true; sIsInitialized = true;

View File

@@ -17,10 +17,8 @@
package android.nfc.cardemulation; package android.nfc.cardemulation;
import android.app.Activity; import android.app.Activity;
import android.app.ActivityThread;
import android.content.ComponentName; import android.content.ComponentName;
import android.content.Context; import android.content.Context;
import android.content.pm.IPackageManager;
import android.content.pm.PackageManager; import android.content.pm.PackageManager;
import android.nfc.INfcFCardEmulation; import android.nfc.INfcFCardEmulation;
import android.nfc.NfcAdapter; import android.nfc.NfcAdapter;
@@ -70,18 +68,13 @@ public final class NfcFCardEmulation {
throw new UnsupportedOperationException(); throw new UnsupportedOperationException();
} }
if (!sIsInitialized) { if (!sIsInitialized) {
IPackageManager pm = ActivityThread.getPackageManager(); PackageManager pm = context.getPackageManager();
if (pm == null) { if (pm == null) {
Log.e(TAG, "Cannot get PackageManager"); Log.e(TAG, "Cannot get PackageManager");
throw new UnsupportedOperationException(); throw new UnsupportedOperationException();
} }
try { if (!pm.hasSystemFeature(PackageManager.FEATURE_NFC_HOST_CARD_EMULATION_NFCF)) {
if (!pm.hasSystemFeature(PackageManager.FEATURE_NFC_HOST_CARD_EMULATION_NFCF, 0)) { Log.e(TAG, "This device does not support NFC-F card emulation");
Log.e(TAG, "This device does not support NFC-F card emulation");
throw new UnsupportedOperationException();
}
} catch (RemoteException e) {
Log.e(TAG, "PackageManager query failed.");
throw new UnsupportedOperationException(); throw new UnsupportedOperationException();
} }
sIsInitialized = true; sIsInitialized = true;