Implement custom list adapter for list view.
This commit is contained in:
parent
588973e0ad
commit
67ea7abde4
6 changed files with 98 additions and 59 deletions
|
@ -2,15 +2,15 @@ package dev.nuculabs.nucuhub.domain;
|
||||||
|
|
||||||
import androidx.annotation.NonNull;
|
import androidx.annotation.NonNull;
|
||||||
|
|
||||||
|
import java.net.MalformedURLException;
|
||||||
|
import java.net.URL;
|
||||||
import java.util.Locale;
|
import java.util.Locale;
|
||||||
import java.util.regex.Matcher;
|
|
||||||
import java.util.regex.Pattern;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Device represents a NucuHub device.
|
* Device represents a NucuHub device.
|
||||||
*/
|
*/
|
||||||
public class Device {
|
public class Device {
|
||||||
private String target;
|
private URL target;
|
||||||
|
|
||||||
public Device() {
|
public Device() {
|
||||||
}
|
}
|
||||||
|
@ -21,46 +21,37 @@ public class Device {
|
||||||
* @param url Url of the form http://localhost:port
|
* @param url Url of the form http://localhost:port
|
||||||
*/
|
*/
|
||||||
public Device(String url) {
|
public Device(String url) {
|
||||||
validateUrlAgainstRegex(url);
|
stringToURL(url);
|
||||||
target = url;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public Device(String host, int port) {
|
public Device(String host, int port) {
|
||||||
String temp = String.format(Locale.ENGLISH, "%s:%d", host, port);
|
String temp = String.format(Locale.ENGLISH, "%s:%d", host, port);
|
||||||
validateUrlAgainstRegex(temp);
|
stringToURL(temp);
|
||||||
target = temp;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getTarget() {
|
public String getTarget() {
|
||||||
return target;
|
return target.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setTarget(String target) {
|
public void setTarget(String target) {
|
||||||
validateUrlAgainstRegex(target);
|
stringToURL(target);
|
||||||
this.target = target;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean testConnection() {
|
public boolean testConnection() {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void validateUrlAgainstRegex(String url) {
|
private void stringToURL(String url) {
|
||||||
String urlValidationRegex = "^(([^:/?#]+):)?(//([^/?#]*))?([^?#]*)(\\?([^#]*))?(#(.*))?";
|
try {
|
||||||
Pattern p = Pattern.compile(urlValidationRegex);
|
target = new URL(url);
|
||||||
Matcher m = p.matcher(url);
|
} catch (MalformedURLException e) {
|
||||||
if (m.matches()) {
|
throw new IllegalArgumentException(e.getMessage());
|
||||||
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));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@NonNull
|
@NonNull
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
return this.target;
|
return this.target.toString();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -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<Device> 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<Device> 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);
|
||||||
|
}
|
||||||
|
}
|
|
@ -5,10 +5,10 @@ import android.app.Dialog;
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.content.SharedPreferences;
|
import android.content.SharedPreferences;
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
|
import android.util.Log;
|
||||||
import android.view.MenuItem;
|
import android.view.MenuItem;
|
||||||
import android.view.View;
|
import android.view.View;
|
||||||
import android.view.WindowManager;
|
import android.view.WindowManager;
|
||||||
import android.widget.ArrayAdapter;
|
|
||||||
import android.widget.Button;
|
import android.widget.Button;
|
||||||
import android.widget.EditText;
|
import android.widget.EditText;
|
||||||
import android.widget.ListView;
|
import android.widget.ListView;
|
||||||
|
@ -17,15 +17,17 @@ import androidx.appcompat.widget.Toolbar;
|
||||||
import androidx.preference.ListPreference;
|
import androidx.preference.ListPreference;
|
||||||
import com.google.android.material.textfield.TextInputLayout;
|
import com.google.android.material.textfield.TextInputLayout;
|
||||||
import dev.nuculabs.nucuhub.R;
|
import dev.nuculabs.nucuhub.R;
|
||||||
|
import dev.nuculabs.nucuhub.domain.Device;
|
||||||
import dev.nuculabs.nucuhub.domain.SettingValues;
|
import dev.nuculabs.nucuhub.domain.SettingValues;
|
||||||
|
|
||||||
import java.util.HashSet;
|
import java.util.HashSet;
|
||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
|
|
||||||
public class DeviceManagementDialog extends Dialog {
|
public class DeviceManagementDialog extends Dialog {
|
||||||
|
private final String TAG = DeviceManagementDialog.class.getName();
|
||||||
private SharedPreferences sharedPreferences;
|
private SharedPreferences sharedPreferences;
|
||||||
private ListPreference preference = null;
|
private ListPreference preference = null;
|
||||||
private ArrayAdapter<String> adapter;
|
private DeviceListAdapter adapter;
|
||||||
private TextInputLayout deviceTextInputLayout;
|
private TextInputLayout deviceTextInputLayout;
|
||||||
|
|
||||||
public DeviceManagementDialog(@NonNull Context context) {
|
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 ListView deviceListView = requireViewById(R.id.settings_device_dialog_list);
|
||||||
final Button addDeviceButton = requireViewById(R.id.settings_device_add_button);
|
final Button addDeviceButton = requireViewById(R.id.settings_device_add_button);
|
||||||
deviceTextInputLayout = requireViewById(R.id.settings_device_input_device);
|
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);
|
deviceListView.setAdapter(adapter);
|
||||||
|
|
||||||
// Add the existing items in the list adapter so they will be displayed.
|
// Add the existing items in the list adapter so they will be displayed.
|
||||||
final CharSequence [] preferenceCharSeq = preference.getEntries();
|
final CharSequence [] preferenceCharSeq = preference.getEntries();
|
||||||
for (CharSequence item : preferenceCharSeq) {
|
for (CharSequence item : preferenceCharSeq) {
|
||||||
adapter.add(item.toString());
|
adapter.add(new Device(item.toString()));
|
||||||
}
|
}
|
||||||
|
|
||||||
// on-click handlers.
|
// on-click handlers.
|
||||||
|
@ -90,7 +92,7 @@ public class DeviceManagementDialog extends Dialog {
|
||||||
CharSequence[] entries = new CharSequence[itemsLength];
|
CharSequence[] entries = new CharSequence[itemsLength];
|
||||||
HashSet<String> entriesSet = new HashSet<>();
|
HashSet<String> entriesSet = new HashSet<>();
|
||||||
for (int i = 0; i < itemsLength; i++) {
|
for (int i = 0; i < itemsLength; i++) {
|
||||||
entries[i] = adapter.getItem(i);
|
entries[i] = adapter.getItem(i).toString();
|
||||||
entriesSet.add(entries[i].toString());
|
entriesSet.add(entries[i].toString());
|
||||||
}
|
}
|
||||||
preference.setEntries(entries);
|
preference.setEntries(entries);
|
||||||
|
@ -117,10 +119,15 @@ public class DeviceManagementDialog extends Dialog {
|
||||||
@Override
|
@Override
|
||||||
public void onClick(View v) {
|
public void onClick(View v) {
|
||||||
// TODO: Test connection before adding. loading -> testing connection -> show dialog.
|
// TODO: Test connection before adding. loading -> testing connection -> show dialog.
|
||||||
|
try {
|
||||||
EditText editText = Objects.requireNonNull(deviceTextInputLayout.getEditText());
|
EditText editText = Objects.requireNonNull(deviceTextInputLayout.getEditText());
|
||||||
String target = editText.getText().toString();
|
String target = editText.getText().toString();
|
||||||
adapter.add(target);
|
adapter.add(new Device(target));
|
||||||
editText.setText(null);
|
editText.setText("http://");
|
||||||
|
} catch (IllegalArgumentException e) {
|
||||||
|
Log.e(TAG, e.getMessage());
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,12 +4,12 @@
|
||||||
|
|
||||||
<!-- Reply Preference -->
|
<!-- Reply Preference -->
|
||||||
<string-array name="reply_entries">
|
<string-array name="reply_entries">
|
||||||
<item>Reply</item>
|
<item>http://localhost:9090/</item>
|
||||||
<item>Reply to all</item>
|
<item>http://google.com/</item>
|
||||||
</string-array>
|
</string-array>
|
||||||
|
|
||||||
<string-array name="reply_values">
|
<string-array name="reply_values">
|
||||||
<item>reply</item>
|
<item>http://localhost:9090/</item>
|
||||||
<item>reply_all</item>
|
<item>http://google.com/</item>
|
||||||
</string-array>
|
</string-array>
|
||||||
</resources>
|
</resources>
|
|
@ -36,7 +36,7 @@
|
||||||
<string name="device_management_title">Device Management</string>
|
<string name="device_management_title">Device Management</string>
|
||||||
<string name="settings_connect_new_device">Connect new device</string>
|
<string name="settings_connect_new_device">Connect new device</string>
|
||||||
<string name="settings_device_test_connection_btn">Add Device</string>
|
<string name="settings_device_test_connection_btn">Add Device</string>
|
||||||
<string name="settings_device_add_new_device_input_hint">hostname:port</string>
|
<string name="settings_device_add_new_device_input_hint">http://hostname:port</string>
|
||||||
<string name="settings_device_saved_devices">Saved Devices</string>
|
<string name="settings_device_saved_devices">Saved Devices</string>
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -11,7 +11,7 @@ import static org.junit.Assert.*;
|
||||||
public class DeviceTest {
|
public class DeviceTest {
|
||||||
@Test
|
@Test
|
||||||
public void test_constructionValidTargets() {
|
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) {
|
for (String target : testCases) {
|
||||||
Device device = new Device(target);
|
Device device = new Device(target);
|
||||||
assertEquals(device.getTarget(), target);
|
assertEquals(device.getTarget(), target);
|
||||||
|
@ -21,31 +21,11 @@ public class DeviceTest {
|
||||||
@Test
|
@Test
|
||||||
public void test_constructionValidHostsAndPorts() {
|
public void test_constructionValidHostsAndPorts() {
|
||||||
Random random = new Random();
|
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) {
|
for (String target : testCases) {
|
||||||
int port = random.nextInt(65535);
|
int port = random.nextInt(65535);
|
||||||
Device device = new Device(target, port);
|
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");
|
|
||||||
}
|
|
||||||
}
|
}
|
Loading…
Reference in a new issue