diff options
| author | 2023-03-03 21:30:38 +0000 | |
|---|---|---|
| committer | 2023-03-03 21:31:21 +0000 | |
| commit | 12fed4b17adb0605998c7609d068ff652363589f (patch) | |
| tree | 9ee518ea1af473b2c2feffc3772ed5ecdb8fef03 | |
| parent | d6cd6eb7be630bc9ce7defa4599ffcd78446e4dc (diff) | |
Revert "nfc(api): Remove fg checks from API class"
Revert submission 2448864-nfc_remove_activity_fg_check_in_api
Reason for revert: Maybe the root cause of b/271214245
Reverted changes: /q/submissionid:2448864-nfc_remove_activity_fg_check_in_api
Bug: 263565738
Test: N/A
Change-Id: I594f99c8a90682c78704f96a2210277b2b2b01d6
| -rw-r--r-- | core/java/android/nfc/NfcAdapter.java | 25 |
1 files changed, 25 insertions, 0 deletions
diff --git a/core/java/android/nfc/NfcAdapter.java b/core/java/android/nfc/NfcAdapter.java index 611df0a4ea90..6dc80cf4c374 100644 --- a/core/java/android/nfc/NfcAdapter.java +++ b/core/java/android/nfc/NfcAdapter.java @@ -26,6 +26,8 @@ import android.annotation.SdkConstant.SdkConstantType; import android.annotation.SystemApi; import android.annotation.UserIdInt; import android.app.Activity; +import android.app.ActivityThread; +import android.app.OnActivityPausedListener; import android.app.PendingIntent; import android.compat.annotation.UnsupportedAppUsage; import android.content.Context; @@ -1568,11 +1570,17 @@ public final class NfcAdapter { if (activity == null || intent == null) { throw new NullPointerException(); } + if (!activity.isResumed()) { + throw new IllegalStateException("Foreground dispatch can only be enabled " + + "when your activity is resumed"); + } try { TechListParcel parcel = null; if (techLists != null && techLists.length > 0) { parcel = new TechListParcel(techLists); } + ActivityThread.currentActivityThread().registerOnActivityPausedListener(activity, + mForegroundDispatchListener); sService.setForegroundDispatch(intent, filters, parcel); } catch (RemoteException e) { attemptDeadServiceRecovery(e); @@ -1600,8 +1608,25 @@ public final class NfcAdapter { throw new UnsupportedOperationException(); } } + ActivityThread.currentActivityThread().unregisterOnActivityPausedListener(activity, + mForegroundDispatchListener); + disableForegroundDispatchInternal(activity, false); + } + + OnActivityPausedListener mForegroundDispatchListener = new OnActivityPausedListener() { + @Override + public void onPaused(Activity activity) { + disableForegroundDispatchInternal(activity, true); + } + }; + + void disableForegroundDispatchInternal(Activity activity, boolean force) { try { sService.setForegroundDispatch(null, null, null); + if (!force && !activity.isResumed()) { + throw new IllegalStateException("You must disable foreground dispatching " + + "while your activity is still resumed"); + } } catch (RemoteException e) { attemptDeadServiceRecovery(e); } |