Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.sifb.mw.prod.hce;
- import android.annotation.TargetApi;
- import android.app.Activity;
- import android.content.Context;
- import android.content.Intent;
- import android.nfc.cardemulation.HostApduService;
- import android.os.Build;
- import android.os.Bundle;
- import android.os.Vibrator;
- import android.support.v4.content.IntentCompat;
- import com.sifb.base.MwApp;
- import com.sifb.mw.prod.hce.util.MyAPDUUtil;
- import com.sifb.mw.prod.hce.util.MyFileUtil;
- import com.sifb.mw.prod.hce.util.MyFormatUtil;
- import com.sifb.mw.prod.hce.util.MyLogsUtil;
- import com.sifb.mw.prod.hce.util.MySprefUtil;
- import com.sifb.sdk.util.MyTime;
- import java.util.Arrays;
- /**
- * Created by Yee Lynn on 4/3/2016.
- */
- @TargetApi(Build.VERSION_CODES.KITKAT)
- public class HCEHostApduService extends HostApduService {
- private static final String TAG = HCEConstant.HCE_TAG + "/HCEHostApduService";
- private String hceAmt, hceMerchant;
- private boolean isPayButton = false;
- //Application
- private MwApp curApplication;
- public void onCreate() {
- super.onCreate();
- MyLogsUtil.loggi(TAG, "onCreate");
- HCEConstant.hceTime = MyTime.getCurrentTimeStamp("yyMMddHHmmss");
- MyFileUtil.saveLogToExt(TAG, "onCreate");
- curApplication = (MwApp) getApplication();
- isPayButton = MySprefUtil.getHCEPrefBool(this, HCEConstant.HCE_SPREF_PAYBUTTON);
- MyLogsUtil.loggi(TAG, "isPayButton: " + isPayButton);
- }
- @Override
- public byte[] processCommandApdu(byte[] commandApdu, Bundle bundle) {
- isPayButton = MySprefUtil.getHCEPrefBool(this, HCEConstant.HCE_SPREF_PAYBUTTON);
- byte[] responseApdu = MyAPDUUtil.ERROR_UNKNOWN_RESP;
- String inboundApduDescription = "Start HCE";
- boolean isValid = false;
- switch (curApplication.getHCEActivityType()) {
- case NO_ACT:
- MyLogsUtil.loggi(TAG, "NO_ACT: " + MyFormatUtil.bytearrtoHexString(commandApdu));
- MyFileUtil.saveLogToExt(TAG, "NO_ACT: " + MyFormatUtil.bytearrtoHexString(commandApdu));
- if (Arrays.equals(HCEResponse.AID_SEL, commandApdu)) {
- inboundApduDescription = "AID Selected";
- responseApdu = HCEResponse.AID_RESP;
- isValid = true;
- } else {
- inboundApduDescription = "Invalid";
- responseApdu = MyAPDUUtil.ERROR_UNKNOWN_RESP;
- }
- MyLogsUtil.loggi(TAG, "NO_ACT inboundApduDescription: " + inboundApduDescription);
- MyFileUtil.saveLogToExt(TAG, "NO_ACT inboundApduDescription: " + inboundApduDescription);
- MyFileUtil.saveLogToExt(TAG, "NO_ACT responseApdu: " + MyFormatUtil.bytearrtoHexString(responseApdu));
- if (isValid) {
- MyLogsUtil.loggi(TAG, "Validated app!");
- cardsPage();
- }
- break;
- case PAY_ACT:
- MyLogsUtil.loggi(TAG, "PAY_ACT: " + MyFormatUtil.bytearrtoHexString(commandApdu));
- MyFileUtil.saveLogToExt(TAG, "PAY_ACT: " + MyFormatUtil.bytearrtoHexString(commandApdu));
- if (isPayButton) {
- if (Arrays.equals(HCEResponse.AID_SEL, commandApdu)) {
- inboundApduDescription = "PAY Button Selected";
- responseApdu = HCEResponse.AID_RESP_PAYBTN;
- isValid = true;
- } else {
- inboundApduDescription = "Invalid";
- responseApdu = MyAPDUUtil.ERROR_UNKNOWN_RESP;
- }
- } else {
- if (Arrays.equals(HCEResponse.AID_PAY, commandApdu)) {
- inboundApduDescription = "PAY selected";
- responseApdu = HCEResponse.AID_RESP;
- isValid = true;
- // } else if (validateAmount(commandApdu)) {
- // inboundApduDescription = "Valid Amount";
- // responseApdu = HCEResponse.AMT_RESP;
- // isValid = true;
- } else {
- inboundApduDescription = "Invalid";
- responseApdu = MyAPDUUtil.ERROR_UNKNOWN_RESP;
- }
- }
- MyLogsUtil.loggi(TAG, "PAY_ACT inboundApduDescription: " + inboundApduDescription);
- MyFileUtil.saveLogToExt(TAG, "PAY_ACT inboundApduDescription: " + inboundApduDescription);
- MyFileUtil.saveLogToExt(TAG, "PAY_ACT responseApdu: " + MyFormatUtil.bytearrtoHexString(responseApdu));
- if (isValid) {
- MyLogsUtil.loggi(TAG, "Confirmed payment!");
- confirmPage();
- }
- break;
- default:
- break;
- }
- MyLogsUtil.loggi(TAG, inboundApduDescription);
- MyFileUtil.saveLogToExt(TAG, "processCommandApdu inboundApduDescription: " + inboundApduDescription);
- return responseApdu;
- }
- // private boolean validateAmount(byte[] commandApdu) {
- // boolean isValid = false;
- //
- // String commandStr = MyFormatUtil.bytearrtoHexString(commandApdu);
- //
- // if (commandStr.length() >= 12) {
- // String valid = commandStr.substring(0, 12);
- //
- // if (valid.equals("414d4f554e54")) {
- // String amount = commandStr.substring(12, 36);
- // String merchant = commandStr.substring(36);
- // hceAmt = MyKeypadUtil.formatAmountSelections(MyKeypadUtil.FLAG_FORMAT_COMMADECIMAL, amount);
- // hceMerchant = MyFormatUtil.hexstrToString(merchant);
- //
- // MyLogsUtil.loggi(TAG, "Amount: " + hceAmt);
- // MyLogsUtil.loggi(TAG, "Merchant: " + hceMerchant);
- // MyFileUtil.saveLogToExt(TAG, "Amount: " + hceAmt);
- // MyFileUtil.saveLogToExt(TAG, "Merchant: " + hceMerchant);
- // isValid = true;
- // }
- // }
- //
- // return isValid;
- // }
- private Activity getHCEActivity() {
- return curApplication.getHCEActivity();
- }
- private boolean isHCEActExist() {
- if (getHCEActivity() != null) {
- return true;
- } else {
- return false;
- }
- }
- private void cardsPage() {
- HCEConstant.isStart = true;
- if (isHCEActExist()) {
- MyLogsUtil.loggi(TAG, "cardsPage isHCEActExist true");
- Activity iAct = getHCEActivity();
- Intent intentCards = new Intent(iAct, ActivityHCECards.class);
- startActivity(intentCards);
- iAct.finish();
- } else {
- MyLogsUtil.loggi(TAG, "cardsPage isHCEActExist false");
- Intent intentCards = new Intent(this, ActivityHCECards.class);
- intentCards.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
- intentCards.setFlags(IntentCompat.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
- // TODO: fix bg launch, act x started intentCards.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
- startActivity(intentCards);
- }
- }
- private void confirmPage() {
- Vibrator v = (Vibrator) curApplication.getSystemService(Context.VIBRATOR_SERVICE);
- v.vibrate(500);
- MySprefUtil.removeHCEPref(this, HCEConstant.HCE_SPREF_PAYBUTTON);
- if (isHCEActExist()) {
- MyLogsUtil.loggi(TAG, "confirmPage isHCEActExist true");
- ActivityHCEPayment.startAct(getHCEActivity(), hceAmt, hceMerchant);
- } else {
- MyLogsUtil.loggi(TAG, "confirmPage isHCEActExist false");
- Intent intentConfirm = new Intent(this, ActivityHCEConfirm.class);
- Bundle bundle = new Bundle();
- bundle.putString(HCEConstant.HCE_BUND_AMT, hceAmt);
- bundle.putString(HCEConstant.HCE_BUND_MERCHANT, hceMerchant);
- intentConfirm.putExtras(bundle);
- intentConfirm.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
- startActivity(intentConfirm);
- }
- }
- @Override
- public void onDeactivated(int reason) {
- MyLogsUtil.loggi(TAG, "onDeactivated(" + String.valueOf(reason) + ")");
- MyFileUtil.saveLogToExt(TAG, "onDeactivated");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement