add ip and mac as input
This commit is contained in:
parent
4ae9fd2306
commit
1594f0ad43
|
|
@ -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<Integer> 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();
|
||||
|
|
|
|||
|
|
@ -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();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -40,6 +40,50 @@
|
|||
|
||||
</LinearLayout>
|
||||
|
||||
<LinearLayout
|
||||
android:id="@+id/container_target_ipmac"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="0dp"
|
||||
android:orientation="horizontal"
|
||||
android:layout_weight="0.4">
|
||||
|
||||
<TextView
|
||||
android:layout_width="0dp"
|
||||
android:layout_height="match_parent"
|
||||
android:layout_weight="1"
|
||||
android:text="IP:"
|
||||
android:gravity="center"
|
||||
android:textSize="16dp"/>
|
||||
|
||||
<EditText
|
||||
android:id="@+id/remote_ip"
|
||||
android:layout_width="0dp"
|
||||
android:layout_height="match_parent"
|
||||
android:layout_weight="3"
|
||||
android:hint="Remote IP"
|
||||
android:text="192.168.10.1"
|
||||
android:inputType="text"
|
||||
android:textSize="16dp"/>
|
||||
|
||||
<TextView
|
||||
android:layout_width="0dp"
|
||||
android:layout_height="match_parent"
|
||||
android:layout_weight="1"
|
||||
android:text="MAC:"
|
||||
android:gravity="center"
|
||||
android:textSize="16dp"/>
|
||||
|
||||
<EditText
|
||||
android:id="@+id/remote_mac"
|
||||
android:layout_width="0dp"
|
||||
android:layout_height="match_parent"
|
||||
android:layout_weight="3"
|
||||
android:hint="Remote MAC"
|
||||
android:text="f0:de:f1:0b:45:4a"
|
||||
android:inputType="text"
|
||||
android:textSize="16dp"/>
|
||||
|
||||
</LinearLayout>
|
||||
<!--<HorizontalScrollView-->
|
||||
<!--android:id="@+id/container_config"-->
|
||||
<!--android:layout_width="match_parent"-->
|
||||
|
|
|
|||
Loading…
Reference in New Issue