From 181a94edbe413f9e13a3ae2fa0851fada62b3c72 Mon Sep 17 00:00:00 2001 From: M M Arif Date: Sun, 8 Mar 2020 12:46:31 +0500 Subject: [PATCH] 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