104 lines
3.6 KiB
Java
104 lines
3.6 KiB
Java
package im.ricochet.androidod;
|
|
|
|
import android.content.SharedPreferences;
|
|
import android.os.AsyncTask;
|
|
import android.os.StrictMode;
|
|
import android.support.v7.app.AppCompatActivity;
|
|
import android.os.Bundle;
|
|
import android.util.Log;
|
|
import android.view.View;
|
|
import android.widget.Button;
|
|
import android.widget.ProgressBar;
|
|
import android.widget.TextView;
|
|
|
|
import java.io.BufferedReader;
|
|
import java.io.InputStreamReader;
|
|
import java.io.PrintWriter;
|
|
import java.net.Socket;
|
|
|
|
import goRicochetMobile.GoRicochetMobile;
|
|
|
|
public class ConnectActivity extends AppCompatActivity {
|
|
|
|
public static final String PREFERNCE_FILE = "im.ricochet.PREFERENCE_FILE";
|
|
private static final String TAG = "InitActivity";
|
|
private static final String PRIVATE_KEY_KEY = "privateKey";
|
|
|
|
@Override
|
|
protected void onCreate(Bundle savedInstanceState) {
|
|
// Load identity and connect to addr. Enable appropriate buttons
|
|
// If no identity, generate.
|
|
|
|
super.onCreate(savedInstanceState);
|
|
setContentView(im.ricochet.androidod.R.layout.activity_connect);
|
|
|
|
TextView progressText = (TextView)findViewById(im.ricochet.androidod.R.id.progressTextView);
|
|
//progressText.setText("Loading private key...");
|
|
|
|
SharedPreferences prefs = getSharedPreferences(PREFERNCE_FILE, MODE_PRIVATE);
|
|
String privateKey = prefs.getString(PRIVATE_KEY_KEY, "");
|
|
if (privateKey.equals("")) {
|
|
// Kick of identity generateion
|
|
regenIdentity();
|
|
} else {
|
|
Button regenButton = (Button)findViewById(R.id.regenIdentButton);
|
|
regenButton.setEnabled(true);
|
|
}
|
|
|
|
/*Log.i(TAG, "Ready!");
|
|
progressText.setText("Ready!");
|
|
ProgressBar progressBar = (ProgressBar)findViewById(im.ricochet.androidod.R.id.progressBar);
|
|
progressBar.setVisibility(View.GONE);
|
|
|
|
Log.i(TAG, "Setting thread policy perms");
|
|
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
|
|
StrictMode.setThreadPolicy(policy);
|
|
|
|
Log.i(TAG, "Starting Echo Bot");
|
|
//GoRicochetMobile.echoBot(privateKey);
|
|
new EchoBot(privateKey).execute();
|
|
*/
|
|
}
|
|
|
|
private void regenIdentity() {
|
|
Button regenButton = (Button)findViewById(R.id.regenIdentButton);
|
|
regenButton.setEnabled(false);
|
|
TextView progressText = (TextView)findViewById(im.ricochet.androidod.R.id.progressTextView);
|
|
progressText.setText("Generating new identity...");
|
|
ProgressBar spinner = (ProgressBar)findViewById(R.id.progressBar);
|
|
spinner.setVisibility(View.VISIBLE);
|
|
|
|
String privateKey;
|
|
try {
|
|
privateKey = GoRicochetMobile.generatePrivateKey();
|
|
|
|
SharedPreferences prefs = getSharedPreferences(PREFERNCE_FILE, MODE_PRIVATE);
|
|
SharedPreferences.Editor prefsEditor = prefs.edit();
|
|
prefsEditor.putString(PRIVATE_KEY_KEY, privateKey);
|
|
prefsEditor.commit();
|
|
progressText.setText("");
|
|
} catch (Exception e) {
|
|
Log.e(TAG, e.toString());
|
|
progressText.setText("ERROR: unable to generate new identity: " + e.toString());
|
|
}
|
|
|
|
spinner.setVisibility(View.INVISIBLE);
|
|
regenButton.setEnabled(true);
|
|
}
|
|
|
|
private class EchoBot extends AsyncTask<Void, Void, Void> {
|
|
String privateKey;
|
|
|
|
public EchoBot(String privateKey) {
|
|
this.privateKey = privateKey;
|
|
}
|
|
|
|
@Override
|
|
public Void doInBackground(Void... voids) {
|
|
GoRicochetMobile.echoBot(privateKey);
|
|
return null;
|
|
}
|
|
|
|
}
|
|
}
|