Skip to content

Commit

Permalink
Fixed a bug that crashed the app when downloading a file that already
Browse files Browse the repository at this point in the history
exists
  • Loading branch information
afonsotrepa committed Nov 16, 2017
1 parent 7949d8b commit 89ede53
Showing 1 changed file with 38 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public abstract class Line extends Page implements Serializable {


/**
* Used to download/save a file
* Opens an interface for the user to download the line
*
* @param context the context of the current activity
*/
Expand Down Expand Up @@ -82,56 +82,51 @@ public void onClick(final DialogInterface dialog, int which) {
.DIRECTORY_DOWNLOADS)
+ "/" + input.getText().toString());

new Thread(new Runnable() {
@Override
public void run() {
try {
if (file.exists()) {
//quit if file already exists
Toast.makeText(context, "File already exists", Toast
.LENGTH_LONG)
.show();
dialog.cancel();
} else {
//create the file
file.createNewFile();
//read and write the file
Connection conn = new Connection(server, port);
conn.getBinary(selector, file);


///TODO: need to add some code so the files get detected
/// by DownloadManager or something (not possible??)
//
//https://developer.android
// .com/reference/android/app/DownloadManager
// .html#addCompletedDownload%28java.lang.String,%20java
// .lang.String,%20boolean,%20java.lang.String,%20java
// .lang.String,%20long,%20boolean%29

//context.getSystemService(DownloadManager.class);
try {
if (file.exists()) {
Toast.makeText(context, "File already exists", Toast.LENGTH_LONG)
.show();
} else {
file.createNewFile();

new Thread(new Runnable() {
@Override
public void run() {
try {
Connection conn = new Connection(server, port);
conn.getBinary(selector, file);

} catch (final IOException e) {
Toast.makeText(context, e.getMessage(), Toast
.LENGTH_LONG).show();
}
}
});

} catch (IOException e) {
Toast.makeText(context, e.getMessage(), Toast.LENGTH_LONG)
.show();
dialog.cancel();
}
Toast.makeText(context, "File saved", Toast.LENGTH_SHORT).show();

///TODO: need to add some code so the files get detected
/// by DownloadManager or something (not possible??)
}
}).start();

Toast.makeText(context, "File saved", Toast.LENGTH_SHORT).show();
} catch (final IOException e) {
Toast.makeText(context, e.getMessage(), Toast.LENGTH_LONG).show();
}
}
});
}
);


alertDialog.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
new DialogInterface.OnClickListener()

{
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
}
);

alertDialog.show();
}
Expand Down Expand Up @@ -191,10 +186,10 @@ public static Line makeLine(Character type, String text, String selector, String

case ';': //video file
return new VideoLine(
text, //remove the type tag
selector,
server,
port);
text, //remove the type tag
selector,
server,
port);

case 's': //audio file
return new AudioLine(
Expand Down

0 comments on commit 89ede53

Please sign in to comment.