summaryrefslogtreecommitdiffstats
path: root/src/com/android/nfc
diff options
context:
space:
mode:
Diffstat (limited to 'src/com/android/nfc')
-rw-r--r--src/com/android/nfc/BeamShareActivity.java9
-rwxr-xr-xsrc/com/android/nfc/NfcService.java111
-rw-r--r--src/com/android/nfc/beam/BeamTransferManager.java3
-rw-r--r--src/com/android/nfc/cardemulation/CardEmulationManager.java10
-rw-r--r--src/com/android/nfc/handover/HandoverDataParser.java9
5 files changed, 99 insertions, 43 deletions
diff --git a/src/com/android/nfc/BeamShareActivity.java b/src/com/android/nfc/BeamShareActivity.java
index 76629b88..5b8acacd 100644
--- a/src/com/android/nfc/BeamShareActivity.java
+++ b/src/com/android/nfc/BeamShareActivity.java
@@ -76,6 +76,15 @@ public class BeamShareActivity extends Activity {
}
}
+ @Override
+ protected void onDestroy() {
+ try {
+ unregisterReceiver(mReceiver);
+ } catch (Exception e) {
+ Log.w(TAG, e.getMessage());
+ }
+ super.onDestroy();
+ }
private void showNfcDialogAndExit(int msgId) {
IntentFilter filter = new IntentFilter(NfcAdapter.ACTION_ADAPTER_STATE_CHANGED);
diff --git a/src/com/android/nfc/NfcService.java b/src/com/android/nfc/NfcService.java
index 90a9bb2f..ab50f23a 100755
--- a/src/com/android/nfc/NfcService.java
+++ b/src/com/android/nfc/NfcService.java
@@ -73,6 +73,7 @@ import android.os.UserManager;
import android.provider.Settings;
import android.util.Log;
+import com.android.internal.logging.MetricsLogger;
import com.android.nfc.DeviceHost.DeviceHostListener;
import com.android.nfc.DeviceHost.LlcpConnectionlessSocket;
import com.android.nfc.DeviceHost.LlcpServerSocket;
@@ -86,6 +87,7 @@ import com.android.nfc.handover.HandoverDataParser;
import java.io.FileDescriptor;
import java.io.PrintWriter;
import java.nio.ByteBuffer;
+import java.util.concurrent.atomic.AtomicInteger;
import java.util.Arrays;
import java.util.ArrayList;
import java.util.HashMap;
@@ -109,6 +111,10 @@ public class NfcService implements DeviceHostListener {
static final String PREF_FIRST_BEAM = "first_beam";
static final String PREF_FIRST_BOOT = "first_boot";
+ static final String TRON_NFC_CE = "nfc_ce";
+ static final String TRON_NFC_P2P = "nfc_p2p";
+ static final String TRON_NFC_TAG = "nfc_tag";
+
static final int MSG_NDEF_TAG = 0;
static final int MSG_LLCP_LINK_ACTIVATION = 1;
static final int MSG_LLCP_LINK_DEACTIVATED = 2;
@@ -124,7 +130,10 @@ public class NfcService implements DeviceHostListener {
static final int MSG_REGISTER_T3T_IDENTIFIER = 12;
static final int MSG_DEREGISTER_T3T_IDENTIFIER = 13;
static final int MSG_TAG_DEBOUNCE = 14;
+ static final int MSG_UPDATE_STATS = 15;
+ // Update stats every 4 hours
+ static final long STATS_UPDATE_INTERVAL_MS = 4 * 60 * 60 * 1000;
static final long MAX_POLLING_PAUSE_TIMEOUT = 40000;
static final int TASK_ENABLE = 1;
@@ -207,6 +216,14 @@ public class NfcService implements DeviceHostListener {
int mDebounceTagDebounceMs;
ITagRemovedCallback mDebounceTagRemovedCallback;
+ // Only accessed on one thread so doesn't need locking
+ NdefMessage mLastReadNdefMessage;
+
+ // Metrics
+ AtomicInteger mNumTagsDetected;
+ AtomicInteger mNumP2pDetected;
+ AtomicInteger mNumHceDetected;
+
// mState is protected by this, however it is only modified in onCreate()
// and the default AsyncTask thread so it is read unprotected from that
// thread
@@ -270,6 +287,8 @@ public class NfcService implements DeviceHostListener {
@Override
public void onHostCardEmulationDeactivated(int technology) {
if (mCardEmulationManager != null) {
+ // Do metrics here so we don't slow the CE path down
+ mNumHceDetected.incrementAndGet();
mCardEmulationManager.onHostCardEmulationDeactivated(technology);
}
}
@@ -295,6 +314,7 @@ public class NfcService implements DeviceHostListener {
*/
@Override
public void onLlcpFirstPacketReceived(NfcDepEndpoint device) {
+ mNumP2pDetected.incrementAndGet();
sendMessage(NfcService.MSG_LLCP_LINK_FIRST_PACKET, device);
}
@@ -370,6 +390,10 @@ public class NfcService implements DeviceHostListener {
mScreenState = mScreenStateHelper.checkScreenState();
+ mNumTagsDetected = new AtomicInteger();
+ mNumP2pDetected = new AtomicInteger();
+ mNumHceDetected = new AtomicInteger();
+
// Intents for all users
IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_OFF);
filter.addAction(Intent.ACTION_SCREEN_ON);
@@ -407,6 +431,8 @@ public class NfcService implements DeviceHostListener {
ServiceManager.addService(SERVICE_NAME, mNfcAdapter);
new EnableDisableTask().execute(TASK_BOOT); // do blocking boot tasks
+
+ mHandler.sendEmptyMessageDelayed(MSG_UPDATE_STATS, STATS_UPDATE_INTERVAL_MS);
}
void initSoundPool() {
@@ -1800,6 +1826,7 @@ public class NfcService implements DeviceHostListener {
case MSG_NDEF_TAG:
if (DBG) Log.d(TAG, "Tag detected, notifying applications");
+ mNumTagsDetected.incrementAndGet();
TagEndpoint tag = (TagEndpoint) msg.obj;
byte[] debounceTagUid;
int debounceTagMs;
@@ -1809,26 +1836,6 @@ public class NfcService implements DeviceHostListener {
debounceTagMs = mDebounceTagDebounceMs;
debounceTagRemovedCallback = mDebounceTagRemovedCallback;
}
- if (debounceTagUid != null) {
- if (Arrays.equals(debounceTagUid, tag.getUid())) {
- // Still ignoring this tag...poll again and reset debounce timer
- mHandler.removeMessages(MSG_TAG_DEBOUNCE);
- mHandler.sendEmptyMessageDelayed(MSG_TAG_DEBOUNCE, debounceTagMs);
- tag.disconnect();
- return;
- } else {
- synchronized (NfcService.this) {
- mDebounceTagUid = null;
- }
- if (debounceTagRemovedCallback != null) {
- try {
- debounceTagRemovedCallback.onTagRemoved();
- } catch (RemoteException e) {
- // Ignore
- }
- }
- }
- }
ReaderModeParams readerParams = null;
int presenceCheckDelay = DEFAULT_PRESENCE_CHECK_DELAY;
DeviceHost.TagDisconnectedCallback callback =
@@ -1851,11 +1858,6 @@ public class NfcService implements DeviceHostListener {
}
}
- boolean playSound = readerParams == null ||
- (readerParams.flags & NfcAdapter.FLAG_READER_NO_PLATFORM_SOUNDS) == 0;
- if (mScreenState == ScreenStateHelper.SCREEN_STATE_ON_UNLOCKED && playSound) {
- playSound(SOUND_START);
- }
if (tag.getConnectedTechnology() == TagTechnology.NFC_BARCODE) {
// When these tags start containing NDEF, they will require
// the stack to deal with them in a different way, since
@@ -1868,18 +1870,42 @@ public class NfcService implements DeviceHostListener {
}
NdefMessage ndefMsg = tag.findAndReadNdef();
- if (ndefMsg != null) {
- tag.startPresenceChecking(presenceCheckDelay, callback);
- dispatchTagEndpoint(tag, readerParams);
- } else {
- if (tag.reconnect()) {
- tag.startPresenceChecking(presenceCheckDelay, callback);
- dispatchTagEndpoint(tag, readerParams);
- } else {
+ if (ndefMsg == null) {
+ // First try to see if this was a bad tag read
+ if (!tag.reconnect()) {
+ tag.disconnect();
+ break;
+ }
+ }
+
+ if (debounceTagUid != null) {
+ // If we're debouncing and the UID or the NDEF message of the tag match,
+ // don't dispatch but drop it.
+ if (Arrays.equals(debounceTagUid, tag.getUid()) ||
+ (ndefMsg != null && ndefMsg.equals(mLastReadNdefMessage))) {
+ mHandler.removeMessages(MSG_TAG_DEBOUNCE);
+ mHandler.sendEmptyMessageDelayed(MSG_TAG_DEBOUNCE, debounceTagMs);
tag.disconnect();
- playSound(SOUND_ERROR);
+ return;
+ } else {
+ synchronized (NfcService.this) {
+ mDebounceTagUid = null;
+ mDebounceTagRemovedCallback = null;
+ }
+ if (debounceTagRemovedCallback != null) {
+ try {
+ debounceTagRemovedCallback.onTagRemoved();
+ } catch (RemoteException e) {
+ // Ignore
+ }
+ }
}
}
+
+ mLastReadNdefMessage = ndefMsg;
+
+ tag.startPresenceChecking(presenceCheckDelay, callback);
+ dispatchTagEndpoint(tag, readerParams);
break;
case MSG_LLCP_LINK_ACTIVATION:
if (mIsDebugBuild) {
@@ -1936,6 +1962,7 @@ public class NfcService implements DeviceHostListener {
synchronized (NfcService.this) {
mDebounceTagUid = null;
tagRemovedCallback = mDebounceTagRemovedCallback;
+ mDebounceTagRemovedCallback = null;
}
if (tagRemovedCallback != null) {
try {
@@ -1945,6 +1972,22 @@ public class NfcService implements DeviceHostListener {
}
}
break;
+ case MSG_UPDATE_STATS:
+ if (mNumTagsDetected.get() > 0) {
+ MetricsLogger.count(mContext, TRON_NFC_TAG, mNumTagsDetected.get());
+ mNumTagsDetected.set(0);
+ }
+ if (mNumHceDetected.get() > 0) {
+ MetricsLogger.count(mContext, TRON_NFC_CE, mNumHceDetected.get());
+ mNumHceDetected.set(0);
+ }
+ if (mNumP2pDetected.get() > 0) {
+ MetricsLogger.count(mContext, TRON_NFC_P2P, mNumP2pDetected.get());
+ mNumP2pDetected.set(0);
+ }
+ removeMessages(MSG_UPDATE_STATS);
+ sendEmptyMessageDelayed(MSG_UPDATE_STATS, STATS_UPDATE_INTERVAL_MS);
+ break;
default:
Log.e(TAG, "Unknown message received");
break;
diff --git a/src/com/android/nfc/beam/BeamTransferManager.java b/src/com/android/nfc/beam/BeamTransferManager.java
index a082d8fd..e05c223d 100644
--- a/src/com/android/nfc/beam/BeamTransferManager.java
+++ b/src/com/android/nfc/beam/BeamTransferManager.java
@@ -251,7 +251,8 @@ public class BeamTransferManager implements Handler.Callback,
}
public boolean isRunning() {
- if (mState != STATE_NEW && mState != STATE_IN_PROGRESS && mState != STATE_W4_NEXT_TRANSFER) {
+ if (mState != STATE_NEW && mState != STATE_IN_PROGRESS && mState != STATE_W4_NEXT_TRANSFER
+ && mState != STATE_CANCELLING) {
return false;
} else {
return true;
diff --git a/src/com/android/nfc/cardemulation/CardEmulationManager.java b/src/com/android/nfc/cardemulation/CardEmulationManager.java
index 73ed881a..5f1ff5b8 100644
--- a/src/com/android/nfc/cardemulation/CardEmulationManager.java
+++ b/src/com/android/nfc/cardemulation/CardEmulationManager.java
@@ -32,6 +32,8 @@ import android.nfc.cardemulation.NfcFCardEmulation;
import android.os.Binder;
import android.os.RemoteException;
import android.os.UserHandle;
+import android.os.PowerManager;
+import android.os.SystemClock;
import android.provider.Settings;
import android.util.Log;
@@ -75,6 +77,7 @@ public class CardEmulationManager implements RegisteredServicesCache.Callback,
final Context mContext;
final CardEmulationInterface mCardEmulationInterface;
final NfcFCardEmulationInterface mNfcFCardEmulationInterface;
+ final PowerManager mPowerManager;
public CardEmulationManager(Context context) {
mContext = context;
@@ -91,6 +94,7 @@ public class CardEmulationManager implements RegisteredServicesCache.Callback,
context, mNfcFServicesCache, mT3tIdentifiersCache, this);
mServiceCache.initialize();
mNfcFServicesCache.initialize();
+ mPowerManager = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
}
public INfcCardEmulation getNfcCardEmulationInterface() {
@@ -103,6 +107,9 @@ public class CardEmulationManager implements RegisteredServicesCache.Callback,
public void onHostCardEmulationActivated(int technology) {
+ if (mPowerManager != null) {
+ mPowerManager.userActivity(SystemClock.uptimeMillis(), PowerManager.USER_ACTIVITY_EVENT_TOUCH, 0);
+ }
if (technology == NFC_HCE_APDU) {
mHostEmulationManager.onHostEmulationActivated();
mPreferredServices.onHostEmulationActivated();
@@ -114,6 +121,9 @@ public class CardEmulationManager implements RegisteredServicesCache.Callback,
}
public void onHostCardEmulationData(int technology, byte[] data) {
+ if (mPowerManager != null) {
+ mPowerManager.userActivity(SystemClock.uptimeMillis(), PowerManager.USER_ACTIVITY_EVENT_TOUCH, 0);
+ }
if (technology == NFC_HCE_APDU) {
mHostEmulationManager.onHostEmulationData(data);
} else if (technology == NFC_HCE_NFCF) {
diff --git a/src/com/android/nfc/handover/HandoverDataParser.java b/src/com/android/nfc/handover/HandoverDataParser.java
index eb77aaad..2921c679 100644
--- a/src/com/android/nfc/handover/HandoverDataParser.java
+++ b/src/com/android/nfc/handover/HandoverDataParser.java
@@ -443,15 +443,8 @@ public class HandoverDataParser {
break;
}
- byte[] reversedTK = new byte[len - 1];
- payload.get(reversedTK);
-
byte[] securityManagerTK = new byte[len - 1];
-
- //TK in AD is in reverse order
- for (int i = 0; i < reversedTK.length; i++) {
- securityManagerTK[i] = reversedTK[securityManagerTK.length - 1 - i];
- }
+ payload.get(securityManagerTK);
result.oobData = new OobData();
result.oobData.setSecurityManagerTk(securityManagerTK);