1 /* 2 * Copyright (C) 2025 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package android.nfc; 18 19 import android.app.Activity; 20 import android.compat.annotation.UnsupportedAppUsage; 21 import android.os.Binder; 22 import android.os.Bundle; 23 24 /** 25 * NFC state associated with an {@link Activity} 26 * 27 * @hide 28 */ 29 public class NfcActivityState { 30 /** 31 * @hide 32 */ 33 @UnsupportedAppUsage 34 boolean resumed = false; 35 /** 36 * @hide 37 */ 38 @UnsupportedAppUsage 39 Activity activity; 40 /** 41 * @hide 42 */ 43 @UnsupportedAppUsage 44 NfcAdapter.ReaderCallback readerCallback = null; 45 /** 46 * @hide 47 */ 48 @UnsupportedAppUsage 49 int readerModeFlags = 0; 50 /** 51 * @hide 52 */ 53 @UnsupportedAppUsage 54 Bundle readerModeExtras = null; 55 /** 56 * @hide 57 */ 58 @UnsupportedAppUsage 59 Binder token; 60 61 /** 62 * @hide 63 */ 64 @UnsupportedAppUsage 65 int mPollTech = NfcAdapter.FLAG_USE_ALL_TECH; 66 /** 67 * @hide 68 */ 69 @UnsupportedAppUsage 70 int mListenTech = NfcAdapter.FLAG_USE_ALL_TECH; 71 72 /** 73 * @hide 74 */ 75 @UnsupportedAppUsage 76 private final NfcActivityManager mNfcActivityManager; 77 78 /** 79 * @hide 80 */ NfcActivityState(Activity activity, NfcActivityManager activityManager)81 public NfcActivityState(Activity activity, NfcActivityManager activityManager) { 82 this.mNfcActivityManager = activityManager; 83 if (activity.isDestroyed()) { 84 throw new IllegalStateException("activity is already destroyed"); 85 } 86 // Check if activity is resumed right now, as we will not 87 // immediately get a callback for that. 88 resumed = activity.isResumed(); 89 90 this.activity = activity; 91 this.token = new Binder(); 92 mNfcActivityManager.registerApplication(activity.getApplication()); 93 } 94 95 /** 96 * @hide 97 */ destroy()98 public void destroy() { 99 mNfcActivityManager.unregisterApplication(activity.getApplication()); 100 resumed = false; 101 activity = null; 102 readerCallback = null; 103 readerModeFlags = 0; 104 readerModeExtras = null; 105 token = null; 106 107 mPollTech = NfcAdapter.FLAG_USE_ALL_TECH; 108 mListenTech = NfcAdapter.FLAG_USE_ALL_TECH; 109 } 110 111 /** 112 * @hide 113 */ 114 @Override toString()115 public String toString() { 116 StringBuilder s = new StringBuilder("["); 117 s.append(readerCallback); 118 s.append("]"); 119 return s.toString(); 120 } 121 } 122