]> www.average.org Git - YkNeoCR.git/blobdiff - src/org/average/nfcauthcr/Enroll.java
WIP convert to service (not working)
[YkNeoCR.git] / src / org / average / nfcauthcr / Enroll.java
index 5b125fa490a3d4cb01527eb2a48a576530ba3171..4ca03a88bc4f9437cd2f837ceb12a820719f462c 100644 (file)
@@ -5,25 +5,49 @@ import java.util.Random;
 import android.os.Bundle;
 import android.app.Activity;
 import android.app.AlertDialog;
 import android.os.Bundle;
 import android.app.Activity;
 import android.app.AlertDialog;
-import android.preference.PreferenceManager;
+import android.content.Context;
+import android.content.ComponentName;
 import android.content.Intent;
 import android.content.Intent;
+import android.content.ServiceConnection;
 import android.content.SharedPreferences;
 import android.content.SharedPreferences.Editor;
 import android.content.DialogInterface;
 import android.content.SharedPreferences;
 import android.content.SharedPreferences.Editor;
 import android.content.DialogInterface;
+import android.preference.PreferenceManager;
 import android.util.Log;
 import android.view.View;
 import android.widget.TextView;
 import android.widget.RadioButton;
 
 import org.average.nfcauthcr.Check;
 import android.util.Log;
 import android.view.View;
 import android.widget.TextView;
 import android.widget.RadioButton;
 
 import org.average.nfcauthcr.Check;
+import org.average.nfcauthcr.CheckConnector;
 
 public class Enroll extends Activity {
 
        private final String TAG = getClass().getName();
 
 
 public class Enroll extends Activity {
 
        private final String TAG = getClass().getName();
 
+       private Enroll thisActivity = this;
        private static boolean waitingForResult = false;
        private static SharedPreferences prefs;
        private static int slot;
        private static boolean waitingForResult = false;
        private static SharedPreferences prefs;
        private static int slot;
+       private AlertDialog swipeDialog;
+       private CheckConnector checkConnector;
+       private boolean mBound = false;
+       private byte[] challenge = new byte[63];
+
+       private ServiceConnection mConnection = new ServiceConnection() {
+               @Override
+               public void onServiceConnected(ComponentName className,
+                                               IBinder service) {
+                       checkConnector = (CheckConnector) service;
+                       checkConnector.setCaller(activity);
+                       mBound = true;
+               }
+
+               @Override
+               public void onServiceDisconnected(ComponentName arg0) {
+                       mBound = false;
+               }
+       };
 
        @Override
        protected void onCreate(Bundle savedInstanceState)
 
        @Override
        protected void onCreate(Bundle savedInstanceState)
@@ -31,6 +55,8 @@ public class Enroll extends Activity {
                super.onCreate(savedInstanceState);
                Log.v(TAG, "Starting");
                prefs = PreferenceManager.getDefaultSharedPreferences(this);
                super.onCreate(savedInstanceState);
                Log.v(TAG, "Starting");
                prefs = PreferenceManager.getDefaultSharedPreferences(this);
+               Intent intent = new Intent(this, Check.class);
+               bindService(intent, mConnection, Context.BIND_AUTO_CREATE);
        }
 
        @Override
        }
 
        @Override
@@ -53,13 +79,19 @@ public class Enroll extends Activity {
        protected void onPause() {
                super.onPause();
                Log.v(TAG, "Going inactive, try to stop");
        protected void onPause() {
                super.onPause();
                Log.v(TAG, "Going inactive, try to stop");
-               if (!waitingForResult) { finish(); }
+               if (!waitingForResult) {
+                       finish();
+               }
        }
 
        @Override
        protected void onStop() {
                super.onStop();
                Log.v(TAG, "Stop requested");
        }
 
        @Override
        protected void onStop() {
                super.onStop();
                Log.v(TAG, "Stop requested");
+               if (mBound) {
+                       unbindService(mConnection);
+                       mBound = false;
+               }
        }
 
        public void onSlotSelectionClicked(View view) {
        }
 
        public void onSlotSelectionClicked(View view) {
@@ -78,38 +110,37 @@ public class Enroll extends Activity {
        public void onEnrollClicked(View view) {
                Log.v(TAG, "Enroll clicked");
                if (slot > 0) {
        public void onEnrollClicked(View view) {
                Log.v(TAG, "Enroll clicked");
                if (slot > 0) {
-                       runEnrollment(slot);
+                       runEnrollment();
                } else {
                } else {
-                       showEnrollResult("Must specify which slot to use");
+                       showEnrollResult(R.string.need_slot);
                }
        }
 
                }
        }
 
-       public void onActivityResult(int requestCode, int resultCode,
-                                       Intent intent) {
-               Log.v(TAG, "Got activity result");
+       public void runCallback(int rc, byte[] response) {
+               Log.v(TAG, "Got response");
                waitingForResult = false;
                waitingForResult = false;
-
-               if (resultCode != RESULT_OK) {
-                       Log.v(TAG, "Error result code " + resultCode);
+               if(swipeDialog != null) {
+                       swipeDialog.dismiss();
+                       swipeDialog = null;
+               }
+               if (rc != 0) {
+                       Log.v(TAG, "Error result code " + rc);
                        return;
                }
                        return;
                }
-               byte[] challenge = intent.getByteArrayExtra("challenge");
                Log.v(TAG, "Challenge is \"" + hex(challenge) + "\"");
                Log.v(TAG, "Challenge is \"" + hex(challenge) + "\"");
-               byte[] response = intent.getByteArrayExtra("response");
                Log.v(TAG, "Response is  \"" + hex(response) + "\"");
                Editor editor = prefs.edit();
                editor.putString("challenge", hex(challenge));
                editor.putString("response", hex(response));
                editor.commit();
                Log.v(TAG, "Response is  \"" + hex(response) + "\"");
                Editor editor = prefs.edit();
                editor.putString("challenge", hex(challenge));
                editor.putString("response", hex(response));
                editor.commit();
+               showEnrollResult(R.string.enroll_success);
                Log.v(TAG, "stored new challenge and response");
        }
 
                Log.v(TAG, "stored new challenge and response");
        }
 
-       private void showEnrollResult(final String msg) {
-               Log.v(TAG, "Show result: \"" + msg + "\"");
-
+       private void showEnrollResult(int messageid) {
                AlertDialog.Builder builder = new AlertDialog.Builder(this);
                builder.setTitle(R.string.enrollresult);
                AlertDialog.Builder builder = new AlertDialog.Builder(this);
                builder.setTitle(R.string.enrollresult);
-               builder.setMessage(msg);
+               builder.setMessage(messageid);
                builder.setPositiveButton(android.R.string.ok,
                                new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int which) {
                builder.setPositiveButton(android.R.string.ok,
                                new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int which) {
@@ -121,17 +152,28 @@ public class Enroll extends Activity {
                dialog.show();
        }
 
                dialog.show();
        }
 
-       private void runEnrollment(int slot) {
+       private void runEnrollment() {
                Random rng = new Random();
                Random rng = new Random();
-               byte[] challenge = new byte[63];
                rng.nextBytes(challenge);
                Log.v(TAG, "Random challenge: " + hex(challenge));
                rng.nextBytes(challenge);
                Log.v(TAG, "Random challenge: " + hex(challenge));
-               Log.v(TAG, "Launching challenging activity");
-               Intent crIntent = new Intent(this, Check.class);
-               crIntent.putExtra("slot", slot);
-               crIntent.putExtra("challenge", challenge);
-               this.startActivityForResult(crIntent, 0);
                waitingForResult = true;
                waitingForResult = true;
+               checkConnector.runEnroll(slot, challenge);
+               if (swipeDialog != null) swipeDialog.dismiss();
+               swipeDialog = makeDialog();
+               swipeDialog.show();
+       }
+
+       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) {
+                               Log.v(TAG, "unbind service FIXME");
+                       }
+               });
+               return builder.create();
        }
 
        private String hex(byte[] a) {
        }
 
        private String hex(byte[] a) {