Move UI things to UI thread

This commit is contained in:
Matthew Wong 2015-08-14 17:35:43 -04:00
parent 838471ec3a
commit eced1dd314
3 changed files with 25 additions and 25 deletions

View file

@ -153,21 +153,19 @@ public class SshKeyGen extends AppCompatActivity {
} }
} }
private class generateTask extends AsyncTask<View, Void, Exception> { private class generateTask extends AsyncTask<String, Void, Exception> {
private ProgressDialog pd; private ProgressDialog pd;
protected Exception doInBackground(View... views) { @Override
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); protected void onPreExecute() {
imm.hideSoftInputFromWindow(views[0].getWindowToken(), 0); super.onPreExecute();
pd = ProgressDialog.show(SshKeyGen.this, "", "Generating keys");
}
Spinner spinner = (Spinner) findViewById(R.id.length); protected Exception doInBackground(String... strings) {
int length = (Integer) spinner.getSelectedItem(); int length = Integer.parseInt(strings[0]);
String passphrase = strings[1];
EditText editText = (EditText) findViewById(R.id.passphrase); String comment = strings[2];
String passphrase = editText.getText().toString();
editText = (EditText) findViewById(R.id.comment);
String comment = editText.getText().toString();
JSch jsch = new JSch(); JSch jsch = new JSch();
try { try {
@ -192,13 +190,6 @@ public class SshKeyGen extends AppCompatActivity {
} }
} }
@Override
protected void onPreExecute() {
super.onPreExecute();
pd = ProgressDialog.show(SshKeyGen.this, "", "Generating keys");
}
@Override @Override
protected void onPostExecute(Exception e) { protected void onPostExecute(Exception e) {
super.onPostExecute(e); super.onPostExecute(e);
@ -230,6 +221,12 @@ public class SshKeyGen extends AppCompatActivity {
// private and public key, then replaces the SshKeyGenFragment with a // private and public key, then replaces the SshKeyGenFragment with a
// ShowSshKeyFragment which displays the public key. // ShowSshKeyFragment which displays the public key.
public void generate(View view) { public void generate(View view) {
new generateTask().execute(view); String length = Integer.toString((Integer) ((Spinner) findViewById(R.id.length)).getSelectedItem());
String passphrase = ((EditText) findViewById(R.id.passphrase)).getText().toString();
String comment = ((EditText) findViewById(R.id.comment)).getText().toString();
new generateTask().execute(length, passphrase, comment);
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
} }
} }

View file

@ -86,7 +86,7 @@ public class UserPreference extends AppCompatActivity {
findPreference("ssh_keygen").setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() { findPreference("ssh_keygen").setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
@Override @Override
public boolean onPreferenceClick(Preference preference) { public boolean onPreferenceClick(Preference preference) {
callingActivity.makeSshKey(); callingActivity.makeSshKey(true);
return true; return true;
} }
}); });
@ -202,7 +202,7 @@ public class UserPreference extends AppCompatActivity {
getSshKey(); getSshKey();
break; break;
case "make_ssh_key": case "make_ssh_key":
makeSshKey(); makeSshKey(false);
break; break;
case "git_external": case "git_external":
selectExternalGitRepository(); selectExternalGitRepository();
@ -252,12 +252,14 @@ public class UserPreference extends AppCompatActivity {
/** /**
* Opens a key generator to generate a public/private key pair * Opens a key generator to generate a public/private key pair
*/ */
public void makeSshKey() { public void makeSshKey(boolean fromPreferences) {
Intent intent = new Intent(getApplicationContext(), SshKeyGen.class); Intent intent = new Intent(getApplicationContext(), SshKeyGen.class);
startActivity(intent); startActivity(intent);
if (!fromPreferences) {
setResult(RESULT_OK); setResult(RESULT_OK);
finish(); finish();
} }
}
private void copySshKey(Uri uri) throws IOException { private void copySshKey(Uri uri) throws IOException {
InputStream sshKey = this.getContentResolver().openInputStream(uri); InputStream sshKey = this.getContentResolver().openInputStream(uri);

View file

@ -21,6 +21,7 @@
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_marginLeft="16dp" android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:text="@string/ssh_keygen_tip" android:text="@string/ssh_keygen_tip"
android:textAppearance="?android:attr/textAppearanceMedium"/> android:textAppearance="?android:attr/textAppearanceMedium"/>