Added to obtain an address from a domain.
authorJaehong Jo <jaehong.jo@samsung.com>
Thu, 27 Oct 2016 11:52:44 +0000 (20:52 +0900)
committerAshok Babu Channa <ashok.channa@samsung.com>
Wed, 2 Nov 2016 11:09:33 +0000 (11:09 +0000)
For connect to cloud using domain.

Change-Id: I3afc2f86f3b9e02bc048354cf9c209df095af21f
Signed-off-by: Jaehong Jo <jaehong.jo@samsung.com>
Reviewed-on: https://gerrit.iotivity.org/gerrit/13781
Tested-by: jenkins-iotivity <jenkins-iotivity@opendaylight.org>
Reviewed-by: Ashok Babu Channa <ashok.channa@samsung.com>
android/examples/simplebase/src/main/java/org/iotivity/base/examples/CloudFragment.java
android/examples/simplebase/src/main/res/layout/fragment_cloud.xml
android/examples/simplebase/src/main/res/layout/input.xml

index 7f7c534..443c4c7 100644 (file)
@@ -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() {
index 13fb8a6..6610566 100644 (file)
                     android:layout_weight="1"
                     android:text="Sub Device\nPresence" />
             </LinearLayout>
+
             <LinearLayout
                 android:layout_width="match_parent"
                 android:layout_height="wrap_content"
                 android:layout_gravity="center_horizontal"
+                android:layout_marginBottom="2dp"
                 android:gravity="center_horizontal"
-                android:orientation="horizontal"
-                android:layout_marginBottom="2dp">
+                android:orientation="horizontal">
 
                 <Button
                     android:id="@+id/findresource_button"
index d3fa703..8b0b553 100755 (executable)
@@ -9,9 +9,29 @@
         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"