From 504471023a98ab8c56ea1525bdbb855372d5de78 Mon Sep 17 00:00:00 2001 From: Jaehong Jo Date: Thu, 27 Oct 2016 20:52:44 +0900 Subject: [PATCH] Added to obtain an address from a domain. For connect to cloud using domain. Change-Id: I3afc2f86f3b9e02bc048354cf9c209df095af21f Signed-off-by: Jaehong Jo Reviewed-on: https://gerrit.iotivity.org/gerrit/13781 Tested-by: jenkins-iotivity Reviewed-by: Ashok Babu Channa --- .../org/iotivity/base/examples/CloudFragment.java | 101 +++++++++++++++------ .../src/main/res/layout/fragment_cloud.xml | 5 +- .../simplebase/src/main/res/layout/input.xml | 22 ++++- 3 files changed, 96 insertions(+), 32 deletions(-) diff --git a/android/examples/simplebase/src/main/java/org/iotivity/base/examples/CloudFragment.java b/android/examples/simplebase/src/main/java/org/iotivity/base/examples/CloudFragment.java index 7f7c534..443c4c7 100644 --- a/android/examples/simplebase/src/main/java/org/iotivity/base/examples/CloudFragment.java +++ b/android/examples/simplebase/src/main/java/org/iotivity/base/examples/CloudFragment.java @@ -30,6 +30,7 @@ import android.content.DialogInterface; 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; @@ -38,6 +39,8 @@ import android.widget.CheckBox; 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; @@ -66,6 +69,8 @@ import org.iotivity.base.RequestType; 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; @@ -87,13 +92,7 @@ public class CloudFragment extends Fragment implements 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; @@ -1315,46 +1314,90 @@ public class CloudFragment extends Fragment implements 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() { diff --git a/android/examples/simplebase/src/main/res/layout/fragment_cloud.xml b/android/examples/simplebase/src/main/res/layout/fragment_cloud.xml index 13fb8a6..6610566 100644 --- a/android/examples/simplebase/src/main/res/layout/fragment_cloud.xml +++ b/android/examples/simplebase/src/main/res/layout/fragment_cloud.xml @@ -181,13 +181,14 @@ android:layout_weight="1" android:text="Sub Device\nPresence" /> + + android:orientation="horizontal">