1 package com.android.cts.verifier.nfc.hce; 2 3 import android.app.Dialog; 4 import android.content.BroadcastReceiver; 5 import android.content.ComponentName; 6 import android.content.Context; 7 import android.content.Intent; 8 import android.content.IntentFilter; 9 import android.nfc.NfcAdapter; 10 import android.nfc.NfcManager; 11 import android.os.Bundle; 12 13 import com.android.cts.verifier.R; 14 import com.android.cts.verifier.nfc.NfcDialogs; 15 16 public class ScreenOffPaymentEmulatorActivity extends BaseEmulatorActivity { 17 final static int STATE_SCREEN_ON = 0; 18 final static int STATE_SCREEN_OFF = 1; 19 private static final int SECURE_NFC_ENABLED_DIALOG_ID = 1; 20 private int mState = STATE_SCREEN_ON; 21 22 private ScreenOnOffReceiver mReceiver; 23 private NfcAdapter mNfcAdapter; 24 25 @Override onCreate(Bundle savedInstanceState)26 protected void onCreate(Bundle savedInstanceState) { 27 super.onCreate(savedInstanceState); 28 setContentView(R.layout.pass_fail_text); 29 setPassFailButtonClickListeners(); 30 getPassButton().setEnabled(false); 31 mState = STATE_SCREEN_ON; 32 setupServices(this, ScreenOffPaymentService.COMPONENT); 33 34 mReceiver = new ScreenOnOffReceiver(); 35 IntentFilter filter = new IntentFilter(); 36 filter.addAction(Intent.ACTION_SCREEN_OFF); 37 filter.addAction(Intent.ACTION_SCREEN_ON); 38 registerReceiver(mReceiver, filter); 39 40 NfcManager nfcManager = getSystemService(NfcManager.class); 41 mNfcAdapter = nfcManager.getDefaultAdapter(); 42 } 43 44 @Override onResume()45 protected void onResume() { 46 super.onResume(); 47 if (mNfcAdapter.isSecureNfcSupported() && mNfcAdapter.isSecureNfcEnabled()) { 48 showDialog(SECURE_NFC_ENABLED_DIALOG_ID); 49 } 50 } 51 52 @Override onDestroy()53 protected void onDestroy() { 54 super.onDestroy(); 55 unregisterReceiver(mReceiver); 56 } 57 58 @Override onServicesSetup(boolean result)59 void onServicesSetup(boolean result) { 60 // Verify ScreenOff HCE service is the default 61 if (makePaymentDefault(ScreenOffPaymentService.COMPONENT, 62 R.string.nfc_hce_change_preinstalled_wallet)) { 63 // Wait for callback 64 } else { 65 NfcDialogs.createHceTapReaderDialog(this, 66 getString(R.string.nfc_screen_off_hce_payment_help)).show(); 67 } 68 } 69 70 @Override onPaymentDefaultResult(ComponentName component, boolean success)71 void onPaymentDefaultResult(ComponentName component, boolean success) { 72 if (success) { 73 NfcDialogs.createHceTapReaderDialog(this, 74 getString(R.string.nfc_screen_off_hce_payment_help)).show(); 75 } 76 } 77 78 @Override onPause()79 protected void onPause() { 80 super.onPause(); 81 } buildReaderIntent(Context context)82 public static Intent buildReaderIntent(Context context) { 83 Intent readerIntent = new Intent(context, SimpleReaderActivity.class); 84 readerIntent.putExtra(SimpleReaderActivity.EXTRA_APDUS, 85 ScreenOffPaymentService.APDU_COMMAND_SEQUENCE); 86 readerIntent.putExtra(SimpleReaderActivity.EXTRA_RESPONSES, 87 ScreenOffPaymentService.APDU_RESPOND_SEQUENCE); 88 readerIntent.putExtra(SimpleReaderActivity.EXTRA_LABEL, 89 context.getString(R.string.nfc_screen_off_hce_payment_reader)); 90 return readerIntent; 91 } 92 93 @Override onApduSequenceComplete(ComponentName component, long duration)94 void onApduSequenceComplete(ComponentName component, long duration) { 95 if (component.equals(ScreenOffPaymentService.COMPONENT) && mState == STATE_SCREEN_OFF) { 96 getPassButton().setEnabled(true); 97 } 98 } 99 100 @Override onCreateDialog(int id, Bundle args)101 public Dialog onCreateDialog(int id, Bundle args) { 102 switch (id) { 103 case SECURE_NFC_ENABLED_DIALOG_ID: 104 return NfcDialogs.createSecureNfcEnabledDialog(this); 105 default: 106 return super.onCreateDialog(id, args); 107 } 108 } 109 110 private class ScreenOnOffReceiver extends BroadcastReceiver { 111 @Override onReceive(Context context, Intent intent)112 public void onReceive(Context context, Intent intent) { 113 String action = intent.getAction(); 114 if (action.equals(Intent.ACTION_SCREEN_OFF)) { 115 mState = STATE_SCREEN_OFF; 116 } else if (action.equals(Intent.ACTION_SCREEN_ON)) { 117 mState = STATE_SCREEN_ON; 118 } 119 } 120 } 121 } 122