FTP: Add custom path support

This commit is contained in:
Vishal
2017-03-07 13:19:50 +05:30
parent 0d1b8c2e93
commit 00b4973e1a
4 changed files with 78 additions and 7 deletions
@@ -31,6 +31,8 @@ import com.amaze.filemanager.activities.MainActivity;
import com.amaze.filemanager.services.ftpservice.FTPService;
import com.amaze.filemanager.utils.theme.AppTheme;
import java.io.File;
/**
* Created by yashwanthreddyg on 10-06-2016.
*/
@@ -113,21 +115,71 @@ public class FTPServerFragment extends Fragment {
int portNumber = Integer.parseInt(name);
if (portNumber < 1024) {
Toast.makeText(getActivity(), R.string.ftp_port_change_error_invalid, Toast.LENGTH_SHORT)
.show();
.show();
} else {
FTPService.changeFTPServerPort(preferences, portNumber);
Toast.makeText(getActivity(), R.string.ftp_port_change_success, Toast.LENGTH_SHORT)
.show();
.show();
}
}
}
})
.positiveText("CHANGE")
.positiveText(getResources().getString(R.string.change).toUpperCase())
.negativeText(R.string.cancel)
.build()
.show();
return true;
case R.id.ftp_path:
MaterialDialog.Builder dialogBuilder = new MaterialDialog.Builder(getActivity());
dialogBuilder.title(getString(R.string.ftp_path));
dialogBuilder.input(getString(R.string.ftp_path_hint),
FTPService.getDefaultPathFromPreferences(preferences),
false, new MaterialDialog.InputCallback() {
@Override
public void onInput(@NonNull MaterialDialog dialog, CharSequence input) {
}
});
dialogBuilder.onPositive(new MaterialDialog.SingleButtonCallback() {
@Override
public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
EditText editText = dialog.getInputEditText();
if (editText != null) {
String path = editText.getText().toString();
File pathFile = new File(path);
if (pathFile.exists() && pathFile.isDirectory()) {
FTPService.changeFTPServerPath(preferences, pathFile.getPath());
Toast.makeText(getActivity(), R.string.ftp_path_change_success,
Toast.LENGTH_SHORT)
.show();
} else {
// try to get parent
File pathParentFile = new File(pathFile.getParent());
if (pathParentFile.exists() && pathParentFile.isDirectory()) {
FTPService.changeFTPServerPath(preferences, pathParentFile.getPath());
Toast.makeText(getActivity(), R.string.ftp_path_change_success,
Toast.LENGTH_SHORT)
.show();
} else {
// don't have access, print error
Toast.makeText(getActivity(), R.string.ftp_path_change_error_invalid,
Toast.LENGTH_SHORT)
.show();
}
}
}
}
});
dialogBuilder.positiveText(getResources().getString(R.string.change).toUpperCase())
.negativeText(R.string.cancel)
.build()
.show();
}
return false;
@@ -45,6 +45,8 @@ public class FTPService extends Service implements Runnable {
public static final int DEFAULT_PORT = 2211;
public static final String PORT_PREFERENCE_KEY = "ftpPort";
public static final String KEY_PREFERENCE_PATH = "ftp_path";
public static final String DEFAULT_PATH = Environment.getExternalStorageDirectory().getAbsolutePath();
public static int getDefaultPortFromPreferences(SharedPreferences preferences) {
try {
@@ -57,13 +59,22 @@ public class FTPService extends Service implements Runnable {
}
}
public static String getDefaultPathFromPreferences(SharedPreferences preferences) {
return preferences.getString(KEY_PREFERENCE_PATH, DEFAULT_PATH);
}
public static void changeFTPServerPort(SharedPreferences preferences, int port) {
preferences.edit()
.putInt(PORT_PREFERENCE_KEY, port)
.apply();
}
public static void changeFTPServerPath(SharedPreferences preferences, String path) {
preferences.edit().putString(KEY_PREFERENCE_PATH, path).apply();
}
private static final String TAG = FTPService.class.getSimpleName();
/**
* TODO: 25/10/16 This is ugly
*/
@@ -125,8 +136,6 @@ public class FTPService extends Service implements Runnable {
serverFactory.setConnectionConfig(connectionConfigFactory.createConnectionConfig());
String path = Environment.getExternalStorageDirectory().getAbsolutePath();
BaseUser user = new BaseUser();
if (!isPasswordProtected) {
user.setName("anonymous");
@@ -135,7 +144,7 @@ public class FTPService extends Service implements Runnable {
user.setPassword(password);
}
user.setHomeDirectory(path);
user.setHomeDirectory(getDefaultPathFromPreferences(preferences));
List<Authority> list = new ArrayList<>();
list.add(new WritePermission());
user.setAuthorities(list);
+3
View File
@@ -3,4 +3,7 @@
<item
android:id="@+id/choose_ftp_port"
android:title="@string/ftp_port_edit_menu_title"/>
<item
android:id="@+id/ftp_path"
android:title="@string/ftp_path" />
</menu>
+7
View File
@@ -571,5 +571,12 @@
<string name="service_timer">Time Elapsed</string>
<string name="please_wait">Please Wait</string>
<string name="no_applications">No applications</string>
<string name="ftp_path">Shared Path</string>
<string name="ftp_path_hint">Path to the root of directory to share</string>
<string name="error_ftp_path_file">Path not a directory</string>
<string name="change">Change</string>
<string name="ftp_path_change_success">FTP path changed successfully</string>
<string name="ftp_path_change_error_invalid">Invalid path</string>
</resources>