Skip to content

Commit

Permalink
Delete wrapper resources, add library project
Browse files Browse the repository at this point in the history
  • Loading branch information
skjolber committed Aug 2, 2020
1 parent 4bb7c82 commit 93f3297
Show file tree
Hide file tree
Showing 78 changed files with 324 additions and 5,876 deletions.
9 changes: 8 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,14 @@ buildscript {
repositories {
google()
jcenter()

maven {
url "https://oss.sonatype.org/content/repositories/snapshots/"
}
}

dependencies {
classpath 'com.android.tools.build:gradle:3.6.1'
classpath 'com.android.tools.build:gradle:4.0.1'
}
}

Expand Down Expand Up @@ -37,6 +41,9 @@ configure(buildProjects()) {
repositories {
google()
jcenter()
maven {
url "https://oss.sonatype.org/content/repositories/snapshots/"
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@

import org.nfctools.NfcException;

public class IsdoDepAdapter extends DefaultTechnology implements CommandTechnology {
public class IsoDepAdapter extends DefaultTechnology implements CommandTechnology {

protected static final String TAG = IsdoDepAdapter.class.getName();
protected static final String TAG = IsoDepAdapter.class.getName();

private DESFireAdapter adapter;
private boolean hostCardEmulation;

public IsdoDepAdapter(int slotNumber, IsoDepWrapper isoDep, boolean hostCardEmulation) {
public IsoDepAdapter(int slotNumber, IsoDepWrapper isoDep, boolean hostCardEmulation) {
super(TagTechnology.ISO_DEP, slotNumber);
this.adapter = new DESFireAdapter(isoDep, false);
this.hostCardEmulation = hostCardEmulation;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,6 @@ public abstract class AbstractBackgroundUsbService extends AbstractService {

private static final String TAG = AbstractBackgroundUsbService.class.getName();

private static final String DESCRIPTOR = "android.nfc.INfcTag";
private static final String CLASS = "android.nfc.INfcTag";


private static class Scanner extends Handler {

private static final long USB_RESCAN_INTERVAL_STANDARD = 1000;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,12 @@
import android.util.Log;

import com.acs.smartcard.ReaderException;
import com.github.skjolber.nfc.command.ACRCommands;
import com.github.skjolber.nfc.hce.INFcTagBinder;
import com.github.skjolber.nfc.hce.resolve.TagProxyStore;
import com.github.skjolber.nfc.hce.tech.TagTechnology;
import com.github.skjolber.nfc.hce.tech.mifare.MifareClassicAdapter;
import com.github.skjolber.nfc.hce.tech.mifare.MifareClassicTagFactory;
import com.github.skjolber.nfc.hce.tech.mifare.IsdoDepAdapter;
import com.github.skjolber.nfc.hce.tech.mifare.IsoDepAdapter;
import com.github.skjolber.nfc.hce.tech.mifare.MifareDesfireTagFactory;
import com.github.skjolber.nfc.hce.tech.mifare.MifareUltralightAdapter;
import com.github.skjolber.nfc.hce.tech.mifare.MifareUltralightTagFactory;
Expand Down Expand Up @@ -63,8 +62,6 @@
import java.util.ArrayList;
import java.util.List;

import custom.java.CommandAPDU;


public abstract class AbstractService extends Service {

Expand Down Expand Up @@ -189,7 +186,7 @@ protected void hce(int slotNumber, byte[] atr, IsoDepWrapper wrapper) {
try {
List<TagTechnology> technologies = new ArrayList<TagTechnology>();
technologies.add(new NfcAAdapter(slotNumber, wrapper, true));
technologies.add(new IsdoDepAdapter(slotNumber, wrapper, true));
technologies.add(new IsoDepAdapter(slotNumber, wrapper, true));

int serviceHandle = store.add(slotNumber, technologies);

Expand Down Expand Up @@ -219,7 +216,7 @@ protected void desfire(int slotNumber, byte[] atr, IsoDepWrapper wrapper) {

List<TagTechnology> technologies = new ArrayList<TagTechnology>();
technologies.add(new NfcAAdapter(slotNumber, wrapper, false));
technologies.add(new IsdoDepAdapter(slotNumber, wrapper, false));
technologies.add(new IsoDepAdapter(slotNumber, wrapper, false));

int serviceHandle = store.add(slotNumber, technologies);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -682,6 +682,7 @@ private static String getCardStatusString(int cardStatus) {
return "The card status is unknown.";
}


/* Get the Error string. */
private static String getErrorString(int errorCode) {
if (errorCode == BluetoothReader.ERROR_SUCCESS) {
Expand Down Expand Up @@ -733,6 +734,7 @@ public static String getResponseString(byte[] response, int errorCode) {
return getErrorString(errorCode);
}


@Override
public void onDestroy() {
Log.i(TAG, "Service destroyed");
Expand Down
4 changes: 2 additions & 2 deletions examples/hostCardEmulationClient/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
android:theme="@style/AppTheme"
>
<activity
android:name=".EchoHostAdpuServiceActivity"
android:name=".HceActivity"
android:label="@string/appName"
android:configChanges="keyboardHidden|orientation|screenSize"
android:launchMode="singleInstance"
Expand All @@ -38,7 +38,7 @@

<meta-data
android:name="android.nfc.cardemulation.host_apdu_service"
android:resource="@xml/apduservice" />
android:resource="@xml/echoservice" />
</service>

<activity
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,8 @@
import java.io.ByteArrayOutputStream;
import java.io.IOException;

import android.content.ComponentName;
import android.content.Intent;
import android.content.SharedPreferences;
import android.nfc.NfcAdapter;
import android.nfc.cardemulation.CardEmulation;
import android.nfc.cardemulation.HostApduService;
import android.os.Bundle;
import android.preference.PreferenceManager;
Expand All @@ -16,6 +13,12 @@
import com.github.skjolber.nfc.util.Broadcast;
import com.github.skjolber.nfc.util.CommandAPDU;

/**
*
* Echo HCE service.
*
*/

public class EchoHostApduService extends HostApduService {

public final static int ISO_SELECT_APPLICATION = 0xA4;//
Expand All @@ -25,23 +28,12 @@ public class EchoHostApduService extends HostApduService {

private static String TAG = EchoHostApduService.class.getName();

protected static String AID = "F00A2B4C6D8E";

protected Broadcast broadcast = new Broadcast(this);

@Override
public void onCreate() {
super.onCreate();
Log.d(TAG, "HCE service created");

CardEmulation cardEmulation = CardEmulation.getInstance(NfcAdapter.getDefaultAdapter(this));

boolean defaultService = cardEmulation.isDefaultServiceForAid(new ComponentName(this, EchoHostApduService.class), AID);

if(!defaultService) {
throw new IllegalArgumentException("Expected default service for AID " + AID);
}
Log.d(TAG, "HCE service AID is " + AID);
Log.d(TAG, getClass().getName() + " HCE service created");
}

@Override
Expand All @@ -57,11 +49,13 @@ public byte[] processCommandApdu(byte[] request, Bundle extras) {
}
}

byte[] response = response(OPERATION_OK);
byte[] response = response(OPERATION_OK, request);
if(ins == ISO_SELECT_APPLICATION || ins == SELECT_APPLICATION) {
String application = toHexString(command.getData(), 1, 3);

Log.i(TAG, "Selected Application " + application);

response = response(OPERATION_OK, new byte[]{0x00, 0x01, 0x02});
} else {
Log.i(TAG, "Received unknown command " + String.format("0x%08X", ins));

Expand All @@ -79,7 +73,7 @@ public byte[] processCommandApdu(byte[] request, Bundle extras) {
}
}

Intent intent = new Intent(this, EchoHostAdpuServiceActivity.class);
Intent intent = new Intent(this, HceActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setAction(Broadcast.HOST_CARD_EMULATION_ACTION_PROCESS_COMMAND_ADPU);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.content.res.Configuration;
import android.net.Uri;
import android.nfc.NfcAdapter;
Expand All @@ -20,18 +21,30 @@

import com.github.skjolber.nfc.util.Broadcast;

import java.util.HashMap;
import java.util.Map;

/**
*
* Activity for this app to echo the HCE service using internal NFC.
*
*/


public class EchoHostAdpuServiceActivity extends DialogActivity {
public class HceActivity extends DialogActivity {

private static String TAG = HceActivity.class.getName();

private static String TAG = EchoHostAdpuServiceActivity.class.getName();
private static final Map<Class, Integer> services;

static {
services = new HashMap<>();
services.put(EchoHostApduService.class, R.xml.echoservice);
services.put(HelloHostApduService.class, R.xml.helloservice);
}

private boolean receiving = false;
private CardEmulation cardEmulation;

private final BroadcastReceiver hostCardEmulationBroadcastReceiver = new BroadcastReceiver() {

Expand Down Expand Up @@ -72,14 +85,17 @@ protected void onCreate(Bundle savedInstanceState) {

setContentView(R.layout.activity_main);

CardEmulation cardEmulation = CardEmulation.getInstance(NfcAdapter.getDefaultAdapter(this));
cardEmulation = CardEmulation.getInstance(NfcAdapter.getDefaultAdapter(this));

boolean defaultService = cardEmulation.isDefaultServiceForAid(new ComponentName(this, EchoHostApduService.class), EchoHostApduService.AID);
for (Map.Entry<Class, Integer> entry : services.entrySet()) {
String aid = Utils.parseAid(this, entry.getValue());

if (!defaultService) {
Log.w(TAG, "Expected default service for AID " + EchoHostApduService.AID);
if (!cardEmulation.isDefaultServiceForAid(new ComponentName(this, entry.getKey()), aid)) {
Log.d(TAG, "This application is NOT the preferred service for aid " + aid);
} else {
Log.d(TAG, "This application is the preferred service for aid " + aid);
}
}
Log.d(TAG, "Service AID is " + EchoHostApduService.AID);

enableBroadcast();

Expand Down Expand Up @@ -112,6 +128,36 @@ public void onClick(DialogInterface dialog, int which) {
show(dialog);
}

@Override
protected void onResume() {
super.onResume();

SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);

// note: this will only work for a single HCE service
for (Map.Entry<Class, Integer> entry : services.entrySet()) {
boolean preferred = prefs.getBoolean(PreferencesActivity.PREFERENCE_HOST_CARD_EMULATION_PREFERENCE + "_" + entry.getKey().getSimpleName(), true);

if(preferred) {
if (cardEmulation.setPreferredService(this, new ComponentName(this, entry.getKey()))) {
Log.d(TAG, "Set " + entry.getKey().getName() + " to be the preferred HCE service for this activity");
} else {
Log.w(TAG, "Unable to set " + entry.getKey().getName() + " to be the preferred HCE service for this activity");
}
}
}
}

@Override
protected void onPause() {
super.onPause();

if(cardEmulation.unsetPreferredService(this)) {
Log.d(TAG, "Unset preferred service for " + getClass().getName());
} else {
Log.d(TAG, "Unable to unset preferred service for " + getClass().getName());
}
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,16 @@ public void onTagDiscovered(Tag tag) {
// attempt to select demo HCE application using iso adpu
String isoApplicationString = prefs.getString(PreferencesActivity.PREFERENCE_HOST_CARD_EMULATION_ISO_APPLICATION_ID, null);
// clean whitespace
isoApplicationString = isoApplicationString.replaceAll("\\s","");
if(isoApplicationString != null) {
isoApplicationString = isoApplicationString.replaceAll("\\s", "");
if(isoApplicationString.isEmpty()) {
isoApplicationString = null;
}
}

if(isoApplicationString == null) {
isoApplicationString = Utils.parseAid(this, R.xml.echoservice);
}

byte[] key = hexStringToByteArray(isoApplicationString);

Expand Down
Loading

0 comments on commit 93f3297

Please sign in to comment.