diff --git a/NucuHub.Android/app/src/main/java/dev/nuculabs/nucuhub/domain/Device.java b/NucuHub.Android/app/src/main/java/dev/nuculabs/nucuhub/domain/Device.java index 5497e5f..c193c8b 100644 --- a/NucuHub.Android/app/src/main/java/dev/nuculabs/nucuhub/domain/Device.java +++ b/NucuHub.Android/app/src/main/java/dev/nuculabs/nucuhub/domain/Device.java @@ -2,15 +2,15 @@ package dev.nuculabs.nucuhub.domain; import androidx.annotation.NonNull; +import java.net.MalformedURLException; +import java.net.URL; import java.util.Locale; -import java.util.regex.Matcher; -import java.util.regex.Pattern; /** * Device represents a NucuHub device. */ public class Device { - private String target; + private URL target; public Device() { } @@ -21,46 +21,37 @@ public class Device { * @param url Url of the form http://localhost:port */ public Device(String url) { - validateUrlAgainstRegex(url); - target = url; + stringToURL(url); } public Device(String host, int port) { String temp = String.format(Locale.ENGLISH, "%s:%d", host, port); - validateUrlAgainstRegex(temp); - target = temp; + stringToURL(temp); } public String getTarget() { - return target; + return target.toString(); } public void setTarget(String target) { - validateUrlAgainstRegex(target); - this.target = target; + stringToURL(target); } public boolean testConnection() { return true; } - private void validateUrlAgainstRegex(String url) { - String urlValidationRegex = "^(([^:/?#]+):)?(//([^/?#]*))?([^?#]*)(\\?([^#]*))?(#(.*))?"; - Pattern p = Pattern.compile(urlValidationRegex); - Matcher m = p.matcher(url); - if (m.matches()) { - if (url.contains("http://") || url.contains("https://")) { - throw new IllegalArgumentException("Don't include schema with URL"); - } - } else { - throw new IllegalArgumentException(String.format(Locale.ENGLISH, - "Malformed URL provided for device: %s", url)); + private void stringToURL(String url) { + try { + target = new URL(url); + } catch (MalformedURLException e) { + throw new IllegalArgumentException(e.getMessage()); } } @NonNull @Override public String toString() { - return this.target; + return this.target.toString(); } } diff --git a/NucuHub.Android/app/src/main/java/dev/nuculabs/nucuhub/ui/settings/device/DeviceListAdapter.java b/NucuHub.Android/app/src/main/java/dev/nuculabs/nucuhub/ui/settings/device/DeviceListAdapter.java new file mode 100644 index 0000000..55aa532 --- /dev/null +++ b/NucuHub.Android/app/src/main/java/dev/nuculabs/nucuhub/ui/settings/device/DeviceListAdapter.java @@ -0,0 +1,61 @@ +package dev.nuculabs.nucuhub.ui.settings.device; + +import android.content.Context; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.BaseAdapter; +import android.widget.TextView; +import dev.nuculabs.nucuhub.domain.Device; + +import java.util.ArrayList; + +public class DeviceListAdapter extends BaseAdapter { + private final ArrayList items = new ArrayList<>(); + private final Context context; + private final LayoutInflater inflater; + + + public DeviceListAdapter(Context context) { + this.context = context; + this.inflater = LayoutInflater.from(context); + } + + public DeviceListAdapter(ArrayList items, Context context) { + this(context); + this.items.addAll(items); + } + + @Override + public int getCount() { + return items.size(); + } + + @Override + public Device getItem(int position) { + return items.get(position); + } + + @Override + public long getItemId(int position) { + return items.get(position).hashCode(); + } + + @Override + public View getView(int position, View convertView, ViewGroup parent) { + View view = convertView; + final TextView text; + if (view == null) { + view = inflater.inflate(android.R.layout.simple_list_item_1, null); + } + + text = (TextView) view; + text.setText(items.get(position).toString()); + + return view; + } + + public boolean add(Device device) { + return items.add(device); + } +} diff --git a/NucuHub.Android/app/src/main/java/dev/nuculabs/nucuhub/ui/settings/device/DeviceManagementDialog.java b/NucuHub.Android/app/src/main/java/dev/nuculabs/nucuhub/ui/settings/device/DeviceManagementDialog.java index 042ce6a..d17d2d7 100644 --- a/NucuHub.Android/app/src/main/java/dev/nuculabs/nucuhub/ui/settings/device/DeviceManagementDialog.java +++ b/NucuHub.Android/app/src/main/java/dev/nuculabs/nucuhub/ui/settings/device/DeviceManagementDialog.java @@ -5,10 +5,10 @@ import android.app.Dialog; import android.content.Context; import android.content.SharedPreferences; import android.os.Bundle; +import android.util.Log; import android.view.MenuItem; import android.view.View; import android.view.WindowManager; -import android.widget.ArrayAdapter; import android.widget.Button; import android.widget.EditText; import android.widget.ListView; @@ -17,15 +17,17 @@ import androidx.appcompat.widget.Toolbar; import androidx.preference.ListPreference; import com.google.android.material.textfield.TextInputLayout; import dev.nuculabs.nucuhub.R; +import dev.nuculabs.nucuhub.domain.Device; import dev.nuculabs.nucuhub.domain.SettingValues; import java.util.HashSet; import java.util.Objects; public class DeviceManagementDialog extends Dialog { + private final String TAG = DeviceManagementDialog.class.getName(); private SharedPreferences sharedPreferences; private ListPreference preference = null; - private ArrayAdapter adapter; + private DeviceListAdapter adapter; private TextInputLayout deviceTextInputLayout; public DeviceManagementDialog(@NonNull Context context) { @@ -43,14 +45,14 @@ public class DeviceManagementDialog extends Dialog { final ListView deviceListView = requireViewById(R.id.settings_device_dialog_list); final Button addDeviceButton = requireViewById(R.id.settings_device_add_button); deviceTextInputLayout = requireViewById(R.id.settings_device_input_device); - adapter = new ArrayAdapter<>(getContext(), android.R.layout.simple_list_item_1); + adapter = new DeviceListAdapter(getContext()); deviceListView.setAdapter(adapter); // Add the existing items in the list adapter so they will be displayed. final CharSequence [] preferenceCharSeq = preference.getEntries(); for (CharSequence item : preferenceCharSeq) { - adapter.add(item.toString()); + adapter.add(new Device(item.toString())); } // on-click handlers. @@ -90,7 +92,7 @@ public class DeviceManagementDialog extends Dialog { CharSequence[] entries = new CharSequence[itemsLength]; HashSet entriesSet = new HashSet<>(); for (int i = 0; i < itemsLength; i++) { - entries[i] = adapter.getItem(i); + entries[i] = adapter.getItem(i).toString(); entriesSet.add(entries[i].toString()); } preference.setEntries(entries); @@ -117,10 +119,15 @@ public class DeviceManagementDialog extends Dialog { @Override public void onClick(View v) { // TODO: Test connection before adding. loading -> testing connection -> show dialog. - EditText editText = Objects.requireNonNull(deviceTextInputLayout.getEditText()); - String target = editText.getText().toString(); - adapter.add(target); - editText.setText(null); + try { + EditText editText = Objects.requireNonNull(deviceTextInputLayout.getEditText()); + String target = editText.getText().toString(); + adapter.add(new Device(target)); + editText.setText("http://"); + } catch (IllegalArgumentException e) { + Log.e(TAG, e.getMessage()); + } + } } } diff --git a/NucuHub.Android/app/src/main/res/values/arrays.xml b/NucuHub.Android/app/src/main/res/values/arrays.xml index 4b18e8b..624988d 100644 --- a/NucuHub.Android/app/src/main/res/values/arrays.xml +++ b/NucuHub.Android/app/src/main/res/values/arrays.xml @@ -4,12 +4,12 @@ - Reply - Reply to all + http://localhost:9090/ + http://google.com/ - reply - reply_all + http://localhost:9090/ + http://google.com/ \ No newline at end of file diff --git a/NucuHub.Android/app/src/main/res/values/strings.xml b/NucuHub.Android/app/src/main/res/values/strings.xml index 1e10d29..27ace7e 100644 --- a/NucuHub.Android/app/src/main/res/values/strings.xml +++ b/NucuHub.Android/app/src/main/res/values/strings.xml @@ -36,7 +36,7 @@ Device Management Connect new device Add Device - hostname:port + http://hostname:port Saved Devices diff --git a/NucuHub.Android/app/src/test/java/dev/nuculabs/nucuhub/domain/DeviceTest.java b/NucuHub.Android/app/src/test/java/dev/nuculabs/nucuhub/domain/DeviceTest.java index 315e7ba..7160052 100644 --- a/NucuHub.Android/app/src/test/java/dev/nuculabs/nucuhub/domain/DeviceTest.java +++ b/NucuHub.Android/app/src/test/java/dev/nuculabs/nucuhub/domain/DeviceTest.java @@ -11,7 +11,7 @@ import static org.junit.Assert.*; public class DeviceTest { @Test public void test_constructionValidTargets() { - String[] testCases = {"localhost:8900/cool", "nuculabs.dev", "www.nuculabs.dev/", "user:pass@nuculabs.dev"}; + String[] testCases = {"http://localhost:8900/cool", "http://nuculabs.dev", "http://www.nuculabs.dev/", "http://user:pass@nuculabs.dev"}; for (String target : testCases) { Device device = new Device(target); assertEquals(device.getTarget(), target); @@ -21,31 +21,11 @@ public class DeviceTest { @Test public void test_constructionValidHostsAndPorts() { Random random = new Random(); - String[] testCases = {"localhost", "nuculabs.dev", "www.nuculabs.dev", "user:pass@nuculabs.dev"}; + String[] testCases = {"http://localhost", "http://nuculabs.dev", "http://www.nuculabs.dev", "http://user:pass@nuculabs.dev"}; for (String target : testCases) { int port = random.nextInt(65535); Device device = new Device(target, port); - assertEquals(device.getTarget(), String.format(Locale.ENGLISH,"%s:%d", target, port)); + assertEquals(device.getTarget(), String.format(Locale.ENGLISH, "%s:%d", target, port)); } } - - @Test(expected = IllegalArgumentException.class) - public void test_constructionInvalidHostAndPortHttps() { - new Device("https://google.com", 443); - } - - @Test(expected = IllegalArgumentException.class) - public void test_constructionInvalidHostAndPortHttp() { - new Device("http://google.com", 443); - } - - @Test(expected = IllegalArgumentException.class) - public void test_constructionInvalidTargetHttp() { - new Device("http://localhost:8900/cool"); - } - - @Test(expected = IllegalArgumentException.class) - public void test_constructionInvalidTargetHttps() { - new Device("https://nuculabs.dev"); - } } \ No newline at end of file