Browse Source

Merge branch 'master' of https://wulf.pfeiffer@git.tk.informatik.tu-darmstadt.de/scm-ssi-student-hostage.git

Wulf Pfeiffer 10 years ago
parent
commit
5663929b97

+ 4 - 4
AndroidManifest.xml

@@ -47,16 +47,16 @@
             android:name="de.tudarmstadt.informatik.hostage.ui2.activity.MainActivity"
             android:configChanges="keyboardHidden|orientation|screenSize"
             android:label="@string/app_name"
-            android:screenOrientation="portrait" > 
+            android:screenOrientation="portrait" >  
             <intent-filter>
                 <action android:name="android.intent.action.MAIN" />
                 <category android:name="android.intent.category.LAUNCHER" />
-            </intent-filter>      
+            </intent-filter>     
         </activity>
         <activity
             android:name="de.tudarmstadt.informatik.hostage.ui.MainActivity"
             android:configChanges="keyboardHidden|orientation|screenSize"
-            android:label="@string/app_name" >          
+            android:label="@string/app_name" >            
         </activity>
         <activity
             android:name="de.tudarmstadt.informatik.hostage.ui.ViewLog"
@@ -93,7 +93,7 @@
             android:theme="@android:style/Theme.Dialog" >
         </activity>
         <activity
-            android:name="de.tudarmstadt.informatik.hostage.sync.nfc.NFCSync"
+            android:name="de.tudarmstadt.informatik.hostage.sync.nfc.NFCSyncActivity"
             android:label="@string/gui_nfc"
             android:theme="@android:style/Theme.Dialog" >
             <intent-filter>

+ 51 - 25
src/de/tudarmstadt/informatik/hostage/sync/nfc/NFCSync.java → src/de/tudarmstadt/informatik/hostage/sync/nfc/NFCSyncActivity.java

@@ -27,13 +27,16 @@ import java.util.Iterator;
 
 import android.annotation.TargetApi;
 import android.app.Activity;
+import android.app.PendingIntent;
 import android.content.Intent;
+import android.content.IntentFilter;
 import android.nfc.NdefMessage;
 import android.nfc.NdefRecord;
 import android.nfc.NfcAdapter;
 import android.nfc.NfcAdapter.CreateNdefMessageCallback;
 import android.nfc.NfcAdapter.OnNdefPushCompleteCallback;
 import android.nfc.NfcEvent;
+import android.nfc.tech.NfcF;
 import android.os.Build;
 import android.os.Bundle;
 import android.os.Handler;
@@ -49,23 +52,30 @@ 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 {
+public class NFCSyncActivity extends Activity implements CreateNdefMessageCallback, OnNdefPushCompleteCallback {
 	NfcAdapter mNfcAdapter;
 	TextView mInfoText;
 	private static final int MESSAGE_SENT = 0x1;
-	private static final int SYNC_SUCCESSFUL = 0x2;
+	private static final int MESSAGE_RECEIVED = 0x3;
+	
 
 	private final Handler mHandler = new Handler() {
 		@Override
 		public void handleMessage(Message msg) {
 			switch (msg.what) {
-			case MESSAGE_SENT:
-				mInfoText.setText("Synchronization data sent, wait for data.");
-				Log.i("NFC", "Message sent!");
+			case MESSAGE_SENT:				
+				runOnUiThread(new Runnable() {
+					public void run() {
+		            	Toast.makeText(NFCSyncActivity.this, "Data sent!", Toast.LENGTH_LONG).show();
+					}
+		        });
 				break;
-			case SYNC_SUCCESSFUL:
-				mInfoText.setText("Synchronization successfull!");
-				Log.i("NFC", "Message sent!");
+			case MESSAGE_RECEIVED:
+				runOnUiThread(new Runnable() {
+					public void run() {
+		            	Toast.makeText(NFCSyncActivity.this, "Data recieved!", Toast.LENGTH_LONG).show();
+					}
+		        });				
 				break;
 			}
 		}
@@ -84,7 +94,7 @@ public class NFCSync extends Activity implements CreateNdefMessageCallback, OnNd
 		} else if(!mNfcAdapter.isEnabled()){
 			mInfoText.setText("Enable Android Beam before synchronizing.");
 		} else {
-			mInfoText.setText("Hold phones together to synchronize.");
+			mInfoText.setText("Hold phones together to send or recieve data.");
 			// Register callback to set NDEF message
 			mNfcAdapter.setNdefPushMessageCallback(this, this);
 			// Register callback to listen for message-sent success
@@ -103,7 +113,6 @@ public class NFCSync extends Activity implements CreateNdefMessageCallback, OnNd
 		HashMap<String, Long> devices_local = dbh.getSyncDevices();
 		ArrayList<SyncInfoRecord> syncInfo = dbh.getSyncInfo();
 		
-		Log.i("NFC", "Creating Message");
 		NdefMessage msg = null;
 		try {
 			NdefRecord netData = NdefRecord.createMime("application/de.tudarmstadt.informatik.hostage", serialize(localNetworkInformation));
@@ -138,10 +147,35 @@ public class NFCSync extends Activity implements CreateNdefMessageCallback, OnNd
 	@Override
 	public void onResume() {
 		super.onResume();
+		IntentFilter[] mIntentFilters = null;
+			
+	    PendingIntent mPendingIntent = PendingIntent.getActivity(this, 0, 
+	    		new Intent(this, getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);
+ 
+        // set an intent filter for all MIME data
+        IntentFilter ndefIntent = new IntentFilter(NfcAdapter.ACTION_NDEF_DISCOVERED);
+        try {
+            ndefIntent.addDataType("*/*");
+            mIntentFilters = new IntentFilter[] { ndefIntent };
+        } catch (Exception e) {
+            Log.e("TagDispatch", e.toString());
+        }
+ 
+        String[][] mNFCTechLists = new String[][] { new String[] { NfcF.class.getName() } };
+        
+        if (mNfcAdapter != null)
+            mNfcAdapter.enableForegroundDispatch(this, mPendingIntent, mIntentFilters, mNFCTechLists);
+	
 		// Check to see that the Activity started due to an Android Beam
 		if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(getIntent().getAction())) {
 			processIntent(getIntent());
-		}
+		}		
+	}
+	
+	@Override
+	public void onPause() {
+		super.onPause();
+		mNfcAdapter.disableForegroundDispatch(this);
 	}
 
 	/**
@@ -151,20 +185,13 @@ public class NFCSync extends Activity implements CreateNdefMessageCallback, OnNd
 		Parcelable[] rawMsgs = intent.getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES);
 		// 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 netData;
-		Object deviceData;
-		Object syncData;
-		Log.i("NFC", "Getting Message!");
 		try {
 			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;
+
+			ArrayList<NetworkRecord> remoteNetworkInformation = (ArrayList<NetworkRecord>) deserialize(msg.getRecords()[0].getPayload());
+			HashMap<String, Long> devices_remote = (HashMap<String, Long>) deserialize(msg.getRecords()[1].getPayload());
 			HashMap<String, Long> devices_local = dbh.getSyncDevices();
-			ArrayList<SyncInfoRecord> syncInfo = (ArrayList<SyncInfoRecord>) syncData;
+			ArrayList<SyncInfoRecord> syncInfo = (ArrayList<SyncInfoRecord>) deserialize(msg.getRecords()[2].getPayload());
 			
 			long tracing_timestamp = 0;
 			if(devices_local.containsKey(TracingSyncService.REMOTE_DEVICE))
@@ -177,11 +204,10 @@ public class NFCSync extends Activity implements CreateNdefMessageCallback, OnNd
 					i.remove();
 				}
 			}
-
 			
 			for ( Iterator<SyncInfoRecord> i = syncInfo.iterator(); i.hasNext(); ){
 				SyncInfoRecord info = i.next();
-				if(devices_remote.containsKey(info.getDeviceID())){
+				if(!devices_remote.containsKey(info.getDeviceID())){
 					i.remove();
 				}				    
 			}	
@@ -189,7 +215,7 @@ public class NFCSync extends Activity implements CreateNdefMessageCallback, OnNd
 			dbh.updateSyncDevices(devices_remote);
 			dbh.updateSyncInfo(syncInfo);
 			dbh.updateNetworkInformation(remoteNetworkInformation);
-			mHandler.obtainMessage(SYNC_SUCCESSFUL).sendToTarget();
+			mHandler.obtainMessage(MESSAGE_RECEIVED).sendToTarget();
 		} catch (ClassNotFoundException e) {
 			e.printStackTrace();
 		} catch (IOException e) {

+ 2 - 2
src/de/tudarmstadt/informatik/hostage/ui/PlayGroundActivity.java

@@ -16,7 +16,7 @@ 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.bluetooth.BluetoothSyncActivity;
-import de.tudarmstadt.informatik.hostage.sync.nfc.NFCSync;
+import de.tudarmstadt.informatik.hostage.sync.nfc.NFCSyncActivity;
 import de.tudarmstadt.informatik.hostage.sync.tracing.TracingSyncActivity;
 
 public class PlayGroundActivity extends Activity {
@@ -47,7 +47,7 @@ public class PlayGroundActivity extends Activity {
 	}
 
 	public void startNFC(View view) {
-		startActivity(new Intent(this, NFCSync.class));
+		startActivity(new Intent(this, NFCSyncActivity.class));
 	}
 
 	public void startBluetooth(View view) {