Browse Source

Form FTP download URI properly

Only send the scheme and host in the intent data Uri and construct an absolute path for every file.
Hopefully that fixes all "file not found" and "cannot change directory" issues with AndFTP.
pull/176/head
Uranium235 10 years ago
parent
commit
a0c48bdb1f
  1. 20
      app/src/main/java/org/transdroid/core/gui/DetailsFragment.java

20
app/src/main/java/org/transdroid/core/gui/DetailsFragment.java

@ -432,13 +432,19 @@ public class DetailsFragment extends Fragment implements OnTrackersUpdatedListen
String urlBase = currentServerSettings.getFtpUrl(); String urlBase = currentServerSettings.getFtpUrl();
if (urlBase == null || urlBase.equals("")) if (urlBase == null || urlBase.equals(""))
urlBase = "ftp://" + currentServerSettings.getAddress(); urlBase = "ftp://" + currentServerSettings.getAddress();
if (!urlBase.endsWith("/"))
urlBase = urlBase + "/";
Uri urlBaseUri = Uri.parse(urlBase);
urlBaseUri = urlBaseUri.normalizeScheme();
String basePath = urlBaseUri.getPath();
// Try using AndFTP intents // Try using AndFTP intents
Intent andftpStart = new Intent(Intent.ACTION_PICK); Intent andftpStart = new Intent(Intent.ACTION_PICK);
andftpStart.setDataAndType(Uri.parse(urlBase), "vnd.android.cursor.dir/lysesoft.andftp.uri"); andftpStart.setDataAndType(Uri.fromParts(urlBaseUri.getScheme(), urlBaseUri.getAuthority(), null),
"vnd.android.cursor.dir/lysesoft.andftp.uri");
andftpStart.putExtra("command_type", "download"); andftpStart.putExtra("command_type", "download");
andftpStart.putExtra("ftp_pasv", "true"); andftpStart.putExtra("ftp_pasv", "true");
if (Uri.parse(urlBase).getUserInfo() != null) if (urlBaseUri.getUserInfo() != null)
andftpStart.putExtra("ftp_username", Uri.parse(urlBase).getUserInfo()); andftpStart.putExtra("ftp_username", Uri.parse(urlBase).getUserInfo());
else else
andftpStart.putExtra("ftp_username", currentServerSettings.getUsername()); andftpStart.putExtra("ftp_username", currentServerSettings.getUsername());
@ -450,16 +456,14 @@ public class DetailsFragment extends Fragment implements OnTrackersUpdatedListen
} }
// Note: AndFTP doesn't understand the directory that Environment.getExternalStoragePublicDirectory() // Note: AndFTP doesn't understand the directory that Environment.getExternalStoragePublicDirectory()
// uses :( // uses :(
// Todo: Let user choose the download directory / make it configurable
andftpStart.putExtra("local_folder", "/sdcard/Download"); andftpStart.putExtra("local_folder", "/sdcard/Download");
for (int f = 0; f < checked.size(); f++) { for (int f = 0; f < checked.size(); f++) {
String file = checked.get(f).getRelativePath(); String file = checked.get(f).getRelativePath();
if (file != null) { if (file != null) {
// If the file is directly in the root, AndFTP fails if we supply the proper path (like if (file.startsWith("/"))
// /file.pdf)
// Work around this bug by removing the leading / if no further directories are used in the path
if (file.startsWith("/") && file.indexOf("/", 1) < 0)
file = file.substring(1); file = file.substring(1);
andftpStart.putExtra("remote_file" + (f + 1), file); andftpStart.putExtra("remote_file" + (f + 1), basePath + file);
} }
} }
if (andftpStart.resolveActivity(getActivity().getPackageManager()) != null) { if (andftpStart.resolveActivity(getActivity().getPackageManager()) != null) {
@ -469,7 +473,7 @@ public class DetailsFragment extends Fragment implements OnTrackersUpdatedListen
} }
// Try using a VIEW intent given an ftp:// scheme URI // Try using a VIEW intent given an ftp:// scheme URI
String url = urlBase + checked.get(0).getFullPath(); String url = urlBase + checked.get(0).getRelativePath();
Intent simpleStart = new Intent(Intent.ACTION_VIEW, Uri.parse(url)) Intent simpleStart = new Intent(Intent.ACTION_VIEW, Uri.parse(url))
.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); .setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
if (simpleStart.resolveActivity(getActivity().getPackageManager()) != null) { if (simpleStart.resolveActivity(getActivity().getPackageManager()) != null) {

Loading…
Cancel
Save