1 /* 2 * Copyright 2021 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 com.android.libraries.testing.deviceshadower.internal.nfc; 18 19 import android.content.Intent; 20 import android.nfc.BeamShareData; 21 import android.nfc.IAppCallback; 22 import android.nfc.NdefMessage; 23 import android.nfc.NfcAdapter; 24 25 import com.android.libraries.testing.deviceshadower.Enums.NfcOperation; 26 import com.android.libraries.testing.deviceshadower.Nfclet; 27 import com.android.libraries.testing.deviceshadower.internal.DeviceShadowEnvironmentImpl; 28 import com.android.libraries.testing.deviceshadower.internal.common.Interrupter; 29 import com.android.libraries.testing.deviceshadower.internal.utils.Logger; 30 31 import javax.annotation.concurrent.GuardedBy; 32 33 /** 34 * Implementation of Nfclet. 35 */ 36 public class NfcletImpl implements Nfclet { 37 38 private static final Logger LOGGER = Logger.create("NfcletImpl"); 39 40 IAppCallback mAppCallback; 41 private final Interrupter mInterrupter; 42 43 @GuardedBy("this") 44 private int mCurrentState; 45 NfcletImpl()46 public NfcletImpl() { 47 mInterrupter = new Interrupter(); 48 mCurrentState = NfcAdapter.STATE_OFF; 49 } 50 onNear(NfcletImpl remote)51 public void onNear(NfcletImpl remote) { 52 if (remote.mAppCallback != null) { 53 LOGGER.v("NFC receiver get beam share data from remote"); 54 BeamShareData data = remote.mAppCallback.createBeamShareData(); 55 DeviceShadowEnvironmentImpl.getLocalDeviceletImpl().getBroadcastManager() 56 .sendBroadcast(createNdefDiscoveredIntent(data), null); 57 } 58 if (mAppCallback != null) { 59 LOGGER.v("NFC sender onNdefPushComplete"); 60 mAppCallback.onNdefPushComplete(); 61 } 62 } 63 getState()64 public synchronized int getState() { 65 return mCurrentState; 66 } 67 enable()68 public boolean enable() { 69 if (shouldInterrupt(NfcOperation.ENABLE)) { 70 return false; 71 } 72 LOGGER.v("Enable NFC Adapter"); 73 updateState(NfcAdapter.STATE_TURNING_ON); 74 updateState(NfcAdapter.STATE_ON); 75 return true; 76 } 77 disable()78 public boolean disable() { 79 if (shouldInterrupt(NfcOperation.DISABLE)) { 80 return false; 81 } 82 LOGGER.v("Disable NFC Adapter"); 83 updateState(NfcAdapter.STATE_TURNING_OFF); 84 updateState(NfcAdapter.STATE_OFF); 85 return true; 86 } 87 88 @Override setInitialState(int state)89 public synchronized Nfclet setInitialState(int state) { 90 mCurrentState = state; 91 return this; 92 } 93 94 @Override setInterruptOperation(NfcOperation operation)95 public Nfclet setInterruptOperation(NfcOperation operation) { 96 mInterrupter.addInterruptOperation(operation); 97 return this; 98 } 99 shouldInterrupt(NfcOperation operation)100 public boolean shouldInterrupt(NfcOperation operation) { 101 return mInterrupter.shouldInterrupt(operation); 102 } 103 updateState(int state)104 private synchronized void updateState(int state) { 105 if (mCurrentState != state) { 106 mCurrentState = state; 107 DeviceShadowEnvironmentImpl.getLocalDeviceletImpl().getBroadcastManager() 108 .sendBroadcast(createAdapterStateChangedIntent(state), null); 109 } 110 } 111 createAdapterStateChangedIntent(int state)112 private Intent createAdapterStateChangedIntent(int state) { 113 Intent intent = new Intent(NfcAdapter.ACTION_ADAPTER_STATE_CHANGED); 114 intent.putExtra(NfcAdapter.EXTRA_ADAPTER_STATE, state); 115 return intent; 116 } 117 createNdefDiscoveredIntent(BeamShareData data)118 private Intent createNdefDiscoveredIntent(BeamShareData data) { 119 Intent intent = new Intent(); 120 intent.setAction(NfcAdapter.ACTION_NDEF_DISCOVERED); 121 intent.putExtra(NfcAdapter.EXTRA_NDEF_MESSAGES, new NdefMessage[]{data.ndefMessage}); 122 // TODO(b/200231384): uncomment when uri and mime type implemented. 123 // ndefUri = message.getRecords()[0].toUri(); 124 // ndefMimeType = message.getRecords()[0].toMimeType(); 125 return intent; 126 } 127 } 128