~xdavidwu/saf-sftp

d85756f3a141178216efa30b6bd1a67d6b19eed0 — xdavidwu 2 years ago 43527c3
MainActivity: add a button for testing connection
2 files changed, 39 insertions(+), 0 deletions(-)

M src/main/java/org/safsftp/MainActivity.java
M src/main/res/xml/main_pre.xml
M src/main/java/org/safsftp/MainActivity.java => src/main/java/org/safsftp/MainActivity.java +36 -0
@@ 1,12 1,20 @@
package org.safsftp;

import android.app.AlertDialog;
import android.content.SharedPreferences;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.net.Uri;
import android.preference.EditTextPreference;
import android.preference.Preference;
import android.preference.PreferenceActivity;
import android.provider.DocumentsContract;
import android.os.Bundle;
import android.os.StrictMode;
import android.util.Log;

import com.trilead.ssh2.Connection;

import java.io.IOException;

public class MainActivity extends PreferenceActivity implements OnSharedPreferenceChangeListener {
	private EditTextPreference hostText, portText, usernameText, passwdText, mountpointText;


@@ 26,6 34,34 @@ public class MainActivity extends PreferenceActivity implements OnSharedPreferen
		usernameText = (EditTextPreference) findPreference("username");
		passwdText = (EditTextPreference) findPreference("passwd");
		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;
				}
			} catch (IOException e) {
				result = e.getMessage();
			}
			connection.close();
			new AlertDialog.Builder(MainActivity.this)
				.setTitle("Connection test result")
				.setMessage(result)
				.setPositiveButton("OK", null)
				.show();
			return true;
		});

		SharedPreferences settings = getPreferenceScreen().getSharedPreferences();
		settings.registerOnSharedPreferenceChangeListener(this);

M src/main/res/xml/main_pre.xml => src/main/res/xml/main_pre.xml +3 -0
@@ 21,4 21,7 @@
			android:inputType="textPassword"
			android:key="passwd" />
	</PreferenceCategory>
	<Preference android:key="test_connection"
		android:title="Test connection"
		android:summary="Attempt a connection" />
</PreferenceScreen>