Commit 32ce1eb9 authored by akwizgran's avatar akwizgran

Show an error message if the address is invalid.

parent 30741380
......@@ -23,6 +23,6 @@ android {
}
dependencies {
implementation 'androidx.appcompat:appcompat:1.0.2'
implementation 'androidx.lifecycle:lifecycle-extensions:2.1.0'
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'androidx.lifecycle:lifecycle-extensions:2.2.0'
}
......@@ -5,16 +5,20 @@ import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import org.briarproject.interferometer.MainViewModel.ConnectionState;
import org.briarproject.interferometer.MainViewModel.StartResult;
import org.briarproject.interferometer.MainViewModel.TestPhase;
import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.ViewModelProviders;
import androidx.lifecycle.ViewModelProvider;
import static android.widget.Toast.LENGTH_SHORT;
import static org.briarproject.interferometer.MainViewModel.ConnectionState.DISCONNECTED;
import static org.briarproject.interferometer.MainViewModel.ConnectionState.DOWNLOADING;
import static org.briarproject.interferometer.MainViewModel.ConnectionState.UPLOADING;
import static org.briarproject.interferometer.MainViewModel.StartResult.INVALID_ADDRESS;
import static org.briarproject.interferometer.MainViewModel.TestPhase.READY;
public class MainActivity extends AppCompatActivity {
......@@ -30,7 +34,7 @@ public class MainActivity extends AppCompatActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
viewModel = ViewModelProviders.of(this).get(MainViewModel.class);
viewModel = new ViewModelProvider(this).get(MainViewModel.class);
serverAddressEditText = findViewById(R.id.server_address_edit_text);
throughputTextView = findViewById(R.id.throughput_text_view);
......@@ -66,7 +70,10 @@ public class MainActivity extends AppCompatActivity {
public void onTestClick(View view) {
TestPhase phase = viewModel.getTestPhase().getValue();
if (phase == null || phase == READY) {
viewModel.startTest(serverAddressEditText.getText().toString());
StartResult result = viewModel.startTest(serverAddressEditText.getText().toString());
if (result == INVALID_ADDRESS) {
Toast.makeText(this, R.string.server_address_error, LENGTH_SHORT).show();
}
} else {
viewModel.stopTest();
}
......
......@@ -23,6 +23,8 @@ import androidx.lifecycle.ViewModel;
import static org.briarproject.interferometer.MainViewModel.ConnectionState.DISCONNECTED;
import static org.briarproject.interferometer.MainViewModel.ConnectionState.DOWNLOADING;
import static org.briarproject.interferometer.MainViewModel.ConnectionState.UPLOADING;
import static org.briarproject.interferometer.MainViewModel.StartResult.INVALID_ADDRESS;
import static org.briarproject.interferometer.MainViewModel.StartResult.STARTED;
import static org.briarproject.interferometer.MainViewModel.TestPhase.DOWNLOAD;
import static org.briarproject.interferometer.MainViewModel.TestPhase.READY;
import static org.briarproject.interferometer.MainViewModel.TestPhase.UPLOAD;
......@@ -33,6 +35,8 @@ public class MainViewModel extends ViewModel {
enum ConnectionState {DISCONNECTED, DOWNLOADING, UPLOADING}
enum StartResult {INVALID_ADDRESS, STARTED}
private static final byte CONNECTION_TYPE_UPLOAD = 0;
private static final byte CONNECTION_TYPE_DOWNLOAD = 1;
private static final int BUFFER_SIZE = 4096;
......@@ -62,9 +66,9 @@ public class MainViewModel extends ViewModel {
}
@UiThread
void startTest(String serverAddress) {
StartResult startTest(String serverAddress) {
HostPort hostPort = parseServerAddress(serverAddress);
if (hostPort == null) return;
if (hostPort == null) return INVALID_ADDRESS;
task = executorService.submit(() -> {
try {
testPhase.postValue(DOWNLOAD);
......@@ -86,6 +90,7 @@ public class MainViewModel extends ViewModel {
testPhase.postValue(READY);
}
});
return STARTED;
}
@UiThread
......
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="colorPrimary">#008577</color>
<color name="colorPrimaryDark">#00574B</color>
<color name="colorAccent">#D81B60</color>
<color name="colorPrimary">#007766</color>
<color name="colorPrimaryDark">#004433</color>
<color name="colorAccent">#00AA99</color>
</resources>
......@@ -2,6 +2,7 @@
<string name="app_name">Interferometer</string>
<string name="server_address">Server address</string>
<string name="server_address_error">Invalid address</string>
<string name="start_test">Start test</string>
<string name="stop_test">Stop test</string>
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment