1 /* 2 * Copyright (C) 2024 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 package com.android.nfc.emulator; 17 18 import android.content.ComponentName; 19 import android.nfc.cardemulation.CardEmulation; 20 import android.os.Bundle; 21 22 import android.util.Log; 23 import androidx.annotation.NonNull; 24 import com.android.nfc.utils.HceUtils; 25 import com.android.nfc.service.TransportService1; 26 27 import java.util.ArrayList; 28 import java.util.Arrays; 29 import java.util.List; 30 31 public class EventListenerEmulatorActivity extends BaseEmulatorActivity { 32 33 private ArrayList<Boolean> mFieldChanged = new ArrayList<>(); 34 private ArrayList<String> mAidNotRouted = new ArrayList<>(); 35 36 private CardEmulation.NfcEventListener mEventListener = new CardEmulation.NfcEventListener() { 37 @Override 38 public void onRemoteFieldChanged(boolean isDetected) { 39 mFieldChanged.add(isDetected); 40 } 41 42 public void onAidNotRouted(@NonNull String aid) { 43 mAidNotRouted.add(aid); 44 } 45 }; 46 47 @Override onCreate(Bundle savedInstanceState)48 protected void onCreate(Bundle savedInstanceState) { 49 super.onCreate(savedInstanceState); 50 51 setupServices(TransportService1.COMPONENT); 52 } 53 onResume()54 public void onResume() { 55 super.onResume(); 56 ComponentName serviceName = 57 new ComponentName(this.getApplicationContext(), TransportService1.class); 58 mCardEmulation.setPreferredService(this, serviceName); 59 waitForPreferredService(); 60 61 mFieldChanged.clear(); 62 mAidNotRouted.clear(); 63 registerEventListener(mEventListener); 64 } 65 66 @Override onPause()67 public void onPause() { 68 super.onPause(); 69 70 mCardEmulation.unsetPreferredService(this); 71 } 72 73 @Override onApduSequenceComplete(ComponentName component, long duration)74 public void onApduSequenceComplete(ComponentName component, long duration) { 75 boolean success = mAidNotRouted.equals(Arrays.asList(HceUtils.ACCESS_AID)); 76 success = success && TransportService1.COMPONENT.equals(component); 77 success = success && mFieldChanged.equals(Arrays.asList(true, false, true)); 78 79 if (success) { 80 setTestPassed(); 81 } 82 } 83 84 @Override onDestroy()85 protected void onDestroy() { 86 super.onDestroy(); 87 mCardEmulation.unregisterNfcEventListener(mEventListener); 88 } 89 90 @Override getPreferredServiceComponent()91 public ComponentName getPreferredServiceComponent(){ 92 return TransportService1.COMPONENT; 93 } 94 } 95