|
@@ -22,6 +22,9 @@ import java.io.IOException;
|
|
|
import java.io.ObjectInputStream;
|
|
|
import java.io.ObjectOutputStream;
|
|
|
import java.util.ArrayList;
|
|
|
+import java.util.HashMap;
|
|
|
+import java.util.Iterator;
|
|
|
+
|
|
|
import android.annotation.TargetApi;
|
|
|
import android.app.Activity;
|
|
|
import android.content.Intent;
|
|
@@ -41,20 +44,27 @@ import android.widget.TextView;
|
|
|
import android.widget.Toast;
|
|
|
import de.tudarmstadt.informatik.hostage.R;
|
|
|
import de.tudarmstadt.informatik.hostage.logging.NetworkRecord;
|
|
|
+import de.tudarmstadt.informatik.hostage.logging.SyncInfoRecord;
|
|
|
import de.tudarmstadt.informatik.hostage.persistence.HostageDBOpenHelper;
|
|
|
+import de.tudarmstadt.informatik.hostage.sync.tracing.TracingSyncService;
|
|
|
|
|
|
@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
|
|
|
public class NFCSync extends Activity implements CreateNdefMessageCallback, OnNdefPushCompleteCallback {
|
|
|
NfcAdapter mNfcAdapter;
|
|
|
TextView mInfoText;
|
|
|
- private static final int MESSAGE_SENT = 1;
|
|
|
+ private static final int MESSAGE_SENT = 0x1;
|
|
|
+ private static final int SYNC_SUCCESSFUL = 0x2;
|
|
|
|
|
|
private final Handler mHandler = new Handler() {
|
|
|
@Override
|
|
|
public void handleMessage(Message msg) {
|
|
|
switch (msg.what) {
|
|
|
case MESSAGE_SENT:
|
|
|
- Toast.makeText(getApplicationContext(), "Message sent!", Toast.LENGTH_LONG).show();
|
|
|
+ mInfoText.setText("Synchronization data sent, wait for data.");
|
|
|
+ Log.i("NFC", "Message sent!");
|
|
|
+ break;
|
|
|
+ case SYNC_SUCCESSFUL:
|
|
|
+ mInfoText.setText("Synchronization successfull!");
|
|
|
Log.i("NFC", "Message sent!");
|
|
|
break;
|
|
|
}
|
|
@@ -71,6 +81,8 @@ public class NFCSync extends Activity implements CreateNdefMessageCallback, OnNd
|
|
|
mNfcAdapter = NfcAdapter.getDefaultAdapter(this);
|
|
|
if (mNfcAdapter == null) {
|
|
|
mInfoText.setText("NFC is not available on this device.");
|
|
|
+ } else if(!mNfcAdapter.isEnabled()){
|
|
|
+ mInfoText.setText("Enable Android Beam before synchronizing.");
|
|
|
} else {
|
|
|
mInfoText.setText("Hold phones together to synchronize.");
|
|
|
// Register callback to set NDEF message
|
|
@@ -88,12 +100,17 @@ public class NFCSync extends Activity implements CreateNdefMessageCallback, OnNd
|
|
|
// Get Networkdata
|
|
|
HostageDBOpenHelper dbh = new HostageDBOpenHelper(this);
|
|
|
ArrayList<NetworkRecord> localNetworkInformation = dbh.getNetworkInformation();
|
|
|
+ HashMap<String, Long> devices_local = dbh.getSyncDevices();
|
|
|
+ ArrayList<SyncInfoRecord> syncInfo = dbh.getSyncInfo();
|
|
|
+
|
|
|
Log.i("NFC", "Creating Message");
|
|
|
NdefMessage msg = null;
|
|
|
try {
|
|
|
- msg = new NdefMessage(NdefRecord.createMime("application/de.tudarmstadt.informatik.hostage", serialize(localNetworkInformation))
|
|
|
+ NdefRecord netData = NdefRecord.createMime("application/de.tudarmstadt.informatik.hostage", serialize(localNetworkInformation));
|
|
|
+ NdefRecord deviceData = NdefRecord.createMime("application/de.tudarmstadt.informatik.hostage", serialize(devices_local));
|
|
|
+ NdefRecord syncData = NdefRecord.createMime("application/de.tudarmstadt.informatik.hostage", serialize(syncInfo));
|
|
|
+ msg = new NdefMessage(netData, deviceData, syncData);
|
|
|
|
|
|
- );
|
|
|
} catch (IOException e) {
|
|
|
e.printStackTrace();
|
|
|
}
|
|
@@ -135,13 +152,44 @@ public class NFCSync extends Activity implements CreateNdefMessageCallback, OnNd
|
|
|
// only one message sent during the beam
|
|
|
NdefMessage msg = (NdefMessage) rawMsgs[0];
|
|
|
// record 0 contains the MIME type, record 1 is the AAR, if present
|
|
|
- Object object;
|
|
|
+ Object netData;
|
|
|
+ Object deviceData;
|
|
|
+ Object syncData;
|
|
|
Log.i("NFC", "Getting Message!");
|
|
|
try {
|
|
|
- object = deserialize(msg.getRecords()[0].getPayload());
|
|
|
- ArrayList<NetworkRecord> remoteNetworkInformation = (ArrayList<NetworkRecord>) object;
|
|
|
HostageDBOpenHelper dbh = new HostageDBOpenHelper(this);
|
|
|
+ netData = deserialize(msg.getRecords()[0].getPayload());
|
|
|
+ deviceData = deserialize(msg.getRecords()[1].getPayload());
|
|
|
+ syncData = deserialize(msg.getRecords()[2].getPayload());
|
|
|
+ ArrayList<NetworkRecord> remoteNetworkInformation = (ArrayList<NetworkRecord>) netData;
|
|
|
+ HashMap<String, Long> devices_remote = (HashMap<String, Long>) deviceData;
|
|
|
+ HashMap<String, Long> devices_local = dbh.getSyncDevices();
|
|
|
+ ArrayList<SyncInfoRecord> syncInfo = (ArrayList<SyncInfoRecord>) syncData;
|
|
|
+
|
|
|
+ long tracing_timestamp = 0;
|
|
|
+ if(devices_local.containsKey(TracingSyncService.REMOTE_DEVICE))
|
|
|
+ tracing_timestamp = devices_local.get(TracingSyncService.REMOTE_DEVICE);
|
|
|
+
|
|
|
+ for(Iterator<String> i = devices_remote.keySet().iterator(); i.hasNext(); ){
|
|
|
+ String key = i.next();
|
|
|
+ if((devices_local.containsKey(key) && devices_local.get(key) >= devices_remote.get(key))
|
|
|
+ || (tracing_timestamp > devices_remote.get(key))){
|
|
|
+ i.remove();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ for ( Iterator<SyncInfoRecord> i = syncInfo.iterator(); i.hasNext(); ){
|
|
|
+ SyncInfoRecord info = i.next();
|
|
|
+ if(devices_remote.containsKey(info.getDeviceID())){
|
|
|
+ i.remove();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ dbh.updateSyncDevices(devices_remote);
|
|
|
+ dbh.updateSyncInfo(syncInfo);
|
|
|
dbh.updateNetworkInformation(remoteNetworkInformation);
|
|
|
+ mHandler.obtainMessage(SYNC_SUCCESSFUL).sendToTarget();
|
|
|
} catch (ClassNotFoundException e) {
|
|
|
e.printStackTrace();
|
|
|
} catch (IOException e) {
|