1 | package nl.powercraft.tagtracker; |
---|
2 | |
---|
3 | |
---|
4 | import java.io.File; |
---|
5 | import java.io.IOException; |
---|
6 | import java.text.DateFormat; |
---|
7 | import java.util.ArrayList; |
---|
8 | import java.util.Date; |
---|
9 | |
---|
10 | import android.app.Activity; |
---|
11 | import android.app.AlertDialog; |
---|
12 | import android.app.PendingIntent; |
---|
13 | import android.content.Context; |
---|
14 | import android.content.DialogInterface; |
---|
15 | import android.content.Intent; |
---|
16 | import android.content.IntentFilter; |
---|
17 | import android.content.IntentFilter.MalformedMimeTypeException; |
---|
18 | import android.content.SharedPreferences; |
---|
19 | import android.media.AudioManager; |
---|
20 | import android.media.ToneGenerator; |
---|
21 | import android.net.Uri; |
---|
22 | import android.nfc.NfcAdapter; |
---|
23 | import android.nfc.tech.NfcA; |
---|
24 | import android.os.Bundle; |
---|
25 | import android.os.Environment; |
---|
26 | import android.os.Handler; |
---|
27 | import android.os.PowerManager; |
---|
28 | import android.os.PowerManager.WakeLock; |
---|
29 | import android.util.Log; |
---|
30 | import android.view.Menu; |
---|
31 | import android.view.MenuInflater; |
---|
32 | import android.view.MenuItem; |
---|
33 | import android.view.View; |
---|
34 | import android.widget.Button; |
---|
35 | import android.widget.EditText; |
---|
36 | import android.widget.RelativeLayout; |
---|
37 | import android.widget.ToggleButton; |
---|
38 | |
---|
39 | public class TagTrackerActivity extends Activity { |
---|
40 | private NfcAdapter mAdapter; |
---|
41 | private PendingIntent mPendingIntent; |
---|
42 | private IntentFilter[] mFilters; |
---|
43 | private String[][] mTechLists; |
---|
44 | private FileHandler fh = new FileHandler(); |
---|
45 | private boolean writeAllow = false; |
---|
46 | private ArrayList<String> cardID = new ArrayList<String>(); |
---|
47 | private RelativeLayout mScreen; |
---|
48 | |
---|
49 | @SuppressWarnings("unchecked") |
---|
50 | @Override |
---|
51 | public void onCreate(Bundle savedState) { |
---|
52 | super.onCreate(savedState); |
---|
53 | final PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE); |
---|
54 | WakeLock mWakeLock; |
---|
55 | setContentView(R.layout.main); |
---|
56 | final Button button = (Button) findViewById(R.id.zend); |
---|
57 | button.setOnClickListener(new View.OnClickListener() { |
---|
58 | public void onClick(View v) { |
---|
59 | zenden(); |
---|
60 | } |
---|
61 | }); |
---|
62 | SharedPreferences p = getPreferences(MODE_PRIVATE); |
---|
63 | writeAllow = p.getBoolean("writeAllow", false); |
---|
64 | cardID = (ArrayList<String>) ObjectSerializer.deserialize(p.getString("cardID", ObjectSerializer.serialize(new ArrayList<String>()))); |
---|
65 | ToggleButton tg = (ToggleButton) findViewById(R.id.status); |
---|
66 | tg.setChecked(writeAllow); |
---|
67 | mScreen = (RelativeLayout) findViewById(R.id.Screen); |
---|
68 | mWakeLock = pm.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK | PowerManager.ON_AFTER_RELEASE,""); |
---|
69 | mWakeLock.acquire(); |
---|
70 | mAdapter = NfcAdapter.getDefaultAdapter(this); |
---|
71 | |
---|
72 | // Create a generic PendingIntent that will be deliver to this activity. The NFC stack |
---|
73 | // will fill in the intent with the details of the discovered tag before delivering to |
---|
74 | // this activity. |
---|
75 | mPendingIntent = PendingIntent.getActivity(this, 0, new Intent(this, getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0); |
---|
76 | |
---|
77 | // Setup an intent filter for all MIME based dispatches |
---|
78 | IntentFilter tech = new IntentFilter(NfcAdapter.ACTION_TECH_DISCOVERED); |
---|
79 | try { |
---|
80 | tech.addDataType("*/*"); |
---|
81 | } catch (MalformedMimeTypeException e) { |
---|
82 | throw new RuntimeException("fail", e); |
---|
83 | } |
---|
84 | mFilters = new IntentFilter[] { |
---|
85 | tech, |
---|
86 | }; |
---|
87 | |
---|
88 | // Setup a tech list for all NfcF tags |
---|
89 | mTechLists = new String[][] { new String[] { NfcA.class.getName() } }; |
---|
90 | } |
---|
91 | |
---|
92 | public void onToggleClicked(View v) { |
---|
93 | // Perform action on clicks |
---|
94 | if (((ToggleButton) v).isChecked()) { |
---|
95 | if(fh.init()) |
---|
96 | { |
---|
97 | fh.writeStart(DateFormat.getDateTimeInstance().format(new Date())); |
---|
98 | writeAllow = true; |
---|
99 | } |
---|
100 | else |
---|
101 | { |
---|
102 | finish(); |
---|
103 | } |
---|
104 | } else { |
---|
105 | fh.writeEnd(DateFormat.getDateTimeInstance().format(new Date())); |
---|
106 | writeAllow = false; |
---|
107 | cardID.clear(); |
---|
108 | } |
---|
109 | } |
---|
110 | |
---|
111 | @Override |
---|
112 | public void onResume() { |
---|
113 | super.onResume(); |
---|
114 | if (mAdapter != null) mAdapter.enableForegroundDispatch(this, mPendingIntent, mFilters, |
---|
115 | mTechLists); |
---|
116 | } |
---|
117 | |
---|
118 | @Override |
---|
119 | public void onNewIntent(Intent intent) { |
---|
120 | if(writeAllow) |
---|
121 | { |
---|
122 | byte[] bid = intent.getByteArrayExtra(NfcAdapter.EXTRA_ID); |
---|
123 | try |
---|
124 | { |
---|
125 | String uid = getHexString(bid); |
---|
126 | String time = DateFormat.getDateTimeInstance().format(new Date()); |
---|
127 | if(!cardID.contains(uid)) |
---|
128 | { |
---|
129 | //Log.i("Foreground dispatch", "Discovered tag with ID: " + uid); |
---|
130 | //mText.append("\r\n["+time+"] Discovered tag " + ++mCount + " with ID: " + uid); |
---|
131 | fh.writeUID(uid, time); |
---|
132 | cardID.add(uid); |
---|
133 | setBackground(0xFF00FF00, 200); |
---|
134 | playSound(this.getApplicationContext(), false); |
---|
135 | } |
---|
136 | else |
---|
137 | { |
---|
138 | setBackground(0xFFFFEE00, 1000); |
---|
139 | playSound(this.getApplicationContext(), true); |
---|
140 | //mText.append("\r\n["+time+"] Tag ID " + uid + " is already added."); |
---|
141 | } |
---|
142 | } |
---|
143 | catch (Exception e) |
---|
144 | { |
---|
145 | Log.e("TagTracker", "" + e); |
---|
146 | } |
---|
147 | } |
---|
148 | } |
---|
149 | |
---|
150 | public void playSound(Context context, boolean twice) throws IllegalArgumentException, SecurityException, IllegalStateException, IOException { |
---|
151 | final ToneGenerator tg = new ToneGenerator(AudioManager.STREAM_ALARM, 100); |
---|
152 | if(!twice) |
---|
153 | { |
---|
154 | tg.startTone(ToneGenerator.TONE_PROP_BEEP); |
---|
155 | } |
---|
156 | else |
---|
157 | { |
---|
158 | //tg.startTone(ToneGenerator.TONE_PROP_BEEP); |
---|
159 | tg.startTone(ToneGenerator.TONE_PROP_ACK); |
---|
160 | } |
---|
161 | |
---|
162 | } |
---|
163 | |
---|
164 | public static String getHexString(byte[] b) throws Exception { |
---|
165 | String result = ""; |
---|
166 | for (int i=0; i < b.length; i++) { |
---|
167 | result += |
---|
168 | Integer.toString( ( b[i] & 0xff ) + 0x100, 16).substring( 1 ); |
---|
169 | } |
---|
170 | return result; |
---|
171 | } |
---|
172 | |
---|
173 | public void setBackground(int color, int time) |
---|
174 | { |
---|
175 | mScreen.setBackgroundColor(color); |
---|
176 | Handler handler = new Handler(); |
---|
177 | handler.postDelayed(new Runnable() { |
---|
178 | public void run() { |
---|
179 | mScreen.setBackgroundColor(android.R.color.white); |
---|
180 | } |
---|
181 | }, time); |
---|
182 | } |
---|
183 | @Override |
---|
184 | public void onPause() { |
---|
185 | super.onPause(); |
---|
186 | SharedPreferences preferences = getPreferences(MODE_PRIVATE); |
---|
187 | SharedPreferences.Editor editor = preferences.edit(); |
---|
188 | editor.putBoolean("writeAllow", writeAllow); // value to store |
---|
189 | editor.putString("cardID", ObjectSerializer.serialize(cardID)); |
---|
190 | editor.commit(); |
---|
191 | if (mAdapter != null) mAdapter.disableForegroundDispatch(this); |
---|
192 | } |
---|
193 | |
---|
194 | @Override |
---|
195 | public boolean onCreateOptionsMenu(Menu menu) { |
---|
196 | MenuInflater inflater = getMenuInflater(); |
---|
197 | inflater.inflate(R.menu.lo_menu, menu); |
---|
198 | return true; |
---|
199 | } |
---|
200 | @Override |
---|
201 | public boolean onOptionsItemSelected(MenuItem item) { |
---|
202 | // Handle item selection |
---|
203 | switch (item.getItemId()) { |
---|
204 | case R.id.add_uid: |
---|
205 | addUID(); |
---|
206 | return true; |
---|
207 | case R.id.add_note: |
---|
208 | addNote(); |
---|
209 | return true; |
---|
210 | case R.id.stopsend: |
---|
211 | stopSend(); |
---|
212 | return true; |
---|
213 | default: |
---|
214 | return super.onOptionsItemSelected(item); |
---|
215 | } |
---|
216 | } |
---|
217 | |
---|
218 | public void addUID() |
---|
219 | { |
---|
220 | if(writeAllow) |
---|
221 | { |
---|
222 | AlertDialog.Builder alert = new AlertDialog.Builder(this); |
---|
223 | alert.setTitle("UID Toevoegen"); |
---|
224 | |
---|
225 | // Set an EditText view to get user input |
---|
226 | final EditText input = new EditText(this); |
---|
227 | alert.setView(input); |
---|
228 | |
---|
229 | alert.setPositiveButton("Toevoegen", new DialogInterface.OnClickListener() { |
---|
230 | public void onClick(DialogInterface dialog, int whichButton) { |
---|
231 | String value = input.getText().toString(); |
---|
232 | fh.writeUID(value, DateFormat.getDateTimeInstance().format(new Date())); |
---|
233 | return; |
---|
234 | } |
---|
235 | }); |
---|
236 | |
---|
237 | alert.setNegativeButton("Annuleren", new DialogInterface.OnClickListener() { |
---|
238 | |
---|
239 | public void onClick(DialogInterface dialog, int which) { |
---|
240 | // TODO Auto-generated method stub |
---|
241 | return; |
---|
242 | } |
---|
243 | }); |
---|
244 | alert.show(); |
---|
245 | } |
---|
246 | else |
---|
247 | { |
---|
248 | AlertDialog.Builder alert = new AlertDialog.Builder(this, 4); |
---|
249 | alert.setTitle("Oeps!"); |
---|
250 | alert.setMessage("Druk op start om een UID toe te voegen"); |
---|
251 | alert.setNeutralButton("Ok", new DialogInterface.OnClickListener() { |
---|
252 | |
---|
253 | public void onClick(DialogInterface dialog, int which) { |
---|
254 | // TODO Auto-generated method stub |
---|
255 | return; |
---|
256 | } |
---|
257 | }); |
---|
258 | alert.show(); |
---|
259 | } |
---|
260 | } |
---|
261 | |
---|
262 | public void addNote() |
---|
263 | { |
---|
264 | if(writeAllow) |
---|
265 | { |
---|
266 | AlertDialog.Builder alert = new AlertDialog.Builder(this); |
---|
267 | alert.setTitle("Notitie Toevoegen"); |
---|
268 | |
---|
269 | // Set an EditText view to get user input |
---|
270 | final EditText input = new EditText(this); |
---|
271 | alert.setView(input); |
---|
272 | |
---|
273 | alert.setPositiveButton("Toevoegen", new DialogInterface.OnClickListener() { |
---|
274 | public void onClick(DialogInterface dialog, int whichButton) { |
---|
275 | String value = input.getText().toString(); |
---|
276 | fh.writeNote(value); |
---|
277 | return; |
---|
278 | } |
---|
279 | }); |
---|
280 | |
---|
281 | alert.setNegativeButton("Annuleren", new DialogInterface.OnClickListener() { |
---|
282 | |
---|
283 | public void onClick(DialogInterface dialog, int which) { |
---|
284 | // TODO Auto-generated method stub |
---|
285 | return; |
---|
286 | } |
---|
287 | }); |
---|
288 | alert.show(); |
---|
289 | } |
---|
290 | else |
---|
291 | { |
---|
292 | AlertDialog.Builder alert = new AlertDialog.Builder(this, 4); |
---|
293 | alert.setTitle("Oeps!"); |
---|
294 | alert.setMessage("Druk op start om een Notitie toe te voegen"); |
---|
295 | alert.setNeutralButton("Ok", new DialogInterface.OnClickListener() { |
---|
296 | |
---|
297 | public void onClick(DialogInterface dialog, int which) { |
---|
298 | // TODO Auto-generated method stub |
---|
299 | return; |
---|
300 | } |
---|
301 | }); |
---|
302 | alert.show(); |
---|
303 | } |
---|
304 | } |
---|
305 | public void stopSend() |
---|
306 | { |
---|
307 | writeAllow = false; |
---|
308 | ToggleButton tg = (ToggleButton) findViewById(R.id.status); |
---|
309 | tg.setChecked(writeAllow); |
---|
310 | AlertDialog.Builder alert = new AlertDialog.Builder(this); |
---|
311 | alert.setTitle("Email Zenden"); |
---|
312 | |
---|
313 | // Set an EditText view to get user input |
---|
314 | final EditText input = new EditText(this); |
---|
315 | input.setHint("Email"); |
---|
316 | input.setInputType(32); |
---|
317 | alert.setView(input); |
---|
318 | |
---|
319 | alert.setPositiveButton("Verder", new DialogInterface.OnClickListener() { |
---|
320 | public void onClick(DialogInterface dialog, int whichButton) { |
---|
321 | String value = input.getText().toString(); |
---|
322 | Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND); |
---|
323 | emailIntent.setType("text/xml"); |
---|
324 | emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, new String[] |
---|
325 | {value}); |
---|
326 | emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, |
---|
327 | "Tag Track from " + DateFormat.getDateTimeInstance().format(new Date())); |
---|
328 | emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, |
---|
329 | "XML Attached"); |
---|
330 | File root = new File(Environment.getExternalStorageDirectory(), "TagTracker"); |
---|
331 | File xmlfile = new File(root, "tags.xml"); |
---|
332 | emailIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse("file://"+ xmlfile)); |
---|
333 | startActivity(Intent.createChooser(emailIntent, "Stuur Email")); |
---|
334 | return; |
---|
335 | } |
---|
336 | }); |
---|
337 | alert.show(); |
---|
338 | } |
---|
339 | public void zenden() |
---|
340 | { |
---|
341 | if(!writeAllow) |
---|
342 | { |
---|
343 | AlertDialog.Builder alert = new AlertDialog.Builder(this); |
---|
344 | alert.setTitle("Email Zenden"); |
---|
345 | |
---|
346 | // Set an EditText view to get user input |
---|
347 | final EditText input = new EditText(this); |
---|
348 | input.setHint("Email"); |
---|
349 | input.setInputType(32); |
---|
350 | alert.setView(input); |
---|
351 | |
---|
352 | alert.setPositiveButton("Verder", new DialogInterface.OnClickListener() { |
---|
353 | public void onClick(DialogInterface dialog, int whichButton) { |
---|
354 | String value = input.getText().toString(); |
---|
355 | Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND); |
---|
356 | emailIntent.setType("text/xml"); |
---|
357 | emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, new String[] |
---|
358 | {value}); |
---|
359 | emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, |
---|
360 | "Tag Track from " + DateFormat.getDateTimeInstance().format(new Date())); |
---|
361 | emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, |
---|
362 | "XML Attached"); |
---|
363 | File root = new File(Environment.getExternalStorageDirectory(), "TagTracker"); |
---|
364 | File xmlfile = new File(root, "tags.xml"); |
---|
365 | emailIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse("file://"+ xmlfile)); |
---|
366 | startActivity(Intent.createChooser(emailIntent, "Stuur Email")); |
---|
367 | return; |
---|
368 | } |
---|
369 | }); |
---|
370 | alert.show(); |
---|
371 | } |
---|
372 | else |
---|
373 | { |
---|
374 | AlertDialog.Builder alert = new AlertDialog.Builder(this, 4); |
---|
375 | alert.setTitle("Oeps!"); |
---|
376 | alert.setMessage("Druk op stop om te verzenden"); |
---|
377 | alert.setNeutralButton("Ok", new DialogInterface.OnClickListener() { |
---|
378 | |
---|
379 | public void onClick(DialogInterface dialog, int which) { |
---|
380 | // TODO Auto-generated method stub |
---|
381 | return; |
---|
382 | } |
---|
383 | }); |
---|
384 | alert.show(); |
---|
385 | } |
---|
386 | } |
---|
387 | } |
---|