]> www.average.org Git - YkNeoCR.git/commitdiff
intents work
authorEugene Crosser <crosser@average.org>
Wed, 1 May 2013 22:25:05 +0000 (02:25 +0400)
committerEugene Crosser <crosser@average.org>
Wed, 1 May 2013 22:25:05 +0000 (02:25 +0400)
AndroidManifest.xml
res/values/strings.xml
src/org/average/nfcauthcr/NFCAuthCRCheck.java [new file with mode: 0644]
src/org/average/nfcauthcr/NFCAuthCREnroll.java

index ee3692d339fb36b699577c411212bf96222c1451..efcae302e317cf8b84de6846edc2ea315e6202a1 100644 (file)
@@ -22,6 +22,9 @@
                 <category android:name="android.intent.category.LAUNCHER" />
             </intent-filter>
         </activity>
+        <activity android:name=".NFCAuthCRCheck"
+                  android:label="@string/app_name">
+        </activity>
 
     </application>
 </manifest>
index 0fa12b66802be0fad0cc93cdcfb42aead51d8300..b13405eded132bab4e7d944554afe7e724fc9cf3 100644 (file)
@@ -6,4 +6,10 @@
        <string name="slot_1">Slot 1</string>
        <string name="slot_2">Slot 2</string>
        <string name="enrollresult">Enrollment result:</string>
+       <string name="challenging">Ready to send challenge</string>
+       <string name="swipe">Please touch the token with the phone</string>
+       <string name="no_nfc">Cannot reach the NFC adapter</string>
+       <string name="nfc_disabled">Need to enable NFC in Settings</string>
+       <string name="tag_lost">Communitation with the tag lost</string>
+       <string name="tag_error">Error communicating with the tag</string>
 </resources>
diff --git a/src/org/average/nfcauthcr/NFCAuthCRCheck.java b/src/org/average/nfcauthcr/NFCAuthCRCheck.java
new file mode 100644 (file)
index 0000000..6637c00
--- /dev/null
@@ -0,0 +1,149 @@
+package org.average.nfcauthcr;
+
+import java.io.IOException;
+
+import android.os.Bundle;
+import android.app.Activity;
+import android.app.AlertDialog;
+import android.app.PendingIntent;
+import android.content.DialogInterface;
+import android.content.Intent;
+import android.content.IntentFilter;
+import android.nfc.NfcAdapter;
+import android.nfc.Tag;
+import android.nfc.TagLostException;
+import android.nfc.tech.IsoDep;
+import android.util.Log;
+import android.widget.Toast;
+
+public class NFCAuthCRCheck extends Activity {
+
+       private final String TAG = getClass().getName();
+
+       private static final byte[] selectCommand =
+               {0x00, (byte) 0xA4, 0x04, 0x00, 0x07, (byte) 0xA0,
+                0x00, 0x00, 0x05, 0x27, 0x20, 0x01, 0x00};
+
+       private AlertDialog swipeDialog;
+       private PendingIntent tagIntent;
+
+       @Override
+       protected void onResume() {
+               super.onResume();
+               Log.v(TAG, "Starting the work");
+
+               Intent intent = getIntent();
+               Bundle extras = intent.getExtras();
+               setResult(RESULT_CANCELED);
+               if(swipeDialog != null) {
+                       swipeDialog.dismiss();
+                       swipeDialog = null;
+               }
+               if(extras != null) {
+                       int slot = extras.getInt("slot");
+                       if (slot > 0) {
+                               swipeDialog = makeDialog();
+                               swipeDialog.show();
+                               enableDispatch(slot);
+                       }
+               }
+       }
+
+       @Override
+       protected void onPause() {
+               super.onPause();
+               Log.v(TAG, "Finished the work");
+
+               if(swipeDialog != null) {
+                       swipeDialog.dismiss();
+                       swipeDialog = null;
+               }
+               disableDispatch();
+       }
+
+       public void onNewIntent(Intent intent) {
+               Log.v(TAG, "NFC Intent arrived");
+               int slot = intent.getIntExtra("slot", -1);
+               if (slot <= 0) return;
+               Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
+               if (tag == null) return;
+               IsoDep isoTag = IsoDep.get(tag);
+               try {
+                       isoTag.connect();
+                       byte[] resp = isoTag.transceive(selectCommand);
+                       int length = resp.length;
+                       if (resp[length - 2] == (byte)0x90 &&
+                           resp[length - 1] == 0x00) {
+                               doChallengeYubiKey(isoTag, slot);
+                       } else {
+                               Toast.makeText(this, R.string.tag_error,
+                                               Toast.LENGTH_LONG).show();
+                       }
+               } catch (TagLostException e) {
+                       Toast.makeText(this,
+                               R.string.tag_lost, Toast.LENGTH_LONG).show();
+               } catch (IOException e) {
+                       Toast.makeText(this,
+                               getText(R.string.tag_error) +
+                               e.getMessage(),
+                               Toast.LENGTH_LONG).show();
+               }
+               finish();
+       }
+
+       private AlertDialog makeDialog() {
+               AlertDialog.Builder builder = new AlertDialog.Builder(this);
+               builder.setTitle(R.string.challenging);
+               builder.setMessage(R.string.swipe);
+               builder.setOnCancelListener(
+                               new DialogInterface.OnCancelListener() {
+                       public void onCancel(DialogInterface dialog) {
+                               finish();
+                       }
+               });
+               return builder.create();
+       }
+
+       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();
+                       return;
+               }
+               if (! adapter.isEnabled()) {
+                       Toast.makeText(this, R.string.nfc_disabled,
+                                               Toast.LENGTH_LONG).show();
+                       finish();
+                       return;
+               }
+               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);
+               }
+       }
+
+       private void doChallengeYubiKey(IsoDep isoTag, int slot)
+                                               throws IOException {
+               Intent data = getIntent();
+               data.putExtra("response","<FIXME>real data here");
+               setResult(RESULT_OK, data);
+       }
+}
index 6aae6f1970e4e38b30d10285ed3b1ab904abeafc..bb6b4f0357118db644ce4e7aace211823aa5be34 100644 (file)
@@ -4,6 +4,7 @@ import android.os.Bundle;
 import android.app.Activity;
 import android.app.AlertDialog;
 import android.preference.PreferenceManager;
+import android.content.Intent;
 import android.content.SharedPreferences;
 import android.content.SharedPreferences.Editor;
 import android.content.DialogInterface;
@@ -12,8 +13,10 @@ import android.view.View;
 import android.widget.TextView;
 import android.widget.RadioButton;
 
-public class NFCAuthCREnroll extends Activity
-{
+import org.average.nfcauthcr.NFCAuthCRCheck;
+
+public class NFCAuthCREnroll extends Activity {
+
        private final String TAG = getClass().getName();
 
        private static boolean waitingForResult = false;
@@ -73,12 +76,24 @@ public class NFCAuthCREnroll extends Activity
        public void onEnrollClicked(View view) {
                Log.v(TAG, "Enroll clicked");
                if (slot > 0) {
-                       showEnrollResult("<FIXME> using slot" + slot);
+                       runChallenge(slot);
                } else {
                        showEnrollResult("Must specify which slot to use");
                }
        }
 
+       public void onActivityResult(int requestCode, int resultCode,
+                                       Intent intent) {
+               Log.v(TAG, "Got activity result");
+               waitingForResult = false;
+               if (resultCode == RESULT_OK) {
+                       String res = intent.getStringExtra("response");
+                       Log.v(TAG, "Response is \"" + res + "\"");
+               } else {
+                       Log.v(TAG, "Error result code " + resultCode);
+               }
+       }
+
        private void showEnrollResult(final String msg) {
                Log.v(TAG, "Show result: \"" + msg + "\"");
 
@@ -95,4 +110,12 @@ public class NFCAuthCREnroll extends Activity
                AlertDialog dialog = builder.create();
                dialog.show();
        }
+
+       private void runChallenge(int slot) {
+               Log.v(TAG, "Launching challenging activity");
+               Intent crIntent = new Intent(this, NFCAuthCRCheck.class);
+               crIntent.putExtra("slot", slot);
+               this.startActivityForResult(crIntent, 0);
+               waitingForResult = true;
+       }
 }