Set independence between node names and showed names on Download Module.

Changed name of the downloaded file, now is taken from the node name
instead of the name from the url
This commit is contained in:
Helena Rodriguez 2012-12-13 15:01:54 +01:00
parent 77d747079b
commit df73cffb4a
3 changed files with 55 additions and 18 deletions

View File

@ -82,6 +82,28 @@ public class DirectoryNavigator
//return itemsToShow;
return currentItems;
}
/**
* Travel inside a subdirectory.
* @param directoryPosition The position of the subdirectory where we will travel.
* @return Return a list of items that are inside the subdirectory.
* @throws InvalidPath When the directory don't exist.
*/
public ArrayList<DirectoryItem> goToSubDirectory(int directoryPosition) throws InvalidPath
{
String subDirectory = currentItems.get(directoryPosition).getName();
//We increase the path.
path.add(subDirectory);
Node node = goToDirectory();
//ArrayList<DirectoryItem> itemsToShow;
//itemsToShow = new ArrayList<DirectoryItem>(getItems(node));
currentItems= new ArrayList<DirectoryItem>(getItems(node));
//return itemsToShow;
return currentItems;
}
/**
* Travel to the parent directory.
@ -353,8 +375,7 @@ public class DirectoryNavigator
/**
*Searches for a node in the current directory with the given name
*@param name Name of the node located on the current directory.
*@returns -1 in case it does not exists any node with the given name
* directoryItem with the given name
*@returns null in case it does not exists any node with the given name
* */
public DirectoryItem getDirectoryItem(String name){
DirectoryItem node = null;
@ -374,6 +395,20 @@ public class DirectoryNavigator
return node;
}
/**
*Searches for a node in the current directory with the given position
*@param position position where the node is located
*@returns null in case it does not exists any node located at the given position
* */
public DirectoryItem getDirectoryItem(int position){
DirectoryItem node = null;
if(position >=0 && position < currentItems.size()){
node = currentItems.get(position);
}
return node;
}
/**
* Identifies the node with name @a name and gets its file code in case the node is a file. In case the node is a directory returns -1
* @param name Name of the node located on the current directory.

View File

@ -159,12 +159,15 @@ public class DownloadsManager extends MenuActivity {
grid.setOnItemClickListener((new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v,
int position, long id) {
TextView text = (TextView) v.findViewById(R.id.icon_text);
chosenNodeName = text.getText().toString();
DirectoryItem node = navigator.getDirectoryItem(chosenNodeName);
//TextView text = (TextView) v.findViewById(R.id.icon_text);
//chosenNodeName = text.getText().toString();
//DirectoryItem node = navigator.getDirectoryItem(chosenNodeName);
DirectoryItem node = navigator.getDirectoryItem(position);
if(node.getFileCode() == -1) //it is a directory therefore navigates into it
updateView(navigator.goToSubDirectory(chosenNodeName));
updateView(navigator.goToSubDirectory(position));
//updateView(navigator.goToSubDirectory(chosenNodeName));
else{ //it is a files therefore gets its information through web service GETFILE
chosenNodeName = node.getName();
AlertDialog fileInfoDialog = createFileInfoDialog(node.getName(),node.getSize(),node.getTime(),node.getPublisher(),node.getFileCode());
fileInfoDialog.show();
}
@ -419,7 +422,7 @@ public class DownloadsManager extends MenuActivity {
* @param fileSize - file size of the file. It is used to show the download progress in the notification
* */
private void downloadFile(String directory, String url,long fileSize){
new FileDownloaderAsyncTask(getApplicationContext(),true,fileSize).execute(directory,url);
new FileDownloaderAsyncTask(getApplicationContext(),this.chosenNodeName,true,fileSize).execute(directory,url);
}
private void openFileDefaultApp(String absolutePath){
File file = new File(absolutePath);

View File

@ -56,7 +56,8 @@ public class FileDownloaderAsyncTask extends AsyncTask<String,Integer,Boolean> {
this.fileName = text;
this.progressBar = progressBar;
}*/
public FileDownloaderAsyncTask(Context context, boolean notification, long fileSize){
public FileDownloaderAsyncTask(Context context,String fileName, boolean notification, long fileSize){
this.fileName = fileName;
mNotification = new DownloadNotification(context);
this.notification = notification;
this.fileSize = fileSize;
@ -120,12 +121,11 @@ public class FileDownloaderAsyncTask extends AsyncTask<String,Integer,Boolean> {
/* The downloaded file will be saved to a temporary file, whose prefix
* will be the basename of the file and the suffix its extension */
String filename = FileDownloader.getFilenameFromURL(url.getPath());
//if(filename == null)
//if(FileDownloader.getFilenameFromURL(url.getPath() == null)
// throw new FileNotFoundException("URL does not point to a file");
int lastSlashIndex = filename.lastIndexOf("/");
int lastDotIndex = filename.lastIndexOf(".");
int lastSlashIndex = this.fileName.lastIndexOf("/");
int lastDotIndex = this.fileName.lastIndexOf(".");
/* Avoid StringIndexOutOfBoundsException from being thrown if the
* file has no extension (such as "http://www.domain.com/README" */
@ -133,26 +133,25 @@ public class FileDownloaderAsyncTask extends AsyncTask<String,Integer,Boolean> {
String extension = "";
if(lastDotIndex == -1)
basename = filename.substring(lastSlashIndex + 1);
basename = this.fileName.substring(lastSlashIndex + 1);
else {
basename = filename.substring(lastSlashIndex + 1, lastDotIndex);
extension = filename.substring(lastDotIndex);
basename = this.fileName.substring(lastSlashIndex + 1, lastDotIndex);
extension = this.fileName.substring(lastDotIndex);
}
/* The prefix must be at least three characters long */
// if(basename.length() < 3)
// basename = "tmp";
File output = new File(this.getDownloadDir(),filename) ;
File output = new File(this.getDownloadDir(),this.fileName) ;
if(output.exists()){
int i = 1;
do{
output = new File(this.getDownloadDir(),basename+"-"+String.valueOf(i)+extension);
++i;
}while(output.exists());
filename = basename+"-"+String.valueOf(i-1)+extension;
this.fileName = basename+"-"+String.valueOf(i-1)+extension;
}
this.fileName = filename;
mNotification.createNotification(this.fileName);