~xdavidwu/saf-sftp

8f56b8c38654133131fa28a3f4061954ccac1bc9 — xdavidwu 2 years ago d85756f
MainActivity: run connection test on another thread
1 files changed, 23 insertions(+), 18 deletions(-)

M src/main/java/org/safsftp/MainActivity.java
M src/main/java/org/safsftp/MainActivity.java => src/main/java/org/safsftp/MainActivity.java +23 -18
@@ 8,6 8,7 @@ import android.preference.EditTextPreference;
import android.preference.Preference;
import android.preference.PreferenceActivity;
import android.provider.DocumentsContract;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.StrictMode;
import android.util.Log;


@@ 36,30 37,34 @@ public class MainActivity extends PreferenceActivity implements OnSharedPreferen
		mountpointText = (EditTextPreference) findPreference("mountpoint");
		Preference testConnection = findPreference("test_connection");
		testConnection.setOnPreferenceClickListener((p) -> {
			StrictMode.setThreadPolicy(StrictMode.ThreadPolicy.LAX);
			SharedPreferences settings = MainActivity.this.
				getPreferenceScreen().getSharedPreferences();
			Connection connection = new Connection(
				settings.getString("host", ""),
				Integer.parseInt(settings.getString("port", "22")));
			String result = "Succeed.";
			try {
				connection.connect(null, 10000, 10000);
				if (!connection.authenticateWithPassword(settings.getString("username", ""),
					    settings.getString("passwd", ""))) {
					result = "Authentication failed.";
					connection.close();
					return true;
			AsyncTask.execute(() -> {
				String result = "Succeed.";
				try {
					connection.connect(null, 10000, 10000);
					if (!connection.authenticateWithPassword(
							settings.getString("username", ""),
							settings.getString("passwd", ""))) {
						result = "Authentication failed.";
						connection.close();
					}
				} catch (IOException e) {
					result = e.getMessage();
				}
			} catch (IOException e) {
				result = e.getMessage();
			}
			connection.close();
			new AlertDialog.Builder(MainActivity.this)
				.setTitle("Connection test result")
				.setMessage(result)
				.setPositiveButton("OK", null)
				.show();
				connection.close();
				final String message = result;
				MainActivity.this.runOnUiThread(() -> {
					new AlertDialog.Builder(MainActivity.this)
						.setTitle("Connection test result")
						.setMessage(message)
						.setPositiveButton("OK", null)
						.show();
				});
			});
			return true;
		});