Advertisement
Guest User

Untitled

a guest
Jan 18th, 2017
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.31 KB | None | 0 0
  1. package com.sifb.mw.prod.hce;
  2.  
  3. import android.annotation.TargetApi;
  4. import android.app.Activity;
  5. import android.content.Context;
  6. import android.content.Intent;
  7. import android.nfc.cardemulation.HostApduService;
  8. import android.os.Build;
  9. import android.os.Bundle;
  10. import android.os.Vibrator;
  11. import android.support.v4.content.IntentCompat;
  12.  
  13. import com.sifb.base.MwApp;
  14. import com.sifb.mw.prod.hce.util.MyAPDUUtil;
  15. import com.sifb.mw.prod.hce.util.MyFileUtil;
  16. import com.sifb.mw.prod.hce.util.MyFormatUtil;
  17. import com.sifb.mw.prod.hce.util.MyLogsUtil;
  18. import com.sifb.mw.prod.hce.util.MySprefUtil;
  19. import com.sifb.sdk.util.MyTime;
  20.  
  21. import java.util.Arrays;
  22.  
  23. /**
  24. * Created by Yee Lynn on 4/3/2016.
  25. */
  26. @TargetApi(Build.VERSION_CODES.KITKAT)
  27. public class HCEHostApduService extends HostApduService {
  28.  
  29. private static final String TAG = HCEConstant.HCE_TAG + "/HCEHostApduService";
  30. private String hceAmt, hceMerchant;
  31. private boolean isPayButton = false;
  32.  
  33. //Application
  34. private MwApp curApplication;
  35.  
  36. public void onCreate() {
  37. super.onCreate();
  38. MyLogsUtil.loggi(TAG, "onCreate");
  39. HCEConstant.hceTime = MyTime.getCurrentTimeStamp("yyMMddHHmmss");
  40. MyFileUtil.saveLogToExt(TAG, "onCreate");
  41. curApplication = (MwApp) getApplication();
  42. isPayButton = MySprefUtil.getHCEPrefBool(this, HCEConstant.HCE_SPREF_PAYBUTTON);
  43. MyLogsUtil.loggi(TAG, "isPayButton: " + isPayButton);
  44. }
  45.  
  46. @Override
  47. public byte[] processCommandApdu(byte[] commandApdu, Bundle bundle) {
  48. isPayButton = MySprefUtil.getHCEPrefBool(this, HCEConstant.HCE_SPREF_PAYBUTTON);
  49. byte[] responseApdu = MyAPDUUtil.ERROR_UNKNOWN_RESP;
  50. String inboundApduDescription = "Start HCE";
  51. boolean isValid = false;
  52.  
  53. switch (curApplication.getHCEActivityType()) {
  54. case NO_ACT:
  55. MyLogsUtil.loggi(TAG, "NO_ACT: " + MyFormatUtil.bytearrtoHexString(commandApdu));
  56. MyFileUtil.saveLogToExt(TAG, "NO_ACT: " + MyFormatUtil.bytearrtoHexString(commandApdu));
  57.  
  58. if (Arrays.equals(HCEResponse.AID_SEL, commandApdu)) {
  59. inboundApduDescription = "AID Selected";
  60. responseApdu = HCEResponse.AID_RESP;
  61. isValid = true;
  62. } else {
  63. inboundApduDescription = "Invalid";
  64. responseApdu = MyAPDUUtil.ERROR_UNKNOWN_RESP;
  65. }
  66.  
  67. MyLogsUtil.loggi(TAG, "NO_ACT inboundApduDescription: " + inboundApduDescription);
  68. MyFileUtil.saveLogToExt(TAG, "NO_ACT inboundApduDescription: " + inboundApduDescription);
  69. MyFileUtil.saveLogToExt(TAG, "NO_ACT responseApdu: " + MyFormatUtil.bytearrtoHexString(responseApdu));
  70.  
  71. if (isValid) {
  72. MyLogsUtil.loggi(TAG, "Validated app!");
  73. cardsPage();
  74. }
  75. break;
  76. case PAY_ACT:
  77. MyLogsUtil.loggi(TAG, "PAY_ACT: " + MyFormatUtil.bytearrtoHexString(commandApdu));
  78. MyFileUtil.saveLogToExt(TAG, "PAY_ACT: " + MyFormatUtil.bytearrtoHexString(commandApdu));
  79.  
  80. if (isPayButton) {
  81. if (Arrays.equals(HCEResponse.AID_SEL, commandApdu)) {
  82. inboundApduDescription = "PAY Button Selected";
  83. responseApdu = HCEResponse.AID_RESP_PAYBTN;
  84. isValid = true;
  85. } else {
  86. inboundApduDescription = "Invalid";
  87. responseApdu = MyAPDUUtil.ERROR_UNKNOWN_RESP;
  88. }
  89. } else {
  90. if (Arrays.equals(HCEResponse.AID_PAY, commandApdu)) {
  91. inboundApduDescription = "PAY selected";
  92. responseApdu = HCEResponse.AID_RESP;
  93. isValid = true;
  94. // } else if (validateAmount(commandApdu)) {
  95. // inboundApduDescription = "Valid Amount";
  96. // responseApdu = HCEResponse.AMT_RESP;
  97. // isValid = true;
  98. } else {
  99. inboundApduDescription = "Invalid";
  100. responseApdu = MyAPDUUtil.ERROR_UNKNOWN_RESP;
  101. }
  102. }
  103. MyLogsUtil.loggi(TAG, "PAY_ACT inboundApduDescription: " + inboundApduDescription);
  104. MyFileUtil.saveLogToExt(TAG, "PAY_ACT inboundApduDescription: " + inboundApduDescription);
  105. MyFileUtil.saveLogToExt(TAG, "PAY_ACT responseApdu: " + MyFormatUtil.bytearrtoHexString(responseApdu));
  106.  
  107. if (isValid) {
  108. MyLogsUtil.loggi(TAG, "Confirmed payment!");
  109. confirmPage();
  110. }
  111. break;
  112. default:
  113. break;
  114. }
  115. MyLogsUtil.loggi(TAG, inboundApduDescription);
  116. MyFileUtil.saveLogToExt(TAG, "processCommandApdu inboundApduDescription: " + inboundApduDescription);
  117. return responseApdu;
  118. }
  119.  
  120. // private boolean validateAmount(byte[] commandApdu) {
  121. // boolean isValid = false;
  122. //
  123. // String commandStr = MyFormatUtil.bytearrtoHexString(commandApdu);
  124. //
  125. // if (commandStr.length() >= 12) {
  126. // String valid = commandStr.substring(0, 12);
  127. //
  128. // if (valid.equals("414d4f554e54")) {
  129. // String amount = commandStr.substring(12, 36);
  130. // String merchant = commandStr.substring(36);
  131. // hceAmt = MyKeypadUtil.formatAmountSelections(MyKeypadUtil.FLAG_FORMAT_COMMADECIMAL, amount);
  132. // hceMerchant = MyFormatUtil.hexstrToString(merchant);
  133. //
  134. // MyLogsUtil.loggi(TAG, "Amount: " + hceAmt);
  135. // MyLogsUtil.loggi(TAG, "Merchant: " + hceMerchant);
  136. // MyFileUtil.saveLogToExt(TAG, "Amount: " + hceAmt);
  137. // MyFileUtil.saveLogToExt(TAG, "Merchant: " + hceMerchant);
  138. // isValid = true;
  139. // }
  140. // }
  141. //
  142. // return isValid;
  143. // }
  144.  
  145. private Activity getHCEActivity() {
  146. return curApplication.getHCEActivity();
  147. }
  148.  
  149. private boolean isHCEActExist() {
  150. if (getHCEActivity() != null) {
  151. return true;
  152. } else {
  153. return false;
  154. }
  155. }
  156.  
  157. private void cardsPage() {
  158. HCEConstant.isStart = true;
  159. if (isHCEActExist()) {
  160. MyLogsUtil.loggi(TAG, "cardsPage isHCEActExist true");
  161. Activity iAct = getHCEActivity();
  162. Intent intentCards = new Intent(iAct, ActivityHCECards.class);
  163. startActivity(intentCards);
  164. iAct.finish();
  165. } else {
  166. MyLogsUtil.loggi(TAG, "cardsPage isHCEActExist false");
  167. Intent intentCards = new Intent(this, ActivityHCECards.class);
  168. intentCards.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
  169. intentCards.setFlags(IntentCompat.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
  170. // TODO: fix bg launch, act x started intentCards.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
  171. startActivity(intentCards);
  172. }
  173. }
  174.  
  175. private void confirmPage() {
  176. Vibrator v = (Vibrator) curApplication.getSystemService(Context.VIBRATOR_SERVICE);
  177. v.vibrate(500);
  178. MySprefUtil.removeHCEPref(this, HCEConstant.HCE_SPREF_PAYBUTTON);
  179. if (isHCEActExist()) {
  180. MyLogsUtil.loggi(TAG, "confirmPage isHCEActExist true");
  181. ActivityHCEPayment.startAct(getHCEActivity(), hceAmt, hceMerchant);
  182. } else {
  183. MyLogsUtil.loggi(TAG, "confirmPage isHCEActExist false");
  184. Intent intentConfirm = new Intent(this, ActivityHCEConfirm.class);
  185. Bundle bundle = new Bundle();
  186. bundle.putString(HCEConstant.HCE_BUND_AMT, hceAmt);
  187. bundle.putString(HCEConstant.HCE_BUND_MERCHANT, hceMerchant);
  188. intentConfirm.putExtras(bundle);
  189. intentConfirm.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
  190. startActivity(intentConfirm);
  191. }
  192. }
  193.  
  194. @Override
  195. public void onDeactivated(int reason) {
  196. MyLogsUtil.loggi(TAG, "onDeactivated(" + String.valueOf(reason) + ")");
  197. MyFileUtil.saveLogToExt(TAG, "onDeactivated");
  198. }
  199. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement