]> www.average.org Git - YkNeoCR.git/blob - src/org/average/nfcauthcr/Check.java
WIP convert to service (not working)
[YkNeoCR.git] / src / org / average / nfcauthcr / Check.java
1 package org.average.nfcauthcr;
2
3 import android.app.Service;
4 import android.app.PendingIntent;
5 import android.content.Intent;
6 import android.content.IntentFilter;
7 import android.content.SharedPreferences;
8 import android.content.SharedPreferences.Editor;
9 import android.nfc.NfcAdapter;
10 import android.nfc.Tag;
11 import android.nfc.TagLostException;
12 import android.nfc.tech.IsoDep;
13 import android.os.Binder;
14 import android.os.IBinder;
15 import android.preference.PreferenceManager;
16 import android.util.Log;
17 import android.widget.Toast;
18
19 import org.average.nfcauthcr.CheckConnector;
20 import org.average.nfcauthcr.YkNeo;
21 import org.average.nfcauthcr.CRException;
22
23 public class Check extends Service {
24
25         private final String TAG = getClass().getName();
26
27         private final CheckConnector checkConnector = new CheckConnector();
28         private SharedPreferences prefs;
29         private int startId;
30         private PendingIntent tagIntent;
31
32         @Override
33         public void onCreate() {
34                 Log.v(TAG, "Created");
35                 prefs = PreferenceManager.getDefaultSharedPreferences(this);
36         }
37
38         @Override
39         public int onStartCommand (Intent intent, int flags, int startId) {
40                 Log.v(TAG, "Starting service");
41                 this.startId = startId;
42                 int slot = prefs.getInt("slot_number", -1);
43                 if (slot > 0) {
44                         enableDispatch(slot);
45                 } else {
46                         stopSelf(startId);
47                 }
48                 return START_NOT_STICKY;
49         }
50
51         @Override
52         public IBinder onBind(Intent intent) {
53                 Log.v(TAG, "Binding");
54                 checkConnector.setService(this);
55                 return checkConnector;
56         }
57
58         @Override
59         public void onDestroy() {
60                 Log.v(TAG, "Finished the run");
61                 disableDispatch();
62         }
63
64         @Override
65         protected void onNewIntent(Intent intent) {
66                 Log.v(TAG, "NFC Intent arrived");
67                 int slot = intent.getIntExtra("slot", -1);
68                 byte[] challenge = intent.getByteArrayExtra("challenge");
69                 if (slot <= 0) return;
70                 Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
71                 if (tag == null) return;
72                 IsoDep isoTag = IsoDep.get(tag);
73                 try {
74                         byte[] response = YkNeo.doChallengeYubiKey(
75                                                 isoTag, slot, challenge);
76                         Intent data = getIntent();
77                         data.putExtra("response", response);
78                         setResult(RESULT_OK, data);
79                 } catch (CRException e) {
80                         Log.v(TAG, e.getMessage());
81                         Toast.makeText(this, e.getMessage(),
82                                         Toast.LENGTH_LONG).show();
83                 }
84                 finish();
85         }
86
87         private void enableDispatch(int slot) {
88                 Intent intent = getIntent();
89                 intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
90                 intent.putExtra("slot", slot);
91                 tagIntent = PendingIntent.getActivity(this, 0, intent, 0);
92                 IntentFilter iso =
93                         new IntentFilter(NfcAdapter.ACTION_TECH_DISCOVERED);
94                 NfcAdapter adapter = NfcAdapter.getDefaultAdapter(this);
95                 if (adapter == null) {
96                         Toast.makeText(this, R.string.no_nfc,
97                                                 Toast.LENGTH_LONG).show();
98                         finish();
99                 }
100                 if (! adapter.isEnabled()) {
101                         Toast.makeText(this, R.string.nfc_disabled,
102                                                 Toast.LENGTH_LONG).show();
103                         finish();
104                 }
105                 adapter.enableForegroundDispatch(
106                         this, tagIntent, new IntentFilter[] {iso},
107                         new String[][] {new String[] {IsoDep.class.getName()}});
108         }
109
110         private void disableDispatch() {
111                 if (tagIntent != null) {
112                         tagIntent.cancel();
113                         tagIntent = null;
114                 }
115                 NfcAdapter adapter = NfcAdapter.getDefaultAdapter(this);
116                 if (adapter != null) {
117                         adapter.disableForegroundDispatch(this);
118                 }
119         }
120 }