Skip to content

Commit

Permalink
Improve HCE controls
Browse files Browse the repository at this point in the history
  • Loading branch information
skjolber committed Apr 10, 2024
1 parent 7dbef6d commit 8930406
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 66 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,15 @@ protected boolean canStop() {

@Override
protected void stopImpl() {
cardEmulation.unsetPreferredService(activity);
if(cardEmulation != null) {
cardEmulation.unsetPreferredService(activity);
}
}

@Override
protected void startImpl() {
cardEmulation.setPreferredService(activity, service);
if(cardEmulation != null) {
cardEmulation.setPreferredService(activity, service);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import android.content.Context;
import android.content.pm.PackageManager;
import android.nfc.NfcAdapter;
import android.nfc.cardemulation.CardEmulation;

/**
*
Expand All @@ -16,31 +17,36 @@ public class HostCardEmulationService {
protected final Context context;
protected final ComponentName service;

protected final boolean enforceService;
protected final String aid;
protected final CardEmulation cardEmulation;

public HostCardEmulationService(Context context, String serviceClass, String aid) {
public HostCardEmulationService(Context context, Class serviceClass, String aid) {
this.context = context;
this.aid = aid;

NfcAdapter defaultAdapter = NfcAdapter.getDefaultAdapter(context);
if (defaultAdapter != null) {

// enable / disable regardless of whether we are the default service
this.service = new ComponentName(context, serviceClass);
this.enforceService = true;
this.cardEmulation = CardEmulation.getInstance(defaultAdapter);
} else {
this.service = null;
this.enforceService = false;
this.cardEmulation = null;
}
}

public boolean isCardEmulation() {
return cardEmulation != null;
}

public void enable() {
if (enforceService) {
if (isCardEmulation()) {
setMode(PackageManager.COMPONENT_ENABLED_STATE_ENABLED);
}
}

public void disable() {
if (enforceService) {
if (isCardEmulation()) {
setMode(PackageManager.COMPONENT_ENABLED_STATE_DISABLED);
}
}
Expand All @@ -49,4 +55,26 @@ protected void setMode(int mode) {
PackageManager pm = context.getPackageManager();
pm.setComponentEnabledSetting(service, mode, PackageManager.DONT_KILL_APP);
}

public String getAid() {
return aid;
}

public void setEnabled(boolean enabled) {
if (enabled) {
enable();
} else {
disable();
}
}

public boolean isEnabled() {
PackageManager pm = context.getPackageManager();
return pm.getComponentEnabledSetting(service) == PackageManager.COMPONENT_ENABLED_STATE_ENABLED;
}

public boolean isDefault() {
return cardEmulation.isDefaultServiceForAid(service, aid);
}

}

This file was deleted.

0 comments on commit 8930406

Please sign in to comment.