NFCSync.java 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /*
  2. * Copyright (C) 2011 The Android Open Source Project
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package de.tudarmstadt.informatik.hostage.sync;
  17. import java.io.ByteArrayInputStream;
  18. import java.io.ByteArrayOutputStream;
  19. import java.io.IOException;
  20. import java.io.ObjectInputStream;
  21. import java.io.ObjectOutputStream;
  22. import java.util.ArrayList;
  23. import java.util.HashMap;
  24. import android.annotation.TargetApi;
  25. import android.app.Activity;
  26. import android.content.Intent;
  27. import android.nfc.NdefMessage;
  28. import android.nfc.NdefRecord;
  29. import android.nfc.NfcAdapter;
  30. import android.nfc.NfcAdapter.CreateNdefMessageCallback;
  31. import android.nfc.NfcAdapter.OnNdefPushCompleteCallback;
  32. import android.nfc.NfcEvent;
  33. import android.os.Build;
  34. import android.os.Bundle;
  35. import android.os.Handler;
  36. import android.os.Message;
  37. import android.os.Parcelable;
  38. import android.util.Log;
  39. import android.widget.TextView;
  40. import android.widget.Toast;
  41. import de.tudarmstadt.informatik.hostage.R;
  42. import de.tudarmstadt.informatik.hostage.persistence.HostageDBOpenHelper;
  43. @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
  44. public class NFCSync extends Activity implements CreateNdefMessageCallback, OnNdefPushCompleteCallback {
  45. NfcAdapter mNfcAdapter;
  46. TextView mInfoText;
  47. private static final int MESSAGE_SENT = 1;
  48. /** This handler receives a message from onNdefPushComplete */
  49. private final Handler mHandler = new Handler() {
  50. @Override
  51. public void handleMessage(Message msg) {
  52. switch (msg.what) {
  53. case MESSAGE_SENT:
  54. Toast.makeText(getApplicationContext(), "Message sent!", Toast.LENGTH_LONG).show();
  55. Log.i("NFC", "Message sent!");
  56. break;
  57. }
  58. }
  59. };
  60. /**
  61. * Implementation for the CreateNdefMessageCallback interface
  62. */
  63. @Override
  64. public NdefMessage createNdefMessage(NfcEvent event) {
  65. // Get Networkdata
  66. HostageDBOpenHelper dbh = new HostageDBOpenHelper(this);
  67. ArrayList<HashMap<String, Object>> localNetworkInformation = dbh.getNetworkInformation();
  68. Log.i("NFC", "Creating Message");
  69. NdefMessage msg = null;
  70. try {
  71. msg = new NdefMessage(NdefRecord.createMime("application/de.tudarmstadt.informatik.hostage", serialize(localNetworkInformation))
  72. );
  73. } catch (IOException e) {
  74. e.printStackTrace();
  75. }
  76. return msg;
  77. }
  78. @Override
  79. public void onCreate(Bundle savedInstanceState) {
  80. super.onCreate(savedInstanceState);
  81. setContentView(R.layout.activity_nfc);
  82. mInfoText = (TextView) findViewById(R.id.textView);
  83. // Check for available NFC Adapter
  84. mNfcAdapter = NfcAdapter.getDefaultAdapter(this);
  85. if (mNfcAdapter == null) {
  86. mInfoText.setText("NFC is not available on this device.");
  87. } else {
  88. mInfoText.setText("Hold phones together to synchronize.");
  89. // Register callback to set NDEF message
  90. mNfcAdapter.setNdefPushMessageCallback(this, this);
  91. // Register callback to listen for message-sent success
  92. mNfcAdapter.setOnNdefPushCompleteCallback(this, this);
  93. }
  94. }
  95. /**
  96. * Implementation for the OnNdefPushCompleteCallback interface
  97. */
  98. @Override
  99. public void onNdefPushComplete(NfcEvent arg0) {
  100. // A handler is needed to send messages to the activity when this
  101. // callback occurs, because it happens from a binder thread
  102. mHandler.obtainMessage(MESSAGE_SENT).sendToTarget();
  103. }
  104. @Override
  105. public void onNewIntent(Intent intent) {
  106. // onResume gets called after this to handle the intent
  107. setIntent(intent);
  108. }
  109. // HELPER
  110. @Override
  111. public void onResume() {
  112. super.onResume();
  113. // Check to see that the Activity started due to an Android Beam
  114. if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(getIntent().getAction())) {
  115. processIntent(getIntent());
  116. }
  117. }
  118. /**
  119. * Parses the NDEF Message from the intent and prints to the TextView
  120. */
  121. void processIntent(Intent intent) {
  122. Parcelable[] rawMsgs = intent.getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES);
  123. // only one message sent during the beam
  124. NdefMessage msg = (NdefMessage) rawMsgs[0];
  125. // record 0 contains the MIME type, record 1 is the AAR, if present
  126. Object object;
  127. Log.i("NFC", "Getting Message!");
  128. try {
  129. object = deserialize(msg.getRecords()[0].getPayload());
  130. ArrayList<HashMap<String, Object>> remoteNetworkInformation = (ArrayList<HashMap<String, Object>>) object;
  131. HostageDBOpenHelper dbh = new HostageDBOpenHelper(this);
  132. dbh.updateNetworkInformation(remoteNetworkInformation);
  133. } catch (ClassNotFoundException e) {
  134. e.printStackTrace();
  135. } catch (IOException e) {
  136. e.printStackTrace();
  137. }
  138. }
  139. private static Object deserialize(byte[] data) throws IOException, ClassNotFoundException {
  140. ByteArrayInputStream in = new ByteArrayInputStream(data);
  141. ObjectInputStream is = new ObjectInputStream(in);
  142. return is.readObject();
  143. }
  144. private static byte[] serialize(Object obj) throws IOException {
  145. ByteArrayOutputStream out = new ByteArrayOutputStream();
  146. ObjectOutputStream os = new ObjectOutputStream(out);
  147. os.writeObject(obj);
  148. return out.toByteArray();
  149. }
  150. }