diff --git a/app/src/main/java/com/gmail/afonsotrepa/pocketgopher/gopherclient/Page.java b/app/src/main/java/com/gmail/afonsotrepa/pocketgopher/gopherclient/Page.java index eaeb082..3a26998 100644 --- a/app/src/main/java/com/gmail/afonsotrepa/pocketgopher/gopherclient/Page.java +++ b/app/src/main/java/com/gmail/afonsotrepa/pocketgopher/gopherclient/Page.java @@ -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 @@ -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() @@ -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 { @@ -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) + ")" ); } } @@ -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(); } @@ -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(); + } + }); }