diff --git a/OffloadingDemo/mobile/src/main/java/edu/ucsb/cs/sandlab/offloadingdemo/MainActivity.java b/OffloadingDemo/mobile/src/main/java/edu/ucsb/cs/sandlab/offloadingdemo/MainActivity.java index b76a9d0..510de80 100755 --- a/OffloadingDemo/mobile/src/main/java/edu/ucsb/cs/sandlab/offloadingdemo/MainActivity.java +++ b/OffloadingDemo/mobile/src/main/java/edu/ucsb/cs/sandlab/offloadingdemo/MainActivity.java @@ -14,6 +14,7 @@ import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.Button; +import android.widget.EditText; import android.widget.TextView; import android.widget.Toast; @@ -196,10 +197,34 @@ class MainActivity extends Activity { * @param myflag: */ protected void startRecording(boolean myflag) { + AlertDialog.Builder adb; final boolean flagRecv = myflag; final ArrayList selectedItems = new ArrayList<>(); - AlertDialog.Builder adb = new AlertDialog.Builder(MainActivity.this); + // first initialize the target ip and mac + EditText edit_remote_ip = (EditText) findViewById(R.id.remote_ip); + EditText edit_remote_mac = (EditText) findViewById(R.id.remote_mac); + String string_remote_ip = edit_remote_ip.getText().toString(); + String string_remote_mac = edit_remote_mac.getText().toString(); + // check if ip is in valid format and reachable + if (Utilities.validIP(string_remote_ip)) { + remoteIP = string_remote_ip; + } else { + Toast.makeText(this, "Entered IP is not right, will use " + remoteIP + "instead", + Toast.LENGTH_SHORT); + } + // check if mac is in valid format + if (Utilities.validMAC(string_remote_mac)) { + remoteMAC = string_remote_mac; + } else { + Toast.makeText(this, "Entered MAC is not right, will use " + remoteMAC + "instead", + Toast.LENGTH_SHORT); + } + Log.d(TAG, "remote IP is set to " + remoteIP); + Log.d(TAG, "remote MAC is set to " + remoteMAC); + + // then create a dialog for options + adb = new AlertDialog.Builder(MainActivity.this); adb.setMultiChoiceItems(Utilities.existedItems, null, new DialogInterface.OnMultiChoiceClickListener() { @Override @@ -895,8 +920,8 @@ class MainActivity extends Activity { "time_wait_for is set to " + time_wait_for + "ms\n"); } }); - Log.d(TAG, "time_wait_for is set to " + time_wait_for + "ms"); } + Log.d(TAG, "time_wait_for is set to " + time_wait_for + "ms"); } }); mDialog.create().show(); diff --git a/OffloadingDemo/mobile/src/main/java/edu/ucsb/cs/sandlab/offloadingdemo/Utilities.java b/OffloadingDemo/mobile/src/main/java/edu/ucsb/cs/sandlab/offloadingdemo/Utilities.java index 9d5948e..bcab4f5 100755 --- a/OffloadingDemo/mobile/src/main/java/edu/ucsb/cs/sandlab/offloadingdemo/Utilities.java +++ b/OffloadingDemo/mobile/src/main/java/edu/ucsb/cs/sandlab/offloadingdemo/Utilities.java @@ -22,6 +22,8 @@ import java.util.ArrayList; import java.util.Date; import java.util.Enumeration; import java.util.Locale; +import java.util.regex.Matcher; +import java.util.regex.Pattern; /** * Created by yanzi on 10/1/15. @@ -552,4 +554,46 @@ class Utilities { } }); } + + /** + * check if is a valid IP (pingable) + * @param ip: ip address as string + * @return boolean + */ + static boolean validIP(String ip) { + // only check the ip ad ipv4 + if ( ip == null || ip.isEmpty() ) return false; + int i; + String[] parts; + try { + parts = ip.split( "\\." ); + } catch (Exception ignored) { + return false; + } + if ( parts.length != 4 ) return false; + for ( String s : parts ) { + i = Integer.parseInt(s); + if ((i < 0) || (i > 255)) return false; + } + if (ip.endsWith(".")) return false; + // ping the ip and see if it is reachable + try { + return InetAddress.getByName(ip).isReachable(5); + } catch (IOException ignored) { + return false; + } + } + + /** + * check if is a valid MAC + * @param mac: mac address as string + * @return boolean + */ + static boolean validMAC(String mac) { + // use regular expression to validate a mac address + // the only valid format is xx:xx:xx:xx:xx:xx + Pattern p = Pattern.compile("^([a-fA-F0-9][:]){5}[a-fA-F0-9][:]$"); + Matcher m = p.matcher(mac); + return m.find(); + } } diff --git a/OffloadingDemo/mobile/src/main/res/layout/activity_main.xml b/OffloadingDemo/mobile/src/main/res/layout/activity_main.xml index 21c83df..34e11c9 100755 --- a/OffloadingDemo/mobile/src/main/res/layout/activity_main.xml +++ b/OffloadingDemo/mobile/src/main/res/layout/activity_main.xml @@ -40,6 +40,50 @@ + + + + + + + + + + +