]> www.average.org Git - YkNeoCR.git/blob - src/org/average/nfcauthcr/Check.java
27a3d7161acfc7fe444c10564acb6d16ebf9d736
[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 AlertDialog makeDialog() {
88                 AlertDialog.Builder builder = new AlertDialog.Builder(this);
89                 builder.setTitle(R.string.challenging);
90                 builder.setMessage(R.string.swipe);
91                 builder.setOnCancelListener(
92                                 new DialogInterface.OnCancelListener() {
93                         public void onCancel(DialogInterface dialog) {
94                                 finish();
95                         }
96                 });
97                 return builder.create();
98         }
99
100 /*
101 <receiver android:name=".IsoDepReceiver"
102           android:label="IsoDepReceiver">
103      <intent-filter>
104          <action android:name="android.nfc.action.TECH_DISCOVERED" />
105      </intent-filter>
106
107      <meta-data android:name="android.nfc.action.TECH_DISCOVERED"
108          android:resource="@xml/filter_nfc"
109      />
110  </receiver>
111
112 <resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">
113      <!-- capture anything using IsoDep -->
114      <tech-list>
115          <tech>android.nfc.tech.IsoDep</tech>
116      </tech-list>
117  </resources>
118
119 */
120
121         private void enableDispatch(int slot) {
122                 Intent intent = getIntent();
123                 intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
124                 intent.putExtra("slot", slot);
125                 tagIntent = PendingIntent.getActivity(this, 0, intent, 0);
126                 IntentFilter iso =
127                         new IntentFilter(NfcAdapter.ACTION_TECH_DISCOVERED);
128                 NfcAdapter adapter = NfcAdapter.getDefaultAdapter(this);
129                 if (adapter == null) {
130                         Toast.makeText(this, R.string.no_nfc,
131                                                 Toast.LENGTH_LONG).show();
132                         finish();
133                 }
134                 if (! adapter.isEnabled()) {
135                         Toast.makeText(this, R.string.nfc_disabled,
136                                                 Toast.LENGTH_LONG).show();
137                         finish();
138                 }
139                 adapter.enableForegroundDispatch(
140                         this, tagIntent, new IntentFilter[] {iso},
141                         new String[][] {new String[] {IsoDep.class.getName()}});
142         }
143
144         private void disableDispatch() {
145                 if (tagIntent != null) {
146                         tagIntent.cancel();
147                         tagIntent = null;
148                 }
149                 NfcAdapter adapter = NfcAdapter.getDefaultAdapter(this);
150                 if (adapter != null) {
151                         adapter.disableForegroundDispatch(this);
152                 }
153         }
154 }