import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
+import android.util.Patterns;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.LinearLayout;
+import android.widget.RadioButton;
+import android.widget.RadioGroup;
import android.widget.ScrollView;
import android.widget.Switch;
import android.widget.TextView;
import org.iotivity.base.ResourceProperty;
import org.iotivity.base.ServiceType;
+import java.net.InetAddress;
+import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.EnumSet;
import java.util.HashMap;
private static final String TAG = "OIC_SIMPLE_CLOUD";
private final String EOL = System.getProperties().getProperty("line.separator");
- private final Pattern ADDRESS_PORT
- = Pattern.compile(
- "((25[0-5]|2[0-4][0-9]|[0-1][0-9]{2}|[1-9][0-9]|[1-9])" +
- "\\.(25[0-5]|2[0-4][0-9]|[0-1][0-9]{2}|[1-9][0-9]|[1-9]|0)" +
- "\\.(25[0-5]|2[0-4][0-9]|[0-1][0-9]{2}|[1-9][0-9]|[1-9]|0)" +
- "\\.(25[0-5]|2[0-4][0-9]|[0-1][0-9]{2}|[1-9][0-9]|[0-9])" +
- ":([0-9]{1,5}))");
+ private final Pattern PORT_NUMBER = Pattern.compile("(\\d{1,5})");
private Activity mActivity;
private Context mContext;
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(mActivity);
alertDialogBuilder.setView(inputView);
+ final RadioGroup radioGroup = (RadioGroup) inputView.getRootView().findViewById(R.id.radioGroup);
+ final RadioButton radioIP = (RadioButton) inputView.getRootView().findViewById(R.id.radioIP);
final EditText editText = (EditText) inputView.getRootView().findViewById(R.id.inputText);
final CheckBox isSecured = (CheckBox) inputView.getRootView().findViewById(R.id.secured);
+ radioGroup.setVisibility(View.VISIBLE);
+ isSecured.setVisibility(View.VISIBLE);
+ isSecured.setChecked(mSecured);
+
StringBuilder sb = new StringBuilder();
sb.append(Common.TCP_ADDRESS);
sb.append(Common.PORT_SEPARATOR);
sb.append(Common.TCP_PORT);
editText.setText(sb.toString());
- isSecured.setVisibility(View.VISIBLE);
- isSecured.setChecked(mSecured);
-
alertDialogBuilder
.setCancelable(true)
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
- if (editText.getText().length() != 0) {
- final String hosts = editText.getText().toString();
- boolean isValid = ADDRESS_PORT.matcher(hosts).matches();
- if (isValid) {
- final String host[] = hosts.split(Common.PORT_SEPARATOR);
- Common.TCP_ADDRESS = host[0];
- Common.TCP_PORT = host[1];
- mSecured = isSecured.isChecked();
-
- StringBuilder sb = new StringBuilder();
- if (mSecured) {
- sb.append(Common.COAPS_TCP);
+
+ final String hosts = editText.getText().toString();
+ boolean isValid = false;
+
+ if (!hosts.isEmpty() && hosts.contains(Common.PORT_SEPARATOR)) {
+ isValid = true;
+ }
+
+ if (isValid) {
+ final String host[] = hosts.split(Common.PORT_SEPARATOR);
+ mSecured = isSecured.isChecked();
+
+ if (2 > host.length || !PORT_NUMBER.matcher(host[1]).matches()) {
+ isValid = false;
+ } else if (radioIP.isChecked()) {
+ if (Patterns.IP_ADDRESS.matcher(host[0]).matches()) {
+ Common.TCP_ADDRESS = host[0];
+ Common.TCP_PORT = host[1];
} else {
- sb.append(Common.COAP_TCP);
+ isValid = false;
}
- sb.append(Common.TCP_ADDRESS);
- sb.append(Common.PORT_SEPARATOR);
- sb.append(Common.TCP_PORT);
- Common.HOST = sb.toString();
} else {
- Toast.makeText(mContext, "Invalid IP", Toast.LENGTH_SHORT).show();
- showTCPInput();
+ if (Patterns.DOMAIN_NAME.matcher(host[0]).matches()
+ && !Patterns.IP_ADDRESS.matcher(host[0]).matches()) {
+ Thread thread = new Thread() {
+ @Override
+ public void run() {
+ try {
+ Common.TCP_ADDRESS = InetAddress
+ .getByName(host[0]).getHostAddress();
+ Common.TCP_PORT = host[1];
+ } catch (UnknownHostException e) {
+ e.printStackTrace();
+ msg("Failed to get host address.");
+ }
+ }
+ };
+ thread.start();
+ try {
+ thread.join();
+ } catch (InterruptedException e) {
+ e.printStackTrace();
+ }
+ } else {
+ isValid = false;
+ }
}
}
+
+ if (isValid) {
+ StringBuilder sb = new StringBuilder();
+ if (mSecured) {
+ sb.append(Common.COAPS_TCP);
+ } else {
+ sb.append(Common.COAP_TCP);
+ }
+ sb.append(Common.TCP_ADDRESS);
+ sb.append(Common.PORT_SEPARATOR);
+ sb.append(Common.TCP_PORT);
+ Common.HOST = sb.toString();
+ msg("Set Host : " + Common.HOST);
+ } else {
+ Toast.makeText(mContext, "Invalid input", Toast.LENGTH_SHORT).show();
+ showTCPInput();
+ }
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
android:id="@+id/inputView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
- android:text="Input Server IP and Port"
+ android:text="Enter the server information"
android:textAppearance="?android:attr/textAppearanceMedium" />
+ <RadioGroup
+ android:id="@+id/radioGroup"
+ android:layout_width="match_parent"
+ android:layout_height="wrap_content"
+ android:visibility="gone">
+
+ <RadioButton
+ android:id="@+id/radioIP"
+ android:layout_width="match_parent"
+ android:layout_height="wrap_content"
+ android:checked="true"
+ android:text="Use IP and Port" />
+
+ <RadioButton
+ android:id="@+id/radioDomain"
+ android:layout_width="match_parent"
+ android:layout_height="wrap_content"
+ android:text="Use Domain and Port" />
+ </RadioGroup>
+
<EditText
android:id="@+id/inputText"
android:layout_width="match_parent"