1 package com.android.cts.verifier.nfc.hce;
2 
3 import android.annotation.TargetApi;
4 import android.content.ComponentName;
5 import android.content.Context;
6 import android.content.Intent;
7 import android.os.Bundle;
8 
9 import com.android.cts.verifier.R;
10 import com.android.cts.verifier.nfc.NfcDialogs;
11 
12 @TargetApi(19)
13 public class ChangeDefaultEmulatorActivity extends BaseEmulatorActivity {
14     final static int STATE_IDLE = 0;
15     final static int STATE_SERVICE1_SETTING_UP = 1;
16     final static int STATE_SERVICE2_SETTING_UP = 2;
17     final static int STATE_MAKING_SERVICE1_DEFAULT = 3;
18     final static int STATE_MAKING_SERVICE2_DEFAULT = 4;
19     final static int STATE_DEFAULT_CHANGED = 5;
20 
21     boolean mReceiverRegistered = false;
22     int mState = STATE_IDLE;
23 
24     @Override
onCreate(Bundle savedInstanceState)25     protected void onCreate(Bundle savedInstanceState) {
26         super.onCreate(savedInstanceState);
27         setContentView(R.layout.pass_fail_text);
28         setPassFailButtonClickListeners();
29         getPassButton().setEnabled(false);
30 
31         mState = STATE_SERVICE2_SETTING_UP;
32         setupServices(this, PaymentService2.COMPONENT);
33     }
34 
35     @Override
onResume()36     protected void onResume() {
37         super.onResume();
38     }
39 
40     @Override
onServicesSetup(boolean result)41     void onServicesSetup(boolean result) {
42         if (mState == STATE_SERVICE2_SETTING_UP) {
43             mState = STATE_SERVICE1_SETTING_UP;
44             setupServices(this, PaymentService1.COMPONENT, PaymentService2.COMPONENT);
45             return;
46         }
47         if (!makePaymentDefault(PaymentService2.COMPONENT,
48                 R.string.nfc_hce_change_preinstalled_wallet)) {
49             // Service 2 is already default, make one default now
50             mState = STATE_MAKING_SERVICE1_DEFAULT;
51             makePaymentDefault(PaymentService1.COMPONENT, R.string.nfc_hce_change_default_help);
52         } else {
53             mState = STATE_MAKING_SERVICE2_DEFAULT;
54             // will get callback when 2 is made default
55         }
56     }
57 
58     @Override
onPause()59     protected void onPause() {
60         super.onPause();
61         if (mReceiverRegistered) {
62             unregisterReceiver(mReceiver);
63         }
64     }
65 
buildReaderIntent(Context context)66     public static Intent buildReaderIntent(Context context) {
67         Intent readerIntent = new Intent(context, SimpleReaderActivity.class);
68         readerIntent.putExtra(SimpleReaderActivity.EXTRA_APDUS,
69                 PaymentService1.APDU_COMMAND_SEQUENCE);
70         readerIntent.putExtra(SimpleReaderActivity.EXTRA_RESPONSES,
71                 PaymentService1.APDU_RESPOND_SEQUENCE);
72         readerIntent.putExtra(SimpleReaderActivity.EXTRA_LABEL,
73                 context.getString(R.string.nfc_hce_change_default_reader));
74         return readerIntent;
75     }
76 
77     @Override
onApduSequenceComplete(ComponentName component, long duration)78     void onApduSequenceComplete(ComponentName component, long duration) {
79         if (component.equals(PaymentService1.COMPONENT)) {
80             getPassButton().setEnabled(true);
81         }
82     }
83 
84     @Override
onPaymentDefaultResult(ComponentName component, boolean success)85     void onPaymentDefaultResult(ComponentName component, boolean success) {
86         if (mState == STATE_MAKING_SERVICE2_DEFAULT) {
87             if (success) {
88                 mState = STATE_MAKING_SERVICE1_DEFAULT;
89 	            makePaymentDefault(PaymentService1.COMPONENT, R.string.nfc_hce_change_default_help);
90             }
91         } else if (mState == STATE_MAKING_SERVICE1_DEFAULT) {
92             if (success) {
93                 mState = STATE_DEFAULT_CHANGED;
94                 NfcDialogs.createHceTapReaderDialog(this, null).show();
95             }
96         }
97     }
98 }
99