validate input onion address, save, enable connect button

This commit is contained in:
Dan Ballard 2017-09-05 08:17:26 -07:00
parent 3da1cc5032
commit 6baa48596b
2 changed files with 51 additions and 10 deletions

View File

@ -4,9 +4,12 @@ import android.content.SharedPreferences;
import android.os.AsyncTask; import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity; import android.support.v7.app.AppCompatActivity;
import android.os.Bundle; import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log; import android.util.Log;
import android.view.View; import android.view.View;
import android.widget.Button; import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar; import android.widget.ProgressBar;
import android.widget.TextView; import android.widget.TextView;
@ -14,9 +17,12 @@ import goRicochetMobile.GoRicochetMobile;
public class ConnectActivity extends AppCompatActivity { public class ConnectActivity extends AppCompatActivity {
private static final int ONION_ADDRESS_LENGTH = 16;
public static final String PREFERNCE_FILE = "im.ricochet.PREFERENCE_FILE"; public static final String PREFERNCE_FILE = "im.ricochet.PREFERENCE_FILE";
private static final String TAG = "InitActivity"; private static final String TAG = "InitActivity";
private static final String PRIVATE_KEY_KEY = "privateKey"; private static final String PRIVATE_KEY_KEY = "privateKey";
private static final String CONNECT_TO_ADDRESS_KEY = "connectToAddress";
private static final String RICOCHET_ADDRESS_PREFIX = "ricochet:";
Button regenButton; Button regenButton;
Button connectButton; Button connectButton;
@ -25,8 +31,9 @@ public class ConnectActivity extends AppCompatActivity {
TextView connectStatusText; TextView connectStatusText;
TextView regenIdentStatusText; TextView regenIdentStatusText;
TextView idetityText; TextView idetityText;
EditText addressText;
SharedPreferences prefs;
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
@ -43,6 +50,9 @@ public class ConnectActivity extends AppCompatActivity {
connectButton = (Button)findViewById(R.id.connectButton); connectButton = (Button)findViewById(R.id.connectButton);
connectSpinner = (ProgressBar)findViewById(R.id.connectProgressBar); connectSpinner = (ProgressBar)findViewById(R.id.connectProgressBar);
regenIdentSpinner = (ProgressBar)findViewById(R.id.regenIdentProgressBar); regenIdentSpinner = (ProgressBar)findViewById(R.id.regenIdentProgressBar);
addressText = (EditText)findViewById(R.id.addressText);
prefs = getSharedPreferences(PREFERNCE_FILE, MODE_PRIVATE);
regenButton.setOnClickListener(new View.OnClickListener() { regenButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) { public void onClick(View v) {
@ -51,7 +61,21 @@ public class ConnectActivity extends AppCompatActivity {
} }
}); });
SharedPreferences prefs = getSharedPreferences(PREFERNCE_FILE, MODE_PRIVATE); addressText.addTextChangedListener(new TextWatcher(){
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
validateOnionAddress();
}
@Override
public void afterTextChanged(Editable s) {
}
});
String privateKey = prefs.getString(PRIVATE_KEY_KEY, ""); String privateKey = prefs.getString(PRIVATE_KEY_KEY, "");
Log.i(TAG, "Private key loaded:\n"+privateKey); Log.i(TAG, "Private key loaded:\n"+privateKey);
if (privateKey.equals("")) { if (privateKey.equals("")) {
@ -65,6 +89,10 @@ public class ConnectActivity extends AppCompatActivity {
regenButton.setEnabled(true); regenButton.setEnabled(true);
} }
String connectToAddress = prefs.getString(CONNECT_TO_ADDRESS_KEY, RICOCHET_ADDRESS_PREFIX);
addressText.setText(connectToAddress);
validateOnionAddress();
/*Log.i(TAG, "Ready!"); /*Log.i(TAG, "Ready!");
connectStatusText.setText("Ready!"); connectStatusText.setText("Ready!");
ProgressBar progressBar = (ProgressBar)findViewById(im.ricochet.androidod.R.id.progressBar); ProgressBar progressBar = (ProgressBar)findViewById(im.ricochet.androidod.R.id.progressBar);
@ -87,6 +115,18 @@ public class ConnectActivity extends AppCompatActivity {
idetityText.setText("ricochet:" + addr); idetityText.setText("ricochet:" + addr);
} }
private void validateOnionAddress() {
String address = addressText.getText().toString();
if (address.startsWith(RICOCHET_ADDRESS_PREFIX) && address.length() == ONION_ADDRESS_LENGTH + RICOCHET_ADDRESS_PREFIX.length()) {
SharedPreferences.Editor prefsEditor = prefs.edit();
prefsEditor.putString(CONNECT_TO_ADDRESS_KEY, address);
prefsEditor.commit();
connectButton.setEnabled(true);
} else {
connectButton.setEnabled(false);
}
}
private class RegenIdentTask extends AsyncTask<Void, Void, String> { private class RegenIdentTask extends AsyncTask<Void, Void, String> {
@Override @Override
protected void onPreExecute() { protected void onPreExecute() {
@ -104,7 +144,6 @@ public class ConnectActivity extends AppCompatActivity {
try { try {
privateKey = GoRicochetMobile.generatePrivateKey(); privateKey = GoRicochetMobile.generatePrivateKey();
SharedPreferences prefs = getSharedPreferences(PREFERNCE_FILE, MODE_PRIVATE);
SharedPreferences.Editor prefsEditor = prefs.edit(); SharedPreferences.Editor prefsEditor = prefs.edit();
prefsEditor.putString(PRIVATE_KEY_KEY, privateKey); prefsEditor.putString(PRIVATE_KEY_KEY, privateKey);
prefsEditor.commit(); prefsEditor.commit();
@ -120,7 +159,7 @@ public class ConnectActivity extends AppCompatActivity {
Log.i(TAG, "RegenIdentTask.onPostExecute(): " + privateKey); Log.i(TAG, "RegenIdentTask.onPostExecute(): " + privateKey);
regenIdentSpinner.setVisibility(View.INVISIBLE); regenIdentSpinner.setVisibility(View.INVISIBLE);
regenButton.setEnabled(true); regenButton.setEnabled(true);
connectButton.setEnabled(true); validateOnionAddress();
if (privateKey == null) { if (privateKey == null) {
regenIdentStatusText.setText("ERROR: unable to generate new identity"); regenIdentStatusText.setText("ERROR: unable to generate new identity");
} else { } else {

View File

@ -20,20 +20,22 @@
<EditText <EditText
android:id="@+id/addressText" android:id="@+id/addressText"
android:layout_width="wrap_content" android:layout_width="241dp"
android:layout_height="wrap_content" android:layout_height="42dp"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp" android:layout_marginLeft="8dp"
android:layout_marginRight="8dp" android:layout_marginRight="8dp"
android:layout_marginTop="8dp" android:layout_marginStart="8dp"
android:layout_marginTop="9dp"
android:ems="10" android:ems="10"
android:inputType="text" android:inputType="text"
android:minEms="26"
android:text="ricochet:" android:text="ricochet:"
app:layout_constraintHorizontal_bias="0.503" app:layout_constraintHorizontal_bias="0.518"
app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent" app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView" app:layout_constraintTop_toBottomOf="@+id/textView"
android:layout_marginStart="8dp" tools:layout_editor_absoluteX="50dp" />
android:layout_marginEnd="8dp" />
<TextView <TextView
android:id="@+id/textView" android:id="@+id/textView"