Browse Source

preventing third device connection.

Julien Clauter 9 years ago
parent
commit
808acbdcec

+ 25 - 8
src/de/tudarmstadt/informatik/hostage/sync/wifi_direct/ui/WiFiP2pSyncActivity.java

@@ -65,6 +65,7 @@ public class WiFiP2pSyncActivity extends Activity implements AdapterView.OnItemC
 
     private SyncClientTask clientTask;
     private SyncHostTask  hostTask;
+    private BackgroundTask executingTask;
     private boolean isHost;
 
     private WiFiP2pEventHandler _wifiEventHandler = null;
@@ -274,12 +275,16 @@ public class WiFiP2pSyncActivity extends Activity implements AdapterView.OnItemC
                 public void didSucceed() {
                     Toast.makeText(this.activity, SYNCHRONIZATION_COMPLETE_MESSAGE , Toast.LENGTH_SHORT).show();
                     this.activity.wifiEventHandler().disconnect();
+                    this.activity.hostTask = null;
+                    this.activity.clientTask = null;
                 }
 
                 @Override
                 public void didFail() {
                     Toast.makeText(this.activity, SYNCHRONIZATION_FAILED_MESSAGE, Toast.LENGTH_LONG).show();
                     this.activity.wifiEventHandler().disconnect();
+                    this.activity.hostTask = null;
+                    this.activity.clientTask = null;
                 }
             }.init(this);
         }
@@ -329,10 +334,16 @@ public class WiFiP2pSyncActivity extends Activity implements AdapterView.OnItemC
      */
     private void startHost()
     {
-        Log.d("WiFiP2pSyncActivity", "Starting HOST Task" + ".");
-        //Toast.makeText(this, PERFORMING_TASK_AS_HOST , Toast.LENGTH_SHORT).show();
-        this.hostTask = new SyncHostTask(this.ownDevice, this.syncCompletionListener(), getApplicationContext());
-        this.hostTask.execute();
+        if (this.hostTask == null){
+            Log.d("WiFiP2pSyncActivity", "Starting HOST Task" + ".");
+            //Toast.makeText(this, PERFORMING_TASK_AS_HOST , Toast.LENGTH_SHORT).show();
+            this.hostTask = new SyncHostTask(this.ownDevice, this.syncCompletionListener(), getApplicationContext());
+            this.executingTask = this.hostTask;
+            this.hostTask.execute();
+        } else {
+            Log.d("WiFiP2pSyncActivity", "Preventing third device for any syncing" + ".");
+        }
+
     }
 
     /**
@@ -341,10 +352,16 @@ public class WiFiP2pSyncActivity extends Activity implements AdapterView.OnItemC
      */
     private void startClient(WifiP2pInfo info)
     {
-        Log.d("WiFiP2pSyncActivity", "Starting CLIENT Task" + ".");
-        //Toast.makeText(this, PERFORMING_TASK_AS_CLIENT, Toast.LENGTH_SHORT).show();
-        this.clientTask = new SyncClientTask( info.groupOwnerAddress.getHostAddress(),this.ownDevice, this.syncCompletionListener(), getApplicationContext() );
-        this.clientTask.execute();
+        if (this.clientTask == null){
+            Log.d("WiFiP2pSyncActivity", "Starting CLIENT Task" + ".");
+            //Toast.makeText(this, PERFORMING_TASK_AS_CLIENT, Toast.LENGTH_SHORT).show();
+            this.clientTask = new SyncClientTask( info.groupOwnerAddress.getHostAddress(),this.ownDevice, this.syncCompletionListener(), getApplicationContext() );
+            this.executingTask = this.clientTask;
+            this.clientTask.execute();
+        } else {
+            Log.d("WiFiP2pSyncActivity", "Preventing third device for syncing" + ".");
+        }
+
     }
 
     /**