Skip to content

Commit

Permalink
Makes SshConnectionPool.removeConnection() to run as AsyncTask (#1792)
Browse files Browse the repository at this point in the history
Makes SshConnectionPool.removeConnection() to run as AsyncTask
  • Loading branch information
EmmanuelMess committed Jan 21, 2020
1 parent 27c650a commit 7f03ccf
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import android.os.AsyncTask;
import android.util.Log;

import com.amaze.filemanager.activities.MainActivity;
Expand Down Expand Up @@ -89,12 +91,8 @@ public static final SshConnectionPool getInstance() {
*
* @param url SSH connection URI
*/
public void removeConnection(@NonNull String url) {
url = SshClientUtils.extractBaseUriFrom(url);

if(connections.containsKey(url)) {
SshClientUtils.tryDisconnect(connections.remove(url));
}
public void removeConnection(@NonNull String url, @NonNull Runnable callback) {
new AsyncRemoveConnection(url, callback).execute();
}

/**
Expand Down Expand Up @@ -278,4 +276,31 @@ static final class ConnectionInfo {
this.port = port;
}
}

private final class AsyncRemoveConnection extends AsyncTask<Void, Void, Void> {

private String url;
private Runnable callback;

AsyncRemoveConnection(@NonNull String url, @Nullable Runnable callback) {
this.url = url;
this.callback = callback;
}

@Override
protected Void doInBackground(Void... voids) {
url = SshClientUtils.extractBaseUriFrom(url);

if(connections.containsKey(url)) {
SshClientUtils.tryDisconnect(connections.remove(url));
}
return null;
}

@Override
protected void onPostExecute(Void aVoid) {
if(callback != null)
callback.run();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -173,34 +173,31 @@ username, getArguments().getString("password", null),
selectedParsedKeyPair));

if (!isEmpty(sshHostKey)) {
SshConnectionPool.getInstance().removeConnection(
SshClientUtils.deriveSftpPathFrom(hostname, port, username, password,
selectedParsedKeyPair));

//Verify SSH host key fingerprint by hand, prompt user to override if not match
new GetSshHostFingerprintTask(hostname, port, taskResult -> {
PublicKey hostKey = taskResult.result;
if(hostKey != null) {
final String hostKeyFingerprint = SecurityUtils.getFingerprint(hostKey);
if(hostKeyFingerprint.equals(sshHostKey)){
authenticateAndSaveSetup(connectionName, hostname, port, sshHostKey,
username, password, selectedParsedKeyPairName,
selectedParsedKeyPair, edit);
} else {
new AlertDialog.Builder(context)
.setTitle(R.string.ssh_connect_failed_host_key_changed_title)
.setMessage(R.string.ssh_connect_failed_host_key_changed_prompt)
.setPositiveButton(R.string.update_host_key, (dialog1, which1) -> {
authenticateAndSaveSetup(connectionName, hostname, port,
hostKeyFingerprint, username, password,
selectedParsedKeyPairName, selectedParsedKeyPair, edit);
})
.setNegativeButton(R.string.cancel_recommended, (dialog1, which1) -> dialog1.dismiss())
.show();
SshConnectionPool.getInstance().removeConnection(SshClientUtils.deriveSftpPathFrom(hostname, port, username, password,
selectedParsedKeyPair), () -> {
new GetSshHostFingerprintTask(hostname, port, taskResult -> {
PublicKey hostKey = taskResult.result;
if(hostKey != null) {
final String hostKeyFingerprint = SecurityUtils.getFingerprint(hostKey);
if(hostKeyFingerprint.equals(sshHostKey)){
authenticateAndSaveSetup(connectionName, hostname, port, sshHostKey,
username, password, selectedParsedKeyPairName,
selectedParsedKeyPair, edit);
} else {
new AlertDialog.Builder(context)
.setTitle(R.string.ssh_connect_failed_host_key_changed_title)
.setMessage(R.string.ssh_connect_failed_host_key_changed_prompt)
.setPositiveButton(R.string.update_host_key, (dialog1, which1) -> {
authenticateAndSaveSetup(connectionName, hostname, port,
hostKeyFingerprint, username, password,
selectedParsedKeyPairName, selectedParsedKeyPair, edit);
})
.setNegativeButton(R.string.cancel_recommended, (dialog1, which1) -> dialog1.dismiss())
.show();
}
}
}
}).execute();

}).execute();
});

} else {
new GetSshHostFingerprintTask(hostname, port, taskResult -> {
Expand Down

0 comments on commit 7f03ccf

Please sign in to comment.