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.Intent;
19 import android.nfc.cardemulation.PollingFrame;
20 import android.util.Log;
21 
22 import java.util.ArrayList;
23 import java.util.List;
24 
25 /**
26  * This activity is used to test the polling frame feature.
27  *
28  * <p>This activity will register a broadcast receiver to receive the polling frame information from
29  * the polling loop service.
30  */
31 public class PollingFrameEmulatorActivity extends PollingLoopEmulatorActivity {
32     private static final String TAG = "PollingFrameEmulatorActivity";
33     public static final String POLLING_FRAME_OFF_DETECTED =
34             PACKAGE_NAME + ".POLLING_FRAME_OFF_DETECTED";
35 
36     private List<PollingFrame> pollingFrames = new ArrayList<PollingFrame>();
37 
getPollingFrames()38     public PollingFrame[] getPollingFrames() {
39         PollingFrame[] result = pollingFrames.stream().toArray(PollingFrame[]::new);
40         this.pollingFrames = new ArrayList<PollingFrame>();
41         return result;
42     }
43 
44     @Override
processPollingFrames(List<PollingFrame> frames)45     void processPollingFrames(List<PollingFrame> frames) {
46         Log.d(TAG, "processPollingFrames of size " + frames.size());
47         pollingFrames.addAll(frames);
48 
49         if (frames.stream()
50                 .anyMatch(frame -> frame.getType() == PollingFrame.POLLING_LOOP_TYPE_OFF)) {
51             Intent intent = new Intent(POLLING_FRAME_OFF_DETECTED);
52             sendBroadcast(intent);
53         }
54     }
55 }
56