From 569889ab8394c23a0eb030cbec6e54c0ab77f578 Mon Sep 17 00:00:00 2001 From: Miguel Astor Date: Tue, 26 Nov 2013 14:18:53 -0430 Subject: [PATCH] Added some progress dialogs when setting up connections. --- res/values-es/strings.xml | 2 + res/values/strings.xml | 2 + src/ve/ucv/ciens/ccg/nxtcam/MainActivity.java | 158 ++++++++++++++---- 3 files changed, 129 insertions(+), 33 deletions(-) diff --git a/res/values-es/strings.xml b/res/values-es/strings.xml index ab243ab..bd27c1e 100644 --- a/res/values-es/strings.xml +++ b/res/values-es/strings.xml @@ -25,5 +25,7 @@ Escoja un robot Conexión exitosa con el robot No se pudo conectar con el robot + Por favor espere por la conexión + Por favor espere, buscando dispositivo de control diff --git a/res/values/strings.xml b/res/values/strings.xml index 3ba7e3c..3180466 100644 --- a/res/values/strings.xml +++ b/res/values/strings.xml @@ -25,5 +25,7 @@ Pick a robot Successfully connected with the robot Could not connect with the robot + Please wait for the connection + Please wait, looking for control device \ No newline at end of file diff --git a/src/ve/ucv/ciens/ccg/nxtcam/MainActivity.java b/src/ve/ucv/ciens/ccg/nxtcam/MainActivity.java index 0dbd881..b0a5a20 100644 --- a/src/ve/ucv/ciens/ccg/nxtcam/MainActivity.java +++ b/src/ve/ucv/ciens/ccg/nxtcam/MainActivity.java @@ -14,6 +14,7 @@ import ve.ucv.ciens.ccg.nxtcam.utils.Logger; import ve.ucv.ciens.ccg.nxtcam.utils.ProjectConstants; import android.app.Activity; import android.app.DialogFragment; +import android.app.ProgressDialog; import android.bluetooth.BluetoothAdapter; import android.content.Context; import android.content.Intent; @@ -47,6 +48,7 @@ public class MainActivity extends Activity implements WifiOnDialogListener, Conn // Gui components private Button startButton; private Button connectButton; + private ProgressDialog progressDialog; // Resources. private BTCommunicator btManager; @@ -164,6 +166,9 @@ public class MainActivity extends Activity implements WifiOnDialogListener, Conn wifiOnByMe = false; } + /** + * Shows a WifiOnDialog. + */ private void enableWifi(){ if(!wifiManager.isWifiEnabled()){ DialogFragment wifiOn = new WifiOnDialog(); @@ -172,6 +177,9 @@ public class MainActivity extends Activity implements WifiOnDialogListener, Conn } } + /** + * Launches the standard Bluetooth enable activity. + */ private void enableBT(){ Logger.log_d(TAG, CLASS_NAME + ".enableBT() :: Enabling the Bluetooth radio."); Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); @@ -179,10 +187,99 @@ public class MainActivity extends Activity implements WifiOnDialogListener, Conn btOnByMe = true; } + /** + * Commodity method for showing toasts from an AsyncTask. + * + * @param stringId The id of the string resource to show on the toast. + * @param length Time to show the toast. + */ protected void showToast(int stringId, int length){ Toast.makeText(this, stringId, length).show(); } + /** + * Commodity method that builds an standard Android progress dialog. + * + * The dialog is created as not cancellable and uses an undeterminate spinner as visual style. + * + * @param msg The descriptive text shown by the dialog. + * @return The built dialog. + */ + private ProgressDialog buildProgressDialog(String msg){ + ProgressDialog dialog = new ProgressDialog(this); + dialog.setMessage(msg); + dialog.setCancelable(false); + dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER); + dialog.setProgress(0); + return dialog; + } + + /** + * Listener method for the WifiOnDialog. + * + * This method is called when the user chooses to accept the dialog. It just shows an information + * message with a toast and marks the WiFi radio as turned on by the application. + * + * @param dialog The dialog that called this method. + */ + @Override + public void onWifiOnDialogPositiveClick(DialogFragment dialog) { + Toast.makeText(this, R.string.wifi_on_success, Toast.LENGTH_SHORT).show(); + wifiOnByMe = true; + } + + /** + * Listener method for the WifiOnDialog. + * + * This method is called when the user chooses to cancel the dialog. It just shows an error message + * with a toast and finishes the application. + * + * @param dialog The dialog that called this method. + */ + @Override + public void onWifiOnDialogNegativeClick(DialogFragment dialog) { + Toast.makeText(this, R.string.wifi_on_fail, Toast.LENGTH_LONG).show(); + finish(); + }; + + /** + * Shows the robot selection dialog. + * + * @param view The view that called this method. + */ + public void connectWithRobot(View view){ + if(btManager.isBTEnabled()){ + DialogFragment connectBot = new ConnectRobotDialog(); + connectBot.show(getFragmentManager(), "connect_bot"); + } + } + + /** + * Launches the service discovery task. + */ + public void startConnections(View view){ + ServiceDiscoveryTask serviceDiscovery = new ServiceDiscoveryTask(); + serviceDiscovery.execute(); + } + + /** + * Listener method for the ConnectRobotDialog. + * + * When a user selects a robot to connect to in the dialog, this method launches the connection setup task + * defined in the ConnectRobotTask. + * + * @param dialog The dialog that called this method. + * @param robot The robot selected by the user in the format NAME\nMAC_ADDRESS + */ + @Override + public void onConnectRobotDialogItemClick(DialogFragment dialog, String robot) { + String macAddress = robot.substring(robot.indexOf('\n')+1); + Logger.log_d(TAG, CLASS_NAME + ".onConnectRobotDialogItemClick() :: MAC address: " + macAddress); + connectButton.setEnabled(false); + ConnectRobotTask robotTask = new ConnectRobotTask(macAddress); + robotTask.execute(); + } + /** * Asynchronous task for ad hoc UDP service discovery. * @@ -207,6 +304,13 @@ public class MainActivity extends Activity implements WifiOnDialogListener, Conn } } + @Override + protected void onPreExecute(){ + super.onPreExecute(); + progressDialog = buildProgressDialog(getString(R.string.serv_wait)); + progressDialog.show(); + } + @Override protected Boolean doInBackground(Void... params){ boolean result, done = false; @@ -247,6 +351,10 @@ public class MainActivity extends Activity implements WifiOnDialogListener, Conn @Override protected void onPostExecute(Boolean result){ super.onPostExecute(result); + + progressDialog.dismiss(); + progressDialog = null; + // If a server was found then start the next activity. if(packet != null) startCamActivity(result, packet.getAddress().getHostAddress()); @@ -255,6 +363,11 @@ public class MainActivity extends Activity implements WifiOnDialogListener, Conn } } + /** + * This task handles the establishing of the connection with the NXT robot. + * + * @author miky + */ private class ConnectRobotTask extends AsyncTask{ private final String CLASS_NAME = ConnectRobotTask.class.getSimpleName(); private String macAddress; @@ -263,6 +376,13 @@ public class MainActivity extends Activity implements WifiOnDialogListener, Conn this.macAddress = macAddress; } + @Override + protected void onPreExecute(){ + super.onPreExecute(); + progressDialog = buildProgressDialog(getString(R.string.bt_wait)); + progressDialog.show(); + } + @Override protected Boolean doInBackground(Void... params){ boolean connSet; @@ -278,6 +398,11 @@ public class MainActivity extends Activity implements WifiOnDialogListener, Conn @Override protected void onPostExecute(Boolean result){ + super.onPostExecute(result); + + progressDialog.dismiss(); + progressDialog = null; + if(result){ Logger.log_d(TAG, CLASS_NAME + "doInBackground() :: Connection successful."); showToast(R.string.conn_established, Toast.LENGTH_SHORT); @@ -288,37 +413,4 @@ public class MainActivity extends Activity implements WifiOnDialogListener, Conn } } } - - @Override - public void onWifiOnDialogPositiveClick(DialogFragment dialog) { - Toast.makeText(this, R.string.wifi_on_success, Toast.LENGTH_SHORT).show(); - wifiOnByMe = true; - } - - @Override - public void onWifiOnDialogNegativeClick(DialogFragment dialog) { - Toast.makeText(this, R.string.wifi_on_fail, Toast.LENGTH_LONG).show(); - finish(); - }; - - public void connectWithRobot(View view){ - if(btManager.isBTEnabled()){ - DialogFragment connectBot = new ConnectRobotDialog(); - connectBot.show(getFragmentManager(), "connect_bot"); - } - } - - public void startConnections(View view){ - ServiceDiscoveryTask serviceDiscovery = new ServiceDiscoveryTask(); - serviceDiscovery.execute(); - } - - @Override - public void onConnectRobotDialogItemClick(DialogFragment dialog, String item) { - String macAddress = item.substring(item.indexOf('\n')+1); - Logger.log_d(TAG, CLASS_NAME + ".onConnectRobotDialogItemClick() :: MAC address: " + macAddress); - connectButton.setEnabled(false); - ConnectRobotTask robotTask = new ConnectRobotTask(macAddress); - robotTask.execute(); - } }