From 3794d7ff2b8e6dbdfab4b7b699ea86a95341f178 Mon Sep 17 00:00:00 2001 From: M M Arif Date: Sun, 8 Mar 2020 10:53:00 +0500 Subject: [PATCH 1/3] wip on download files --- app/src/main/AndroidManifest.xml | 1 + .../gitnex/activities/FileViewActivity.java | 89 ++++++++++++++++++- .../BottomSheetFileViewerFragment.java | 57 ++++++++++++ .../res/drawable/ic_file_download_24dp.xml | 5 ++ .../res/layout/bottom_sheet_file_viewer.xml | 36 ++++++++ app/src/main/res/values/strings.xml | 1 + 6 files changed, 187 insertions(+), 2 deletions(-) create mode 100644 app/src/main/java/org/mian/gitnex/fragments/BottomSheetFileViewerFragment.java create mode 100644 app/src/main/res/drawable/ic_file_download_24dp.xml create mode 100644 app/src/main/res/layout/bottom_sheet_file_viewer.xml diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 76675bfd..e8aff7c2 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -5,6 +5,7 @@ + + + diff --git a/app/src/main/res/layout/bottom_sheet_file_viewer.xml b/app/src/main/res/layout/bottom_sheet_file_viewer.xml new file mode 100644 index 00000000..180ffd3d --- /dev/null +++ b/app/src/main/res/layout/bottom_sheet_file_viewer.xml @@ -0,0 +1,36 @@ + + + + + + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 1af444d0..570bf1f1 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -551,4 +551,5 @@ Pull Request was merged successfully Pull Request is not available for merge + Download From 181a94edbe413f9e13a3ae2fa0851fada62b3c72 Mon Sep 17 00:00:00 2001 From: M M Arif Date: Sun, 8 Mar 2020 12:46:31 +0500 Subject: [PATCH 2/3] download file and ask for permissions to write into storage --- .../gitnex/activities/FileViewActivity.java | 116 ++++++++++++------ .../java/org/mian/gitnex/helpers/Toasty.java | 15 +++ .../main/res/layout/custom_toast_error.xml | 18 +++ app/src/main/res/values/strings.xml | 4 +- 4 files changed, 115 insertions(+), 38 deletions(-) create mode 100644 app/src/main/res/layout/custom_toast_error.xml diff --git a/app/src/main/java/org/mian/gitnex/activities/FileViewActivity.java b/app/src/main/java/org/mian/gitnex/activities/FileViewActivity.java index a4dd47f1..aa7348dc 100644 --- a/app/src/main/java/org/mian/gitnex/activities/FileViewActivity.java +++ b/app/src/main/java/org/mian/gitnex/activities/FileViewActivity.java @@ -1,10 +1,11 @@ package org.mian.gitnex.activities; import android.content.Context; -import android.graphics.Bitmap; +import android.content.pm.PackageManager; import android.graphics.BitmapFactory; import android.graphics.drawable.BitmapDrawable; import android.graphics.drawable.Drawable; +import android.os.Build; import android.os.Bundle; import android.os.Environment; import android.text.method.ScrollingMovementMethod; @@ -20,6 +21,8 @@ import android.widget.ProgressBar; import android.widget.TextView; import androidx.annotation.NonNull; import androidx.appcompat.widget.Toolbar; +import androidx.core.app.ActivityCompat; +import androidx.core.content.ContextCompat; import com.github.barteksc.pdfviewer.PDFView; import com.github.barteksc.pdfviewer.util.FitPolicy; import com.github.chrisbanes.photoview.PhotoView; @@ -34,15 +37,12 @@ import org.mian.gitnex.helpers.Toasty; import org.mian.gitnex.models.Files; import org.mian.gitnex.util.AppUtil; import org.mian.gitnex.util.TinyDB; - import java.io.File; -import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.UnsupportedEncodingException; import java.net.URLDecoder; import java.util.Objects; - import retrofit2.Call; import retrofit2.Callback; @@ -63,7 +63,8 @@ public class FileViewActivity extends BaseActivity implements BottomSheetFileVie private PDFView pdfView; private LinearLayout pdfViewFrame; private byte[] decodedPdf; - private Boolean $nightMode; + private Boolean pdfNightMode; + private static final int PERMISSION_REQUEST_CODE = 1; @Override protected int getLayoutResourceId(){ @@ -103,6 +104,8 @@ public class FileViewActivity extends BaseActivity implements BottomSheetFileVie initCloseListener(); closeActivity.setOnClickListener(onClickListener); + tinyDb.putString("downloadFileContents", ""); + try { singleFileName = URLDecoder.decode(singleFileName, "UTF-8"); @@ -147,7 +150,7 @@ public class FileViewActivity extends BaseActivity implements BottomSheetFileVie String fileExtension = FilenameUtils.getExtension(filename); mProgressBar.setVisibility(View.GONE); - // download contents + // download file meta tinyDb.putString("downloadFileName", filename); tinyDb.putString("downloadFileContents", response.body().getContent()); @@ -182,7 +185,7 @@ public class FileViewActivity extends BaseActivity implements BottomSheetFileVie singleCodeContents.setVisibility(View.GONE); pdfViewFrame.setVisibility(View.VISIBLE); - $nightMode = tinyDb.getBoolean("enablePdfMode"); + pdfNightMode = tinyDb.getBoolean("enablePdfMode"); decodedPdf = Base64.decode(response.body().getContent(), Base64.DEFAULT); pdfView.fromBytes(decodedPdf) @@ -200,7 +203,7 @@ public class FileViewActivity extends BaseActivity implements BottomSheetFileVie .fitEachPage(true) .pageSnap(false) .pageFling(true) - .nightMode($nightMode) + .nightMode(pdfNightMode) .load(); } @@ -285,45 +288,84 @@ public class FileViewActivity extends BaseActivity implements BottomSheetFileVie @Override public void onButtonClicked(String text) { - final TinyDB tinyDb = new TinyDB(getApplicationContext()); - switch (text) { case "downloadFile": - //startActivity(new Intent(FileViewActivity.this, CreateNewUserActivity.class)); - byte[] img = tinyDb.getString("downloadFileContents").getBytes(); - Bitmap bitmap = BitmapFactory.decodeByteArray(img, 0, tinyDb.getString("downloadFileContents").length()); - /*new ImageSaver(getApplicationContext()). - setFileName("1.jpg"). - setDirectoryName("images"). - save(bitmap);*/ - - final File dwldsPath = new File(Environment.getExternalStorageDirectory().getPath() + "/Download/1.pdf"); - try { - dwldsPath.createNewFile(); - } catch (IOException e) { - e.printStackTrace(); + if (Build.VERSION.SDK_INT >= 23) + { + if (checkPermission()) + { + requestFileDownload(); + } + else { + requestPermission(); + } } - byte[] pdfAsBytes = Base64.decode(img, 0); - FileOutputStream os = null; - try { - os = new FileOutputStream(dwldsPath, false); - Objects.requireNonNull(os).write(pdfAsBytes); - os.flush(); - os.close(); - } catch (FileNotFoundException e) { - e.printStackTrace(); - } catch (IOException e) { - e.printStackTrace(); + else + { + requestFileDownload(); } - - - Log.i("imgsaved", tinyDb.getString("downloadFileName")); break; + } } + private void requestFileDownload() { + + final TinyDB tinyDb = new TinyDB(getApplicationContext()); + + if(!tinyDb.getString("downloadFileContents").isEmpty()) { + + File outputFileName = new File(tinyDb.getString("downloadFileName")); + final File downloadFilePath = new File(Environment.getExternalStorageDirectory().getPath() + "/Download/" + outputFileName.getName()); + + byte[] pdfAsBytes = Base64.decode(tinyDb.getString("downloadFileContents"), 0); + FileOutputStream fileOutputStream = null; + + try { + + fileOutputStream = new FileOutputStream(downloadFilePath, false); + Objects.requireNonNull(fileOutputStream).write(pdfAsBytes); + fileOutputStream.flush(); + fileOutputStream.close(); + Toasty.info(getApplicationContext(), getString(R.string.downloadFileSaved)); + + } + catch (IOException e) { + Log.e("errorFileDownloading", Objects.requireNonNull(e.getMessage())); + } + + } + else { + Toasty.error(getApplicationContext(), getString(R.string.waitLoadingDownloadFile)); + } + + } + + private boolean checkPermission() { + int result = ContextCompat.checkSelfPermission(this, android.Manifest.permission.WRITE_EXTERNAL_STORAGE); + return result == PackageManager.PERMISSION_GRANTED; + } + + private void requestPermission() { + ActivityCompat.requestPermissions(this, new String[]{android.Manifest.permission.WRITE_EXTERNAL_STORAGE}, PERMISSION_REQUEST_CODE); + } + + @Override + public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { + switch (requestCode) { + case PERMISSION_REQUEST_CODE: + if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) { + Log.i("PermissionsCheck", "Permission Granted"); + } + else { + Log.e("PermissionsCheck", "Permission Denied"); + } + break; + } + } + private void initCloseListener() { onClickListener = new View.OnClickListener() { @Override diff --git a/app/src/main/java/org/mian/gitnex/helpers/Toasty.java b/app/src/main/java/org/mian/gitnex/helpers/Toasty.java index df68e5f1..f8080296 100644 --- a/app/src/main/java/org/mian/gitnex/helpers/Toasty.java +++ b/app/src/main/java/org/mian/gitnex/helpers/Toasty.java @@ -28,4 +28,19 @@ public class Toasty { } + public static void error(Context context, String message) { + + LayoutInflater inflater = LayoutInflater.from(context); + View view = inflater.inflate( context.getResources().getLayout(R.layout.custom_toast_error), null ); + + TextView text = view.findViewById(R.id.toastText); + text.setText(message); + + Toast toast = new Toast(context); + toast.setDuration(Toast.LENGTH_LONG); + toast.setView(view); + toast.show(); + + } + } diff --git a/app/src/main/res/layout/custom_toast_error.xml b/app/src/main/res/layout/custom_toast_error.xml new file mode 100644 index 00000000..7bd724f3 --- /dev/null +++ b/app/src/main/res/layout/custom_toast_error.xml @@ -0,0 +1,18 @@ + + + + + + \ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 570bf1f1..45aea35a 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -551,5 +551,7 @@ Pull Request was merged successfully Pull Request is not available for merge - Download + Download This File + Please wait for the file to load to memory + File is saved to Download directory From f7d5ee9e7ac68feb521cc4ec8398e376e098e8ed Mon Sep 17 00:00:00 2001 From: M M Arif Date: Mon, 9 Mar 2020 21:13:40 +0500 Subject: [PATCH 3/3] Update for issue 267 --- .../mian/gitnex/activities/FileViewActivity.java | 14 ++++++++++++++ .../main/java/org/mian/gitnex/util/AppUtil.java | 13 ++++++++++++- app/src/main/res/values/strings.xml | 1 + 3 files changed, 27 insertions(+), 1 deletion(-) diff --git a/app/src/main/java/org/mian/gitnex/activities/FileViewActivity.java b/app/src/main/java/org/mian/gitnex/activities/FileViewActivity.java index aa7348dc..557b537c 100644 --- a/app/src/main/java/org/mian/gitnex/activities/FileViewActivity.java +++ b/app/src/main/java/org/mian/gitnex/activities/FileViewActivity.java @@ -3,6 +3,7 @@ package org.mian.gitnex.activities; import android.content.Context; import android.content.pm.PackageManager; import android.graphics.BitmapFactory; +import android.graphics.Typeface; import android.graphics.drawable.BitmapDrawable; import android.graphics.drawable.Drawable; import android.os.Build; @@ -11,6 +12,7 @@ import android.os.Environment; import android.text.method.ScrollingMovementMethod; import android.util.Base64; import android.util.Log; +import android.view.Gravity; import android.view.Menu; import android.view.MenuInflater; import android.view.MenuItem; @@ -206,6 +208,18 @@ public class FileViewActivity extends BaseActivity implements BottomSheetFileVie .nightMode(pdfNightMode) .load(); + } + else if (appUtil.excludeFilesInFileViewerExtension(fileExtension)) { // files need to be excluded + + imageView.setVisibility(View.GONE); + singleCodeContents.setVisibility(View.GONE); + pdfViewFrame.setVisibility(View.GONE); + singleFileContentsFrame.setVisibility(View.VISIBLE); + + singleFileContents.setText(getResources().getString(R.string.excludeFilesInFileviewer)); + singleFileContents.setGravity(Gravity.CENTER); + singleFileContents.setTypeface(null, Typeface.BOLD); + } else { // file type not known - plain text view diff --git a/app/src/main/java/org/mian/gitnex/util/AppUtil.java b/app/src/main/java/org/mian/gitnex/util/AppUtil.java index 647e27c5..71451f9f 100644 --- a/app/src/main/java/org/mian/gitnex/util/AppUtil.java +++ b/app/src/main/java/org/mian/gitnex/util/AppUtil.java @@ -218,7 +218,8 @@ public class AppUtil { "coffee", "rc", "rs", "rust", "basic", "clj", "css", "dart", "lisp", "erl", "hs", "lsp", "rkt", "ss", "llvm", "ll", "lua", "matlab", "pascal", "r", "scala", "sql", "latex", "tex", "vb", "vbs", "vhd", "tcl", "wiki.meta", "yaml", "yml", "markdown", "xml", "proto", "regex", "py", "pl", "js", - "html", "htm", "volt", "ini", "htaccess", "conf", "gitignore", "gradle", "txt", "properties", "bat"}; + "html", "htm", "volt", "ini", "htaccess", "conf", "gitignore", "gradle", "txt", "properties", "bat", + "twig"}; return Arrays.asList(extValues).contains(ext); @@ -240,6 +241,16 @@ public class AppUtil { } + public Boolean excludeFilesInFileViewerExtension(String ext) { + + String[] extValues = new String[] {"doc", "docx", "ppt", "pptx", "xls", "xlsx", "xlsm", "odt", + "ott", "odf", "ods", "ots", "exe", "jar", "odg", "otg", "odp", "otp", "bin", "dmg", "psd", + "xcf"}; + + return Arrays.asList(extValues).contains(ext); + + } + public String getLastCharactersOfWord( String str, int count ) { return str.substring(str.length() - count); diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 45aea35a..1660770a 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -554,4 +554,5 @@ Download This File Please wait for the file to load to memory File is saved to Download directory + This file type is not supported in file viewer. Download it instead from the three dotted menu?