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 new file mode 100644 index 0000000..5804d84 --- /dev/null +++ b/NucuHub.Android/app/src/main/java/dev/nuculabs/nucuhub/domain/Device.java @@ -0,0 +1,58 @@ +package dev.nuculabs.nucuhub.domain; + +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; + + public Device() { + } + + /** + * Constructs a new device instance. + * + * @param url Url of the form http://localhost:port + */ + public Device(String url) { + validateUrlAgainstRegex(url); + target = url; + } + + public Device(String host, int port) { + String temp = String.format(Locale.ENGLISH, "%s:%d", host, port); + validateUrlAgainstRegex(temp); + target = temp; + } + + public String getTarget() { + return target; + } + + public void setTarget(String target) { + validateUrlAgainstRegex(target); + this.target = 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)); + } + } +} diff --git a/NucuHub.Android/app/src/main/java/dev/nuculabs/nucuhub/domain/SettingValues.java b/NucuHub.Android/app/src/main/java/dev/nuculabs/nucuhub/domain/SettingValues.java new file mode 100644 index 0000000..1b79cd6 --- /dev/null +++ b/NucuHub.Android/app/src/main/java/dev/nuculabs/nucuhub/domain/SettingValues.java @@ -0,0 +1,6 @@ +package dev.nuculabs.nucuhub.domain; + +public class SettingValues { + public static final String NAME = "settings"; + public static final String CURRENT_DEVICE_LIST = "current_device_list"; +} diff --git a/NucuHub.Android/app/src/main/java/dev/nuculabs/nucuhub/domain/Utils.java b/NucuHub.Android/app/src/main/java/dev/nuculabs/nucuhub/domain/Utils.java new file mode 100644 index 0000000..cad69d7 --- /dev/null +++ b/NucuHub.Android/app/src/main/java/dev/nuculabs/nucuhub/domain/Utils.java @@ -0,0 +1,7 @@ +package dev.nuculabs.nucuhub.domain; + +public class Utils { + public static boolean isNullOrEmpty(String str) { + return str == null || str.isEmpty(); + } +} diff --git a/NucuHub.Android/app/src/main/java/dev/nuculabs/nucuhub/grpc/EnvironmentSensorService.java b/NucuHub.Android/app/src/main/java/dev/nuculabs/nucuhub/grpc/EnvironmentSensorService.java index d253733..6e1c570 100644 --- a/NucuHub.Android/app/src/main/java/dev/nuculabs/nucuhub/grpc/EnvironmentSensorService.java +++ b/NucuHub.Android/app/src/main/java/dev/nuculabs/nucuhub/grpc/EnvironmentSensorService.java @@ -30,6 +30,11 @@ public class EnvironmentSensorService { Log.i(TAG, String.format(Locale.ENGLISH, "Initializing channel host=%s port=%d", host, port)); } + public EnvironmentSensorService(String target) { + this(ManagedChannelBuilder.forTarget(target).usePlaintext()); + Log.i(TAG, String.format(Locale.ENGLISH, "Initializing channel target=%s", target)); + } + public EnvironmentSensorService(ManagedChannelBuilder channelBuilder) { ManagedChannel channel = channelBuilder.build(); blockingStub = EnvironmentSensorGrpcServiceGrpc.newBlockingStub(channel); diff --git a/NucuHub.Android/app/src/main/java/dev/nuculabs/nucuhub/ui/sensors/SensorsFragment.java b/NucuHub.Android/app/src/main/java/dev/nuculabs/nucuhub/ui/sensors/SensorsFragment.java index aa6593a..e37f09a 100644 --- a/NucuHub.Android/app/src/main/java/dev/nuculabs/nucuhub/ui/sensors/SensorsFragment.java +++ b/NucuHub.Android/app/src/main/java/dev/nuculabs/nucuhub/ui/sensors/SensorsFragment.java @@ -37,7 +37,7 @@ public class SensorsFragment extends Fragment { sensorStatusImageView = root.findViewById(R.id.sensorStatusImageView); sensorStatusText = root.findViewById(R.id.sensorStatusChip); - environmentSensorService = new EnvironmentSensorService("192.168.0.100", 8000); + environmentSensorService = new EnvironmentSensorService("192.168.0.100:8000"); setupSensorStatusImage(); setupSensorMeasurementsDisplays(); diff --git a/NucuHub.Android/app/src/main/java/dev/nuculabs/nucuhub/ui/settings/SettingsFragment.java b/NucuHub.Android/app/src/main/java/dev/nuculabs/nucuhub/ui/settings/SettingsFragment.java index 4d89157..409b58d 100644 --- a/NucuHub.Android/app/src/main/java/dev/nuculabs/nucuhub/ui/settings/SettingsFragment.java +++ b/NucuHub.Android/app/src/main/java/dev/nuculabs/nucuhub/ui/settings/SettingsFragment.java @@ -5,12 +5,14 @@ import android.content.SharedPreferences; import android.os.Bundle; import androidx.preference.PreferenceFragmentCompat; import dev.nuculabs.nucuhub.R; +import dev.nuculabs.nucuhub.domain.SettingValues; +import dev.nuculabs.nucuhub.ui.settings.device.DeviceListPreference; +import java.util.Objects; import java.util.Set; public class SettingsFragment extends PreferenceFragmentCompat { - private SharedPreferences sharedPreferences; - + public static final String DEVICE_LIST_PREFERENCE = "device_list"; @Override public void onCreatePreferences(Bundle savedInstanceState, String rootKey) { @@ -18,15 +20,15 @@ public class SettingsFragment extends PreferenceFragmentCompat { // SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this); setPreferencesFromResource(R.xml.root_preferences, rootKey); - updateDeviceListItems(); + initializeDeviceListItems(); } - private void updateDeviceListItems() { - // dummy code to update device list entries - SharedPreferences sp = getContext().getSharedPreferences("settings", Activity.MODE_PRIVATE); + private void initializeDeviceListItems() { + // TODO dummy code to update device list entries + SharedPreferences sp = Objects.requireNonNull(getContext()).getSharedPreferences(SettingValues.NAME, Activity.MODE_PRIVATE); SharedPreferences.Editor spe = sp.edit(); - DeviceListPreference devicesList = findPreference("device_list"); - Set savedEntries = sp.getStringSet("current_device_list", null); + DeviceListPreference devicesList = findPreference(DEVICE_LIST_PREFERENCE); + Set savedEntries = sp.getStringSet(SettingValues.CURRENT_DEVICE_LIST, null); if (savedEntries != null) { CharSequence[] items = new CharSequence[savedEntries.size()]; int index = 0; diff --git a/NucuHub.Android/app/src/main/java/dev/nuculabs/nucuhub/ui/settings/DeviceListPreference.java b/NucuHub.Android/app/src/main/java/dev/nuculabs/nucuhub/ui/settings/device/DeviceListPreference.java similarity index 94% rename from NucuHub.Android/app/src/main/java/dev/nuculabs/nucuhub/ui/settings/DeviceListPreference.java rename to NucuHub.Android/app/src/main/java/dev/nuculabs/nucuhub/ui/settings/device/DeviceListPreference.java index 61368bc..a1c7f0a 100644 --- a/NucuHub.Android/app/src/main/java/dev/nuculabs/nucuhub/ui/settings/DeviceListPreference.java +++ b/NucuHub.Android/app/src/main/java/dev/nuculabs/nucuhub/ui/settings/device/DeviceListPreference.java @@ -1,4 +1,4 @@ -package dev.nuculabs.nucuhub.ui.settings; +package dev.nuculabs.nucuhub.ui.settings.device; import android.content.Context; import android.util.AttributeSet; diff --git a/NucuHub.Android/app/src/main/java/dev/nuculabs/nucuhub/ui/settings/DeviceManagementDialog.java b/NucuHub.Android/app/src/main/java/dev/nuculabs/nucuhub/ui/settings/device/DeviceManagementDialog.java similarity index 87% rename from NucuHub.Android/app/src/main/java/dev/nuculabs/nucuhub/ui/settings/DeviceManagementDialog.java rename to NucuHub.Android/app/src/main/java/dev/nuculabs/nucuhub/ui/settings/device/DeviceManagementDialog.java index 40b5a17..f711046 100644 --- a/NucuHub.Android/app/src/main/java/dev/nuculabs/nucuhub/ui/settings/DeviceManagementDialog.java +++ b/NucuHub.Android/app/src/main/java/dev/nuculabs/nucuhub/ui/settings/device/DeviceManagementDialog.java @@ -1,4 +1,4 @@ -package dev.nuculabs.nucuhub.ui.settings; +package dev.nuculabs.nucuhub.ui.settings.device; import android.app.Activity; import android.app.Dialog; @@ -15,6 +15,7 @@ import androidx.annotation.NonNull; import androidx.appcompat.widget.Toolbar; import androidx.preference.ListPreference; import dev.nuculabs.nucuhub.R; +import dev.nuculabs.nucuhub.domain.SettingValues; import java.util.HashSet; @@ -34,13 +35,13 @@ public class DeviceManagementDialog extends Dialog { getWindow().setLayout(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.MATCH_PARENT); setupToolbar(); - sharedPreferences = getContext().getSharedPreferences("settings", Activity.MODE_PRIVATE); + sharedPreferences = getContext().getSharedPreferences(SettingValues.NAME, Activity.MODE_PRIVATE); final ListView deviceListView = findViewById(R.id.settings_device_dialog_list); - adapter = new ArrayAdapter(getContext(), android.R.layout.simple_list_item_1); + adapter = new ArrayAdapter<>(getContext(), android.R.layout.simple_list_item_1); deviceListView.setAdapter(adapter); - // dummy code to add simple items. + // todo: dummy code to add simple items. final CharSequence [] preferenceCharSeq = preference.getEntries(); for (CharSequence item : preferenceCharSeq) { adapter.add(item.toString()); @@ -83,7 +84,7 @@ public class DeviceManagementDialog extends Dialog { } private void updatePreferenceEntryValues() { - // dummy code to update the entries. + // todo dummy code to update the entries. int itemsLength = adapter.getCount(); CharSequence[] entries = new CharSequence[itemsLength]; HashSet entriesSet = new HashSet<>(); @@ -94,7 +95,7 @@ public class DeviceManagementDialog extends Dialog { preference.setEntries(entries); SharedPreferences.Editor spe = sharedPreferences.edit(); - spe.putStringSet("current_device_list", entriesSet); + spe.putStringSet(SettingValues.CURRENT_DEVICE_LIST, entriesSet); } private void setupToolbar() { diff --git a/NucuHub.Android/app/src/main/res/xml/root_preferences.xml b/NucuHub.Android/app/src/main/res/xml/root_preferences.xml index 74e9c94..86b9e03 100644 --- a/NucuHub.Android/app/src/main/res/xml/root_preferences.xml +++ b/NucuHub.Android/app/src/main/res/xml/root_preferences.xml @@ -38,7 +38,7 @@ -