From b5b09a3a1040dbce1f13e18475be18c906d63c74 Mon Sep 17 00:00:00 2001 From: jnlon Date: Sat, 24 Aug 2019 20:11:24 -0400 Subject: [PATCH 1/4] Show save as dialog on main thread --- .../pocketgopher/gopherclient/Page.java | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) 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..cfdf888 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 @@ -94,6 +94,8 @@ public void download(final Context context) 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,7 +108,6 @@ public void onClick(final DialogInterface dialog, int which) @Override public void run() { - Handler handler = new Handler(Looper.getMainLooper()); //file is always saved in the download directory atm File file = new File( @@ -216,7 +217,15 @@ public void onClick(DialogInterface dialog, int which) } ); - alertDialog.show(); + // show the alert dialog + handler.post(new Runnable() + { + @Override + public void run() + { + alertDialog.show(); + } + }); } From d135701ce161dc2619d5d2872409c76f34b6ff00 Mon Sep 17 00:00:00 2001 From: jnlon Date: Sat, 24 Aug 2019 20:18:45 -0400 Subject: [PATCH 2/4] Fix filename extension detection regex Fixes a crash when saving files without an extension --- .../com/gmail/afonsotrepa/pocketgopher/gopherclient/Page.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 cfdf888..2ea46c1 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 @@ -123,7 +123,7 @@ public void run() { n += 1; - if (fileName.matches("(.*).(.*)")) + if (fileName.matches("(.*)\\.(.*)")) { file = new File( Environment.getExternalStoragePublicDirectory From 14d5b7fecaf04e93710b34a0e36ed5943b256529 Mon Sep 17 00:00:00 2001 From: jnlon Date: Sat, 24 Aug 2019 20:38:20 -0400 Subject: [PATCH 3/4] Fix client not saving to downloads directory - Add missing "/" that prevented saving to downloads directory - Replace duplicate calls to getExternalStoragePublicDirectory - Fix typo in toast message --- .../pocketgopher/gopherclient/Page.java | 32 +++++++------------ 1 file changed, 11 insertions(+), 21 deletions(-) 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 2ea46c1..c6d03d6 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 @@ -109,12 +109,12 @@ public void onClick(final DialogInterface dialog, int which) public void run() { + //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 - ); + File file = new File(downloadDirectory + "/" + fileName); try { @@ -125,26 +125,16 @@ public void run() 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) + ")" ); } } @@ -179,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(); } From dea7624aae5edfe4749a9598c89ba0444053c9fc Mon Sep 17 00:00:00 2001 From: jnlon Date: Sat, 24 Aug 2019 20:56:31 -0400 Subject: [PATCH 4/4] Apply user file name input from save as dialog --- .../com/gmail/afonsotrepa/pocketgopher/gopherclient/Page.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 c6d03d6..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 @@ -92,7 +92,6 @@ 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()); @@ -114,6 +113,7 @@ public void run() Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).toString(); //file is always saved in the download directory atm + final String fileName = input.getText().toString(); File file = new File(downloadDirectory + "/" + fileName); try