X-Git-Url: http://www.average.org/gitweb/?p=YkNeoCR.git;a=blobdiff_plain;f=src%2Forg%2Faverage%2Fnfcauthcr%2FCheck.java;h=27a3d7161acfc7fe444c10564acb6d16ebf9d736;hp=c3738f7fc4f80dbe2c799925d522beaa0ea62153;hb=7382b20863e60aad30b9320f133773b71205f2d7;hpb=a0d60dab1717e69b6d853eb91d0065fe378e3c28
diff --git a/src/org/average/nfcauthcr/Check.java b/src/org/average/nfcauthcr/Check.java
index c3738f7..27a3d71 100644
--- a/src/org/average/nfcauthcr/Check.java
+++ b/src/org/average/nfcauthcr/Check.java
@@ -1,59 +1,68 @@
package org.average.nfcauthcr;
-import android.app.Activity;
-import android.app.AlertDialog;
+import android.app.Service;
import android.app.PendingIntent;
-import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
+import android.content.SharedPreferences;
+import android.content.SharedPreferences.Editor;
import android.nfc.NfcAdapter;
import android.nfc.Tag;
import android.nfc.TagLostException;
import android.nfc.tech.IsoDep;
+import android.os.Binder;
+import android.os.IBinder;
+import android.preference.PreferenceManager;
import android.util.Log;
import android.widget.Toast;
+import org.average.nfcauthcr.CheckConnector;
import org.average.nfcauthcr.YkNeo;
+import org.average.nfcauthcr.CRException;
-public class Check extends Activity {
+public class Check extends Service {
private final String TAG = getClass().getName();
- private AlertDialog swipeDialog;
+ private final CheckConnector checkConnector = new CheckConnector();
+ private SharedPreferences prefs;
+ private int startId;
private PendingIntent tagIntent;
@Override
- protected void onResume() {
- super.onResume();
- Log.v(TAG, "Starting the work");
+ public void onCreate() {
+ Log.v(TAG, "Created");
+ prefs = PreferenceManager.getDefaultSharedPreferences(this);
+ }
- Intent intent = getIntent();
- setResult(RESULT_CANCELED);
- if (swipeDialog != null) {
- swipeDialog.dismiss();
- swipeDialog = null;
- }
- int slot = intent.getIntExtra("slot", -1);
+ @Override
+ public int onStartCommand (Intent intent, int flags, int startId) {
+ Log.v(TAG, "Starting service");
+ this.startId = startId;
+ int slot = prefs.getInt("slot_number", -1);
if (slot > 0) {
- swipeDialog = makeDialog();
- swipeDialog.show();
enableDispatch(slot);
+ } else {
+ stopSelf(startId);
}
+ return START_NOT_STICKY;
}
@Override
- protected void onPause() {
- super.onPause();
- Log.v(TAG, "Finished the work");
+ public IBinder onBind(Intent intent) {
+ Log.v(TAG, "Binding");
+ checkConnector.setService(this);
+ return checkConnector;
+ }
- if(swipeDialog != null) {
- swipeDialog.dismiss();
- swipeDialog = null;
- }
+ @Override
+ public void onDestroy() {
+ Log.v(TAG, "Finished the run");
disableDispatch();
}
- public void onNewIntent(Intent intent) {
+ @Override
+ protected void onNewIntent(Intent intent) {
Log.v(TAG, "NFC Intent arrived");
int slot = intent.getIntExtra("slot", -1);
byte[] challenge = intent.getByteArrayExtra("challenge");
@@ -61,12 +70,16 @@ public class Check extends Activity {
Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
if (tag == null) return;
IsoDep isoTag = IsoDep.get(tag);
- byte[] response = YkNeo.doChallengeYubiKey(
+ try {
+ byte[] response = YkNeo.doChallengeYubiKey(
isoTag, slot, challenge);
- if (response != null) {
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();
}
@@ -84,6 +97,27 @@ public class Check extends Activity {
return builder.create();
}
+/*
+
+
+
+
+
+
+
+
+
+
+
+ android.nfc.tech.IsoDep
+
+
+
+*/
+
private void enableDispatch(int slot) {
Intent intent = getIntent();
intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
@@ -96,13 +130,11 @@ public class Check extends Activity {
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},