@@ 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;
});