From 8048aa5a457a5499c477555babd60cad02109377 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tobias=20L=C3=A4nge?= Date: Mon, 8 Apr 2024 16:09:01 +0200 Subject: [PATCH] Fix storage permission for pdf export --- app/src/main/AndroidManifest.xml | 8 ++-- .../activities/ExportPDFActivity.java | 40 +++++++++---------- 2 files changed, 22 insertions(+), 26 deletions(-) diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 94f07ae..deb45a8 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -2,6 +2,10 @@ + + + - - - - \ No newline at end of file diff --git a/app/src/main/java/org/secuso/privacyfriendlypaindiary/activities/ExportPDFActivity.java b/app/src/main/java/org/secuso/privacyfriendlypaindiary/activities/ExportPDFActivity.java index ece3d60..30eb0f1 100644 --- a/app/src/main/java/org/secuso/privacyfriendlypaindiary/activities/ExportPDFActivity.java +++ b/app/src/main/java/org/secuso/privacyfriendlypaindiary/activities/ExportPDFActivity.java @@ -29,6 +29,7 @@ License, or (at your option) any later version. import android.widget.DatePicker; import android.widget.Toast; +import androidx.annotation.NonNull; import androidx.appcompat.app.AppCompatActivity; import androidx.core.app.ActivityCompat; import androidx.core.content.ContextCompat; @@ -74,7 +75,7 @@ public class ExportPDFActivity extends AppCompatActivity { private TextInputLayout startDateWrapper; private TextInputLayout endDateWrapper; - private SimpleDateFormat dateFormat = new SimpleDateFormat("dd.MM.yyyy", Locale.US); + private final SimpleDateFormat dateFormat = new SimpleDateFormat("dd.MM.yyyy", Locale.US); private Date startDate; private Date endDate; @@ -134,7 +135,7 @@ public void onClick(View view) { case R.id.btn_export: LiveData fileLive = exportAsPDF(); final ExportPDFActivity activity = this; - fileLive.observe(this, new Observer() { + fileLive.observe(this, new Observer<>() { @Override public void onChanged(File file) { if (file != null) { @@ -196,7 +197,7 @@ public void onDateSet(DatePicker datePicker, int year, int month, int day) { } private LiveData exportAsPDF() { - final MutableLiveData[] file = new MutableLiveData[]{new MutableLiveData<>()}; + final MutableLiveData file = new MutableLiveData<>(); if (startDate == null) { startDateWrapper.setError(getString(R.string.start_date_error)); } else if (endDate == null) { @@ -206,25 +207,22 @@ private LiveData exportAsPDF() { diaryEntriesLive.observe(this, diaryEntryInterfaces -> { long userID = new PrefManager(this).getUserID(); if (userID == AbstractPersistentObject.INVALID_OBJECT_ID) { - file[0].setValue(exportAsPDF(new PdfCreator(this, startDate, endDate, diaryEntryInterfaces, new User()).createPdfDocument())); + file.setValue(exportAsPDF(new PdfCreator(this, startDate, endDate, diaryEntryInterfaces, new User()).createPdfDocument())); } else { LiveData userLive = database.getUserByID(userID); userLive.observe(this, userInterface -> { - file[0].setValue(exportAsPDF(new PdfCreator(this, startDate, endDate, diaryEntryInterfaces, userInterface).createPdfDocument())); + file.setValue(exportAsPDF(new PdfCreator(this, startDate, endDate, diaryEntryInterfaces, userInterface).createPdfDocument())); }); } }); } - return file[0]; + return file; } private File exportAsPDF(PdfDocument doc) { - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { - if (ContextCompat.checkSelfPermission(ExportPDFActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE) - != PackageManager.PERMISSION_GRANTED) { - ActivityCompat.requestPermissions(ExportPDFActivity.this, - new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, - PERMISSION_REQUEST_WRITE_EXTERNAL_STORAGE); + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && Build.VERSION.SDK_INT <= Build.VERSION_CODES.P) { + if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) { + ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, PERMISSION_REQUEST_WRITE_EXTERNAL_STORAGE); return null; } } @@ -237,7 +235,7 @@ private File exportAsPDF(PdfDocument doc) { } } - SimpleDateFormat s = new SimpleDateFormat("ddMMyyyy"); + SimpleDateFormat s = new SimpleDateFormat("ddMMyyyy", Locale.getDefault()); String filename = s.format(startDate) + "-" + s.format(endDate); File file = new File(directory, filename + ".pdf"); if (file.exists()) { @@ -277,17 +275,15 @@ public void onChanged(File file) { } @Override - public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) { + public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { super.onRequestPermissionsResult(requestCode, permissions, grantResults); - switch (requestCode) { - case PERMISSION_REQUEST_WRITE_EXTERNAL_STORAGE: { - if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) { - Toast.makeText(this, getString(R.string.permission_write_granted), Toast.LENGTH_LONG).show(); - } else { - Toast.makeText(this, getString(R.string.permission_write_denied), Toast.LENGTH_LONG).show(); - } - return; + if (requestCode == PERMISSION_REQUEST_WRITE_EXTERNAL_STORAGE) { + if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) { + Toast.makeText(this, getString(R.string.permission_write_granted), Toast.LENGTH_LONG).show(); + } else { + Toast.makeText(this, getString(R.string.permission_write_denied), Toast.LENGTH_LONG).show(); } + return; } }