NFCSync.java 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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.nfc;
  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 java.util.Iterator;
  25. import android.annotation.TargetApi;
  26. import android.app.Activity;
  27. import android.content.Intent;
  28. import android.nfc.NdefMessage;
  29. import android.nfc.NdefRecord;
  30. import android.nfc.NfcAdapter;
  31. import android.nfc.NfcAdapter.CreateNdefMessageCallback;
  32. import android.nfc.NfcAdapter.OnNdefPushCompleteCallback;
  33. import android.nfc.NfcEvent;
  34. import android.os.Build;
  35. import android.os.Bundle;
  36. import android.os.Handler;
  37. import android.os.Message;
  38. import android.os.Parcelable;
  39. import android.util.Log;
  40. import android.widget.TextView;
  41. import android.widget.Toast;
  42. import de.tudarmstadt.informatik.hostage.R;
  43. import de.tudarmstadt.informatik.hostage.logging.NetworkRecord;
  44. import de.tudarmstadt.informatik.hostage.logging.SyncInfoRecord;
  45. import de.tudarmstadt.informatik.hostage.persistence.HostageDBOpenHelper;
  46. import de.tudarmstadt.informatik.hostage.sync.tracing.TracingSyncService;
  47. @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
  48. public class NFCSync extends Activity implements CreateNdefMessageCallback, OnNdefPushCompleteCallback {
  49. NfcAdapter mNfcAdapter;
  50. TextView mInfoText;
  51. private static final int MESSAGE_SENT = 0x1;
  52. private static final int SYNC_SUCCESSFUL = 0x2;
  53. private final Handler mHandler = new Handler() {
  54. @Override
  55. public void handleMessage(Message msg) {
  56. switch (msg.what) {
  57. case MESSAGE_SENT:
  58. mInfoText.setText("Synchronization data sent, wait for data.");
  59. Log.i("NFC", "Message sent!");
  60. break;
  61. case SYNC_SUCCESSFUL:
  62. mInfoText.setText("Synchronization successfull!");
  63. Log.i("NFC", "Message sent!");
  64. break;
  65. }
  66. }
  67. };
  68. @Override
  69. public void onCreate(Bundle savedInstanceState) {
  70. super.onCreate(savedInstanceState);
  71. setContentView(R.layout.activity_nfc);
  72. mInfoText = (TextView) findViewById(R.id.nfc_text_view);
  73. // Check for available NFC Adapter
  74. mNfcAdapter = NfcAdapter.getDefaultAdapter(this);
  75. if (mNfcAdapter == null) {
  76. mInfoText.setText("NFC is not available on this device.");
  77. } else if(!mNfcAdapter.isEnabled()){
  78. mInfoText.setText("Enable Android Beam before synchronizing.");
  79. } else {
  80. mInfoText.setText("Hold phones together to synchronize.");
  81. // Register callback to set NDEF message
  82. mNfcAdapter.setNdefPushMessageCallback(this, this);
  83. // Register callback to listen for message-sent success
  84. mNfcAdapter.setOnNdefPushCompleteCallback(this, this);
  85. }
  86. }
  87. /**
  88. * Implementation for the CreateNdefMessageCallback interface
  89. */
  90. @Override
  91. public NdefMessage createNdefMessage(NfcEvent event) {
  92. // Get Networkdata
  93. HostageDBOpenHelper dbh = new HostageDBOpenHelper(this);
  94. ArrayList<NetworkRecord> localNetworkInformation = dbh.getNetworkInformation();
  95. HashMap<String, Long> devices_local = dbh.getSyncDevices();
  96. ArrayList<SyncInfoRecord> syncInfo = dbh.getSyncInfo();
  97. Log.i("NFC", "Creating Message");
  98. NdefMessage msg = null;
  99. try {
  100. NdefRecord netData = NdefRecord.createMime("application/de.tudarmstadt.informatik.hostage", serialize(localNetworkInformation));
  101. NdefRecord deviceData = NdefRecord.createMime("application/de.tudarmstadt.informatik.hostage", serialize(devices_local));
  102. NdefRecord syncData = NdefRecord.createMime("application/de.tudarmstadt.informatik.hostage", serialize(syncInfo));
  103. msg = new NdefMessage(netData, deviceData, syncData);
  104. } catch (IOException e) {
  105. e.printStackTrace();
  106. }
  107. return msg;
  108. }
  109. /**
  110. * Implementation for the OnNdefPushCompleteCallback interface
  111. */
  112. @Override
  113. public void onNdefPushComplete(NfcEvent arg0) {
  114. // A handler is needed to send messages to the activity when this
  115. // callback occurs, because it happens from a binder thread
  116. mHandler.obtainMessage(MESSAGE_SENT).sendToTarget();
  117. }
  118. @Override
  119. public void onNewIntent(Intent intent) {
  120. // onResume gets called after this to handle the intent
  121. setIntent(intent);
  122. }
  123. // HELPER
  124. @Override
  125. public void onResume() {
  126. super.onResume();
  127. // Check to see that the Activity started due to an Android Beam
  128. if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(getIntent().getAction())) {
  129. processIntent(getIntent());
  130. }
  131. }
  132. /**
  133. * Parses the NDEF Message from the intent and prints to the TextView
  134. */
  135. void processIntent(Intent intent) {
  136. Parcelable[] rawMsgs = intent.getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES);
  137. // only one message sent during the beam
  138. NdefMessage msg = (NdefMessage) rawMsgs[0];
  139. // record 0 contains the MIME type, record 1 is the AAR, if present
  140. Object netData;
  141. Object deviceData;
  142. Object syncData;
  143. Log.i("NFC", "Getting Message!");
  144. try {
  145. HostageDBOpenHelper dbh = new HostageDBOpenHelper(this);
  146. netData = deserialize(msg.getRecords()[0].getPayload());
  147. deviceData = deserialize(msg.getRecords()[1].getPayload());
  148. syncData = deserialize(msg.getRecords()[2].getPayload());
  149. ArrayList<NetworkRecord> remoteNetworkInformation = (ArrayList<NetworkRecord>) netData;
  150. HashMap<String, Long> devices_remote = (HashMap<String, Long>) deviceData;
  151. HashMap<String, Long> devices_local = dbh.getSyncDevices();
  152. ArrayList<SyncInfoRecord> syncInfo = (ArrayList<SyncInfoRecord>) syncData;
  153. long tracing_timestamp = 0;
  154. if(devices_local.containsKey(TracingSyncService.REMOTE_DEVICE))
  155. tracing_timestamp = devices_local.get(TracingSyncService.REMOTE_DEVICE);
  156. for(Iterator<String> i = devices_remote.keySet().iterator(); i.hasNext(); ){
  157. String key = i.next();
  158. if((devices_local.containsKey(key) && devices_local.get(key) >= devices_remote.get(key))
  159. || (tracing_timestamp > devices_remote.get(key))){
  160. i.remove();
  161. }
  162. }
  163. for ( Iterator<SyncInfoRecord> i = syncInfo.iterator(); i.hasNext(); ){
  164. SyncInfoRecord info = i.next();
  165. if(devices_remote.containsKey(info.getDeviceID())){
  166. i.remove();
  167. }
  168. }
  169. dbh.updateSyncDevices(devices_remote);
  170. dbh.updateSyncInfo(syncInfo);
  171. dbh.updateNetworkInformation(remoteNetworkInformation);
  172. mHandler.obtainMessage(SYNC_SUCCESSFUL).sendToTarget();
  173. } catch (ClassNotFoundException e) {
  174. e.printStackTrace();
  175. } catch (IOException e) {
  176. e.printStackTrace();
  177. }
  178. }
  179. private static Object deserialize(byte[] data) throws IOException, ClassNotFoundException {
  180. ByteArrayInputStream in = new ByteArrayInputStream(data);
  181. ObjectInputStream is = new ObjectInputStream(in);
  182. return is.readObject();
  183. }
  184. private static byte[] serialize(Object obj) throws IOException {
  185. ByteArrayOutputStream out = new ByteArrayOutputStream();
  186. ObjectOutputStream os = new ObjectOutputStream(out);
  187. os.writeObject(obj);
  188. return out.toByteArray();
  189. }
  190. }