diff --git a/src/com/limelight/AddComputerManually.java b/src/com/limelight/AddComputerManually.java index 47eb3f2c..c4ca148d 100644 --- a/src/com/limelight/AddComputerManually.java +++ b/src/com/limelight/AddComputerManually.java @@ -2,6 +2,7 @@ package com.limelight; import java.net.InetAddress; import java.net.UnknownHostException; +import java.util.concurrent.LinkedBlockingQueue; import com.limelight.computers.ComputerManagerService; import com.limelight.utils.Dialog; @@ -22,51 +23,86 @@ import android.widget.Toast; public class AddComputerManually extends Activity { private Button addPcButton; private TextView hostText; + private ComputerManagerService.ComputerManagerBinder managerBinder; + private LinkedBlockingQueue computersToAdd = new LinkedBlockingQueue(); + private Thread addThread; private ServiceConnection serviceConnection = new ServiceConnection() { public void onServiceConnected(ComponentName className, final IBinder binder) { - new Thread() { - @Override - public void run() { - String msg; - boolean finish = false; - try { - InetAddress addr = InetAddress.getByName(hostText.getText().toString()); - - if (!((ComputerManagerService.ComputerManagerBinder)binder).addComputerBlocking(addr)){ - msg = "Unable to connect to the specified computer. Make sure the required ports are allowed through the firewall."; - } - else { - msg = "Successfully added computer"; - finish = true; - } - } catch (UnknownHostException e) { - msg = "Unable to resolve PC address. Make sure you didn't make a typo in the address."; - } - - final boolean toastFinish = finish; - final String toastMsg = msg; - AddComputerManually.this.runOnUiThread(new Runnable() { - @Override - public void run() { - // Unbind from this service - unbindService(AddComputerManually.this.serviceConnection); - - Toast.makeText(AddComputerManually.this, toastMsg, Toast.LENGTH_LONG).show(); - - if (toastFinish) { - // Close the activity - AddComputerManually.this.finish(); - } - } - }); - } - }.start(); + managerBinder = ((ComputerManagerService.ComputerManagerBinder)binder); + startAddThread(); } public void onServiceDisconnected(ComponentName className) { + joinAddThread(); + managerBinder = null; } }; + private void doAddPc(String host) { + String msg; + boolean finish = false; + try { + InetAddress addr = InetAddress.getByName(host); + + if (!managerBinder.addComputerBlocking(addr)){ + msg = "Unable to connect to the specified computer. Make sure the required ports are allowed through the firewall."; + } + else { + msg = "Successfully added computer"; + finish = true; + } + } catch (UnknownHostException e) { + msg = "Unable to resolve PC address. Make sure you didn't make a typo in the address."; + } + + final boolean toastFinish = finish; + final String toastMsg = msg; + AddComputerManually.this.runOnUiThread(new Runnable() { + @Override + public void run() { + Toast.makeText(AddComputerManually.this, toastMsg, Toast.LENGTH_LONG).show(); + + if (toastFinish && !isFinishing()) { + // Close the activity + AddComputerManually.this.finish(); + } + } + }); + } + + private void startAddThread() { + addThread = new Thread() { + @Override + public void run() { + while (!isInterrupted()) { + String computer; + + try { + computer = computersToAdd.take(); + } catch (InterruptedException e) { + return; + } + + doAddPc(computer); + } + } + }; + addThread.setName("UI - AddComputerManually"); + addThread.start(); + } + + private void joinAddThread() { + if (addThread != null) { + addThread.interrupt(); + + try { + addThread.join(); + } catch (InterruptedException e) {} + + addThread = null; + } + } + @Override protected void onStop() { super.onStop(); @@ -74,6 +110,16 @@ public class AddComputerManually extends Activity { Dialog.closeDialogs(); } + @Override + protected void onDestroy() { + super.onDestroy(); + + if (managerBinder != null) { + joinAddThread(); + unbindService(serviceConnection); + } + } + @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); @@ -82,6 +128,10 @@ public class AddComputerManually extends Activity { this.addPcButton = (Button) findViewById(R.id.addPc); this.hostText = (TextView) findViewById(R.id.hostTextView); + + // Bind to the ComputerManager service + bindService(new Intent(AddComputerManually.this, + ComputerManagerService.class), serviceConnection, Service.BIND_AUTO_CREATE); addPcButton.setOnClickListener(new OnClickListener() { @Override @@ -92,9 +142,7 @@ public class AddComputerManually extends Activity { } Toast.makeText(AddComputerManually.this, "Adding PC...", Toast.LENGTH_SHORT).show(); - - // Bind to the service which will try to add the PC - bindService(new Intent(AddComputerManually.this, ComputerManagerService.class), serviceConnection, Service.BIND_AUTO_CREATE); + computersToAdd.add(hostText.getText().toString()); } }); }