]> www.average.org Git - YkNeoCR.git/blobdiff - src/org/average/nfcauthcr/TagEvent.java
reorg, separate roles
[YkNeoCR.git] / src / org / average / nfcauthcr / TagEvent.java
index 7ef69433537bae7c64cd28196f5d2b4e0f54555b..febfa63fc9914e14089915e06130c3ecba4881c3 100644 (file)
@@ -13,26 +13,35 @@ import android.nfc.tech.IsoDep;
 import android.util.Log;
 import android.widget.Toast;
 
-import org.average.nfcauthcr.YkNeo;
-import org.average.nfcauthcr.CRException;
+import org.average.nfcauthcr.NfcCRdispatch;
 
 public class TagEvent extends Activity {
 
        private final String TAG = getClass().getName();
 
-       private PendingIntent tagIntent;
+       private NfcCRdispatch dispatch = new NfcCRdispatch((Activity)this);
 
        @Override
        protected void onResume() {
                super.onResume();
                Log.v(TAG, "Starting the work");
 
+               new AlertDialog.Builder(this)
+                       .setTitle(R.string.challenging)
+                       .setMessage(R.string.swipe)
+                       .setOnCancelListener(
+                               new DialogInterface.OnCancelListener() {
+                               public void onCancel(DialogInterface dialog) {
+                                       Log.v(TAG, "Cancel");
+                                       finish();
+                               }
+                       })
+                       .create().show();
+
                Intent intent = getIntent();
                setResult(RESULT_CANCELED);
-               int slot = intent.getIntExtra("slot", -1);
-               if (slot > 0) {
-                       enableDispatch(slot);
-               }
+               byte[] challenge = intent.getByteArrayExtra("challenge");
+               dispatch.onResume(challenge);
        }
 
        @Override
@@ -40,83 +49,18 @@ public class TagEvent extends Activity {
                super.onPause();
                Log.v(TAG, "Finished the work");
 
-               disableDispatch();
+               dispatch.onPause();
        }
 
-       public void onNewIntent(Intent intent) {
+       public void onNewIntent(Intent newintent) {
                Log.v(TAG, "NFC Intent arrived");
-               int slot = intent.getIntExtra("slot", -1);
-               byte[] challenge = intent.getByteArrayExtra("challenge");
-               if (slot <= 0) return;
-               Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
-               if (tag == null) return;
-               IsoDep isoTag = IsoDep.get(tag);
-               try {
-                       byte[] response = YkNeo.doChallengeYubiKey(
-                                               isoTag, slot, challenge);
-                       Intent data = getIntent();
-                       data.putExtra("response", response);
-                       setResult(RESULT_OK, data);
-               } catch (CRException e) {
-                       Log.v(TAG, e.getMessage());
-                       Toast.makeText(this, e.getMessage(),
-                                       Toast.LENGTH_LONG).show();
-               }
-               finish();
-       }
-
-/*
-<receiver android:name=".IsoDepReceiver"
-          android:label="IsoDepReceiver">
-     <intent-filter>
-         <action android:name="android.nfc.action.TECH_DISCOVERED" />
-     </intent-filter>
-
-     <meta-data android:name="android.nfc.action.TECH_DISCOVERED"
-         android:resource="@xml/filter_nfc"
-     />
- </receiver>
-
-<resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">
-     <!-- capture anything using IsoDep -->
-     <tech-list>
-         <tech>android.nfc.tech.IsoDep</tech>
-     </tech-list>
- </resources>
 
-*/
-
-       private void enableDispatch(int slot) {
-               Intent intent = getIntent();
-               intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
-               intent.putExtra("slot", slot);
-               tagIntent = PendingIntent.getActivity(this, 0, intent, 0);
-               IntentFilter iso =
-                       new IntentFilter(NfcAdapter.ACTION_TECH_DISCOVERED);
-               NfcAdapter adapter = NfcAdapter.getDefaultAdapter(this);
-               if (adapter == null) {
-                       Toast.makeText(this, R.string.no_nfc,
-                                               Toast.LENGTH_LONG).show();
-                       finish();
-               }
-               if (! adapter.isEnabled()) {
-                       Toast.makeText(this, R.string.nfc_disabled,
-                                               Toast.LENGTH_LONG).show();
+               byte[] response = dispatch.onNewIntent(newintent);
+               if (response != null) {
+                       Intent masterintent = getIntent();
+                       masterintent.putExtra("response", response);
+                       setResult(RESULT_OK, masterintent);
                        finish();
                }
-               adapter.enableForegroundDispatch(
-                       this, tagIntent, new IntentFilter[] {iso},
-                       new String[][] {new String[] {IsoDep.class.getName()}});
-       }
-
-       private void disableDispatch() {
-               if (tagIntent != null) {
-                       tagIntent.cancel();
-                       tagIntent = null;
-               }
-               NfcAdapter adapter = NfcAdapter.getDefaultAdapter(this);
-               if (adapter != null) {
-                       adapter.disableForegroundDispatch(this);
-               }
        }
 }