Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public void download(final Context context)
}

//AlertDialog to be shown when method gets called
AlertDialog.Builder alertDialog = new AlertDialog.Builder(context);
final AlertDialog.Builder alertDialog = new AlertDialog.Builder(context);
alertDialog.setTitle("Save as:");

//the EditText where the user will input the name of the file
Expand All @@ -92,8 +92,9 @@ public void download(final Context context)
input.setText(selector.substring(selector.lastIndexOf("/") + 1)); //default file name
input.setTextAppearance(context, MainActivity.font);
alertDialog.setView(input);
final String fileName = input.getText().toString();

// create the handler for running UI code on main thread
final Handler handler = new Handler(Looper.getMainLooper());

alertDialog.setPositiveButton("Save",
new DialogInterface.OnClickListener()
Expand All @@ -106,14 +107,14 @@ public void onClick(final DialogInterface dialog, int which)
@Override
public void run()
{
Handler handler = new Handler(Looper.getMainLooper());

//get the downloads directory name
final String downloadDirectory =
Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).toString();

//file is always saved in the download directory atm
File file = new File(
Environment.getExternalStoragePublicDirectory(Environment
.DIRECTORY_DOWNLOADS) +
"/" + fileName
);
final String fileName = input.getText().toString();
File file = new File(downloadDirectory + "/" + fileName);

try
{
Expand All @@ -122,28 +123,18 @@ public void run()
{
n += 1;

if (fileName.matches("(.*).(.*)"))
if (fileName.matches("(.*)\\.(.*)"))
{
file = new File(
Environment.getExternalStoragePublicDirectory
(Environment
.DIRECTORY_DOWNLOADS) +
"/" + fileName.substring(0, fileName
.indexOf
('.')) +
file = new File(downloadDirectory + "/" +
fileName.substring(0, fileName.indexOf('.')) +
"(" + String.valueOf(n) + ")" +
fileName.substring(fileName.indexOf(
'.'))
fileName.substring(fileName.indexOf('.'))
);
}
else
{
file = new File(
Environment.getExternalStoragePublicDirectory
(Environment
.DIRECTORY_DOWNLOADS) +
fileName +
"(" + String.valueOf(n) + ")"
file = new File(downloadDirectory + "/" +
fileName + "(" + String.valueOf(n) + ")"
);
}
}
Expand Down Expand Up @@ -178,7 +169,7 @@ public void run()
public void run()
{
Toast.makeText(context,
"File saved saved as: " + f.getName(),
"File saved as: " + f.getName(),
Toast.LENGTH_SHORT
).show();
}
Expand Down Expand Up @@ -216,7 +207,15 @@ public void onClick(DialogInterface dialog, int which)
}
);

alertDialog.show();
// show the alert dialog
handler.post(new Runnable()
{
@Override
public void run()
{
alertDialog.show();
}
});
}


Expand Down